# 파티셔닝하기 >>> X = df_adv.loc[:, ['TV']] >>> Y = df_adv['sales'] >>> print(X.shape) (200, 1) >>> X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=0.3, random_state=42)
2. 위의 X_train과 Y_train으로 선형 회귀모형 만들기
>>> regr = linear_model.LinearRegression() >>> regr.fit(X_train, Y_train) >>> regr.score(X_train, Y_train) # R^2 0.5736021199591975 >>> regr.coef_ array([0.0464078])
3. 위의 모형에 X_test로 예측값 계산하고 실제 Y_test와 비교해 MSE를 구하고 결과 그리기
>>> Y_pred = regr.predict(X_test) >>> np.mean((Y_pred - Y_test) ** 2) # MSE 8.970991242413616 >>> plt.scatter(X_test, Y_test, color='black') >>> plt.plot(X_test, Y_pred, color='blue', linewidth=3) >>> plt.xlabel("TV") >>> plt.ylabel("sales") >>> plt.show()