[code] 4๋‹จ์› ์‹ค์Šต์ฝ”๋“œ

์ˆ˜์ • ๋ชฉ๋ก

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