import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import explained_variance_score, mean_squared_error, mean_absolute_error, r2_score
from sklearn.preprocessing import MinMaxScaler
def PrintRegScore(y_true, y_pred):
print('explained_variance_score: {}'.format(explained_variance_score(y_true, y_pred)))
print('mean_squared_errors: {}'.format(mean_squared_error(y_true, y_pred)))
print('r2_score: {}'.format(r2_score(y_true, y_pred)))
"""
score가 높도록 데이터를 생성
"""
x = np.random.normal(0, 1, sample_size)
y_true = x*30 + np.random.normal(0, 10, sample_size)
scaler = MinMaxScaler()
x_scaled = scaler.fit_transform(x.reshape(-1, 1))
plt.figure(figsize=(10, 4))
plt.scatter(x_scaled, y_true, alpha=0.5, c='red')
plt.savefig('../../assets/images/markdown_img/180605_1217_linreg_score_well_fit.svg')
plt.show()
y_pred = LinearRegression().fit(x_scaled, y_true).predict(x_scaled)
PrintRegScore(y_true, y_pred)
"""
분산을 확 늘려서, score가 낮도록 데이터를 생성한 경우
"""
x = np.random.normal(0, 1, sample_size)
y_true = x + np.random.normal(0, 30000, sample_size)
scaler = MinMaxScaler()
x_scaled = scaler.fit_transform(x.reshape(-1, 1))
plt.figure(figsize=(8, 3))
plt.scatter(x_scaled, y_true, alpha=0.5, c='red')
plt.savefig('../../assets/images/markdown_img/180605_1217_linreg_score_bad_fit.svg')
plt.show()
y_pred = LinearRegression().fit(x_scaled, y_true).predict(x_scaled)
print('explained_variance_score: {}'.format(explained_variance_score(y_true, y_pred)))
print('mean_squared_errors: {}'.format(mean_squared_error(y_true, y_pred)))
print('r2_score: {}'.format(r2_score(y_true, y_pred)))
댓글남기기