Files
serial-position-effect/pre_urp (1).ipynb
T
2026-07-17 20:07:01 +09:00

130 KiB

In [1]:
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error
import pandas as pd
In [2]:
np.random.seed(0)

x = np.arange(0,10,0.4).reshape(-1,1)
y = 3*x+4+2*np.random.randn(len(x)).reshape(-1,1)

plt.scatter(x,y)
plt.show()
In [3]:
regr1 = linear_model.LinearRegression(fit_intercept=False)
regr1.fit(x, y)

y_pred = regr1.predict(x)

plt.scatter(x,y, color='black')
plt.plot(x, y_pred, color='red', linewidth=2)

plt.show()
In [4]:
x_2d = np.concatenate((x,x**2),axis=1)
y_2d = x**2-10*x+25+2.5*np.random.randn(len(x)).reshape(-1,1)

plt.scatter(x,y_2d)
plt.show()
In [5]:
regr2 = linear_model.LinearRegression(fit_intercept=True)
regr2.fit(x_2d, y_2d)

y_pred2 = regr2.predict(x_2d)

plt.scatter(x,y_2d, color='black')
plt.plot(x, y_pred2, color='red', linewidth=2)

plt.show()
In [6]:
df = pd.read_excel(io='korean_words.xlsx')

df.head()
Out [6]:
1음절 2음절 3음절 4음절 5음절 6음절이상
0 검도 자긍심 어림짐작 우스갯소리 어중이떠중이
1 결혼 전염병 인문과학 청딱따구리 자이로스코프
2 녹차 주인공 작심삼일 크리스마스 진인사대천명
3 나비 칸막이 착시효과 아르헨티나 한해살이식물
4 행운 친근감 중구난방 헌법재판소 목도리도마뱀
In [7]:
np.random.seed(0)

# 1음절 2음절 3음절 섞기
###########################
sw_arr = df.iloc[:,:3].to_numpy().reshape(-1,)
np.random.shuffle(sw_arr)
###########################


# 10 10 20 20으로 분할
###########################
ace_list = sw_arr[:10]
ade_list = sw_arr[10:20]
acf_list = sw_arr[20:40]
adf_list = sw_arr[40:]
###########################
In [8]:
ace_list
Out [8]:
array(['황무지', '농산물', '칼국수', '박사', '칸막이', '자긍심', '사랑', '행복', '어깨', '만두'],
      dtype=object)
In [9]:
ade_list
Out [9]:
array(['결혼', '나비', '학', '대통령', '회', '역사', '자연', '녹차', '친근감', '경기도'],
      dtype=object)
In [10]:
acf_list
Out [10]:
array(['무기질', '횡경막', '칼', '값', '솥', '캥거루', '코', '빛', '전염병', '보물', '등교',
       '죄', '폐막식', '저축', '주인공', '행운', '무용', '수리', '태양풍', '춤'],
      dtype=object)
In [11]:
adf_list
Out [11]:
array(['칡', '겁', '단백질', '검도', '못', '윤곽', '턱', '닭', '하반신', '힘', '짐', '루비',
       '땀', '털', '졸업', '논', '낮', '오렌지', '동남아', '대장균'], dtype=object)
In [12]:
np.random.seed(0)

# 4음절 5음절 6음절 섞기
###########################
lw_arr = df.iloc[:,3:].to_numpy().reshape(-1,)
np.random.shuffle(sw_arr)
###########################

# 10 10 20 20으로 분할
###########################
bce_list = lw_arr[:10]
bde_list = lw_arr[10:20]
bcf_list = lw_arr[20:40]
bdf_list = lw_arr[40:]
###########################
In [13]:
bce_list
Out [13]:
array(['어림짐작', '우스갯소리', '어중이떠중이', '인문과학', '청딱따구리', '자이로스코프', '작심삼일',
       '크리스마스', '진인사대천명', '착시효과'], dtype=object)
In [14]:
bde_list
Out [14]:
array(['아르헨티나', '한해살이식물', '중구난방', '헌법재판소', '목도리도마뱀', '천재지변', '황소개구리',
       '패러글라이딩', '팔방미인', '게으름뱅이'], dtype=object)
In [15]:
bcf_list
Out [15]:
array(['자유의여신상', '포화상태', '이데올로기', '콘트라베이스', '하루아침', '순두부찌개', '베이킹파우더',
       '해수욕장', '대중목욕탕', '에스컬레이터', '허수아비', '동음이의어', '장대높이뛰기', '홍익인간',
       '프로그래밍', '히말라야산맥', '훈민정음', '정월대보름', '나무아미타불', '거두절미'], dtype=object)
In [16]:
bdf_list
Out [16]:
array(['식기세척기', '스카치테이프', '대중교통', '지구온난화', '아르키메데스', '두드러기', '아이스크림',
       '조선왕조실록', '만류인력', '오케스트라', '증조할아버지', '물리화학', '김치볶음밥', '가시불가사리',
       '가시광선', '최소공배수', '금메달리스트', '고등학교', '장수풍댕이', '폭탄먼지벌레'], dtype=object)
In [29]:
dummy = pd.read_excel(io='exp_results.xlsx', sheet_name = 'Dummy')

dummy.head()
Out [29]:
1 2 3 4 5 6 7 8 9 10
0 1 1 1 0 1 0 0 1 1 1
1 1 1 0 0 1 0 1 1 0 1
2 1 1 1 1 0 1 0 1 1 1
3 0 0 1 1 0 0 1 0 1 1
4 1 1 0 0 1 1 1 0 1 1
In [32]:
a = temp.mean().to_numpy()
x = np.arange(10).reshape(-1,1) + 1

plt.scatter(x,a)
plt.show()
In [38]:
x_2d = np.concatenate((x**2,x),axis=1)

regr3 = linear_model.LinearRegression(fit_intercept=True)
regr3.fit(x_2d, a)

a_pred = regr3.predict(x_2d)

plt.scatter(x,a, color='black')
plt.plot(x, a_pred, color='red', linewidth=2)

plt.show()
In [39]:
regr3.coef_
Out [39]:
array([ 0.01116427, -0.12344498])
In [40]:
regr3.intercept_
Out [40]:
0.9649122807017545
In [ ]: