수정
상단노출
data1.csv 파일 내려받기
다운로드 바로 가기(클릭)
(6쪽) 3.1절 임포트 코드
import matplotlib.pyplot as plt import pandas as pd import sklearn sklearn.__version__
(9쪽) 3.2절 코드
xlist = [] for x in X : x = float(x) xlist.append(x) ylist = [] for y_ in y : ylist.append(y_) print("xlist") for x in xlist: print(x, end=' ') print(""" """) print("ylist") for t in ylist : print(t, end=' ')
(9쪽) 3.3절 코드
from sklearn.linear_model import LinearRegression reg = LinearRegression() reg.fit(X,y)
(10쪽) 3.4절 코드
y_pred_list = list(y_pred) data_tpl = {'자습시간' : xlist, '시험성적예측값' : y_pred_list, '실제시험성적' : ylist} df = pd.DataFrame(data_tpl, index=[i for i in range(1,21)]) df
(11쪽) 3.5절 코드
plt.scatter(X,y,color = 'blue') plt.plot(X, y_pred, color = 'red') plt.title('Score by Self-Study Time') plt.xlabel('study hours') plt.ylabel('score') plt.show()
(19쪽) 5.3절 코
import numpy as np import matplotlib.pyplot as plt import pandas as pd data_set = pd.read_csv('data1.csv') X = data_set.iloc[:,:-1].values y = data_set.iloc[:, -1].values
(20쪽) 맨 위 코드
xlist = [] for x in X : x = float(x) xlist.append(x) ylist = [] for y_ in y : ylist.append(y_) xlist, ylist
(20쪽) 3단계 코드
import sympy as sy x = sy.symbols('x') a = 0 for x_, y_ in zip(xlist, ylist) : a += (y_ - x_*x)**2 a = a / len(xlist) loss_func = sy.expand(a) print('loss_func :', loss_func) c2 = loss_func.coeff(x,2) c1 = loss_func.coeff(x,1) c0 = loss_func.coeff(x,0)
(21쪽) 코드
opp = 20 alist = [] curr_points = [] while opp>0 : m = input("추세선 기울기 a 값을 입력 (멈추려면 q): ") if m == 'q' : break m=float(m) alist.append(m) x = np.array(range(-5,11)) for m in alist : plt.plot([m-0.5, m+0.5],[c2*m**2+(c1-c2)*m -0.5*c1 +c0, c2*m**2+(c1+c2)*m+c0+0.5*c1], color='blue') mlist = [alist[-1]] x = np.array(mlist) plt.scatter(x, c2*x**2 + c1*x +c0, color='red', label = 'curr') curr_points.append([x,c2*x**2+c1*x +c0]) # print(opp, curr_points) if opp <= 19 : for a in range(0, len(curr_points)-1) : plt.plot([curr_points[a][0], curr_points[a+1][0]], [curr_points[a][1], curr_points[a+1][1]], color = 'orange') plt.legend() plt.show() opp -= 1
전송