Python 散点图错误:x和y的大小必须相同
我正在使用sklearn进行线性回归。 以下是数据:Python 散点图错误:x和y的大小必须相同,python,python-3.x,python-requests,linear-regression,scatter-plot,Python,Python 3.x,Python Requests,Linear Regression,Scatter Plot,我正在使用sklearn进行线性回归。 以下是数据: Name Symbol Date High Low Open Close Volume Marketcap SNo 2709 Bitcoin BTC 2020-09-27 23:59:59 10803.976080 10622.921758 10746.892758 10775.269376 5.374597e+10
Name Symbol Date High Low Open Close Volume Marketcap
SNo
2709 Bitcoin BTC 2020-09-27 23:59:59 10803.976080 10622.921758 10746.892758 10775.269376 5.374597e+10 1.993580e+11
2710 Bitcoin BTC 2020-09-28 23:59:59 10945.347907 10703.893251 10776.613639 10709.652182 4.776239e+10 1.981541e+11
2711 Bitcoin BTC 2020-09-29 23:59:59 10860.000698 10649.495439 10709.650022 10844.640981 4.658240e+10 2.006601e+11
2712 Bitcoin BTC 2020-09-30 23:59:59 10847.256993 10669.321093 10843.870751 10784.491578 4.417107e+10
代码如下:
# Import all the libraries we need for Linear Regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import linear_model
from sklearn import metrics
from sklearn.metrics import confusion_matrix, accuracy_score
import math
# Splitting the data into the "attributes" and "labels".
x = df[["High", "Low", "Open", "Volume", "Marketcap"]].values
y = df["Close"].values
# Splitting the data into the training and testing sets.
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
# Train the Algorithm
regressor = LinearRegression()
regressor.fit(x_train, y_train)
# Testing the linear regression model
regressor.score(x_test, y_test)
# Predicting the price
y_predicted = regressor.predict(x_test)
# Plot the scatter plot
plt.scatter(x_test, y_test, color='red')
plt.plot(x_test, y_predicted, color='blue', linewidth=3)
plt.show()
我已经按照网上的所有说明进行了操作,但是结果显示x和y的大小必须相同。我不确定应该更改哪个数组,或者是另一个错误。
非常感谢。在控制台中键入len(x)和len(y)。它们是一样的吗?请提供一些x和y的数据,因为此代码中没有用于设置“df”值的行。您用于x和y数据的序列需要具有相同数量的点-您需要检查它们,并确定哪个点太多或太少,或者哪个点的形状不正确。此时,只有您可以这样做,因为我们无法访问您的数据,因为您没有提供完整的回溯,因为您没有提供完整的回溯,我们也不知道是哪个plot语句导致了问题。
我不确定…
在您尝试解决此问题的过程中,您排除了任何可能性吗?-你知道哪些部分是正确的吗?我已经更新了数据。