[인공지능수학] 4단원 실습코드

_1. data1.csv 파일 내려받기

다운로드 바로 가기(클릭)

_2. (6쪽) 3.1절 임포트 코드

import matplotlib.pyplot as plt 
import pandas as pd 
import sklearn 
sklearn.__version__

_3. (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=' ')

_4. (9쪽) 3.3절 코드

from sklearn.linear_model import LinearRegression 
reg = LinearRegression() 
reg.fit(X,y)

_5. (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

_6. (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()

_7.(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

_8.(20쪽) 맨 위 코드

xlist = []
for x in X :
  x = float(x)
  xlist.append(x)

ylist = []
for y_ in y :
  ylist.append(y_)

xlist, ylist

_9.(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)

_10. (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

코멘트

댓글 남기기

Cha's Record에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기