선형 회귀 간단한 모델 구현하기

素人·2022년 1월 23일
0

Data

목록 보기
13/30
import numpy as np

data = [1,2,3,5]
arr = np.array(data)
arr.shape

(4,)

arr.dtype

dtype('int32')

np.zeros((10,2))

array([[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]])
; 10행 2열 짜리 행이 모두 0으로 채워진 행렬

import pandas as pd
import matplotlib.pyplot as plt
a = np.array([5,10,15,20])
b = np.array([15,50,34,50])

result = pd.DataFrame({
    "a":a,
    "b":b
    
})

result
a	b

0 5 15
1 10 50
2 15 34
3 20 50

plt.plot(a,b,'*')

#선형 회귀 모델 구현

height = np.array([188,150,180,197,160,175])
height = height.reshape(-1,1)

math = np.array([85,45,80,99,45,75])

from sklearn.linear_model import LinearRegression

line_fitter = LinearRegression()

line_fitter.fit(height,math)
#fit() 함수 : line_fitter.coef : 기울기 저장
#           : line_fitter.intercept_ : 절편 저장

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)

score_predict = line_fitter.predict(height)

plt.plot(height, math, 'x')
plt.plot(height, score_predict)
plt.show()

line_fitter.coef_

array([1.23232984])

line_fitter.intercept_

-144.157722513089

#성능 평가

from sklearn.metrics import mean_squared_error

print("Mean_Sqaured_Error :", mean_squared_error(score_predict, math))

Mean_Sqaured_Error : 17.837150959860406(MSE)

print("RMSE: ", mean_squared_error(score_predict, math)**0.5)

RMSE: 4.22340513802079

print('score: ', line_fitter.score(height, math))

score: 0.9559122942289754

※인프런의 '머신러닝 처음 시작하기'의 선형 회귀 모델 구현을 참고했습니다.
링크텍스트

profile
매일 조금씩:)

0개의 댓글