# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# <div class="alert alert-success" data-title="">
#   <h2><i class="fa fa-tasks" aria-hidden="true"></i> 보스턴 데이터로 다항회귀  </h2>
# </div>

from sklearn.datasets import load_boston
boston = load_boston()

import pandas as pd
boston_df = pd.DataFrame(boston.data, 
                         columns=boston.feature_names, 
                         index=range(1,len(boston.data)+1))
boston_df['PRICE'] = boston.target
boston_df.head()

# ## X, y 설정 

X = pd.DataFrame(boston_df['RM']) 
y = boston_df['PRICE']









# ## 학습평가 나누기 

# 학습용 및 검증용 데이터로 분리한다
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,
                                                       y,
                                                       test_size=0.3,
                                                       random_state = 42)

# ## 단순선형회귀 

from sklearn.linear_model import LinearRegression
model= LinearRegression()
model.fit(X_train,y_train) #  학습!!
y_pred = model.predict(X_test)

# +
from sklearn.metrics import mean_squared_error, r2_score    # 사이킷런에 있는 평가 지표 함수들

mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)

print('MSE : {0:.3f} , RMSE : {1:.3F}'.format(mse , rmse))
print('R2 score : {0:.3f}'.format(r2_score(y_test, y_pred)))
# -

# ## 2차 다항회귀 

# +
# 2차 다항회귀 
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=2)

X_train_poly = poly.fit_transform(X_train)
X_test_poly = poly.fit_transform(X_test)


poly_model = LinearRegression().fit(X_train_poly,y_train)
poly_pred = poly_model.predict(X_test_poly)


mse = mean_squared_error(y_test, poly_pred)
rmse = np.sqrt(mse)

print('MSE : {0:.3f} , RMSE : {1:.3F}'.format(mse , rmse))
print('R2 score : {0:.3f}'.format(r2_score(y_test, poly_pred)))

# -

# ## 6차 다항회귀 를 사용하고, 그래프에 나타내시오. 

# +
degree_poly_6 = PolynomialFeatures(degree=6) # 다항회귀 설정 
# X_train 변경 
X_train_6 = degree_poly_6.fit_transform(X_train)
# X_test 변경 
X_test_6 = degree_poly_6.fit_transform(X_test)

# 바뀐 X_train, X_test를 가지고 모델 학습 
model_6 = LinearRegression().fit(X_train_6,y_train)
y_pred_6 = model_6.predict(X_test_6)

# +
mse = mean_squared_error(y_test, y_pred_6)
rmse = np.sqrt(mse)

print('MSE : {0:.3f} , RMSE : {1:.3F}'.format(mse , rmse))
print('R2 score : {0:.3f}'.format(r2_score(y_test, y_pred_6)))


# +
import matplotlib.pyplot as plt

xx = np.linspace(X['RM'].min()-0.1,X['RM'].max()+0.1)[:,np.newaxis]
xx2 = poly.fit_transform(xx)
xx3 = degree_poly_6.fit_transform(xx) # 추가 부분

yy1 = model.predict(xx)
yy2 = poly_model.predict(xx2)
yy3 = model_6.predict(xx3) # 추가 부분

plt.scatter(X,y,color='red',alpha=0.1,label = 'Price')
plt.plot(xx,yy1,color='blue',linestyle='--',label='Linear') # 1차식을 그릴거니까
plt.plot(xx,yy2,color='green',linestyle='-',label='Quad') # 2차식을 그릴거
plt.plot(xx,yy3,color='orange', linestyle=':',label='6')# 6차식 추가 

plt.title("Room Number and Price",fontsize=15)
plt.xlabel('Room Number',fontsize=15)
plt.ylabel('Price',fontsize=15)
plt.legend(fontsize=10)
plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered