linear model畫圖問題
2019/10/09 下午 04:59
機器學習共學討論版
herohsu
觀看數:47
回答數:3
收藏數:0
ml100-3
ml100-3-d38
在使用linear regression畫圖時,碰到以下問題,
----> 4 plt.scatter(x_test, y_test, color='black')
5 plt.plot(x_test, y_pred, color='blue', linewidth=3)
6 plt.show()
ValueError: x and y must be the same size
我試著去看看test_x, test_y的欄位size, 看起來是正常的
print(x_test.shape, y_test.shape)
(18, 13) (18,)
請問問題在哪
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, linear_model
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score, accuracy_score
from sklearn.preprocessing import StandardScaler
wine = datasets.load_wine()
x_train, x_test, y_train, y_test = train_test_split(wine.data, wine.target, test_size=0.1, random_state=4)
# 建立一個線性回歸模型
regr = linear_model.LinearRegression(normalize=True)
# 將訓練資料丟進去模型訓練
regr.fit(x_train, y_train)
# 將測試資料丟進模型得到預測結果
y_pred = regr.predict(x_test)
print("Mean squared error: %.2f"
% mean_squared_error(y_test, y_pred))
# 畫出回歸模型與實際資料的分佈
print(x_test.shape, y_test.shape)
plt.scatter(x_test, y_test, color='black')
plt.plot(x_test, y_pred, color='blue', linewidth=3)
plt.show()
回答列表
-
2019/10/10 上午 01:31張維元 (WeiYuan)贊同數:0不贊同數:0留言數:0
你的 X 是 18 * 13 代表有 13 個維度?二維的圖怎麼畫 13 個維度?
-
2019/10/14 下午 03:54herohsu贊同數:0不贊同數:0留言數:0
不太了你的意思,13個columns(維度) 不是代表可以畫13條線?
我以為會自動疊加在同一圖,還是要分開畫?
-
2019/10/18 下午 07:25Clement Wong贊同數:0不贊同數:0留言數:1
試着畫了出來, 放在一個圖根本看不到甚麼嘛..