试图用python学习sklearn。但是jupyter ntbk给出的错误信息是 "ValueError.Expected 2D array, got scalar array instead:array=750.Reshape your data either using array.reshape(-1, 1) if your data has a single feature: Expected 2D array, got scalar array instead:array=750.Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample. "*But I have already defined x to be 2D array using x.values.reshape(-1,1)
你可以在这里找到CSV文件和错误代码的截图-> https:/github.comCaptainRDCSV-for-StackOverflow。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.linear_model import LinearRegression
data = pd.read_csv('1.02. Multiple linear regression.csv')
data.head()
x = data[['SAT','Rand 1,2,3']]
y = data['GPA']
reg = LinearRegression()
reg.fit(x,y)r2 = reg.score(x,y)
n = x.shape[0]
p = x.shape[1]
adjusted_r2 = 1-(1-r2)*(n-1)/(n-p-1)
adjusted_r2
reg.predict(1750)
正如你在代码中看到的,你的x有两个变量。卫星导航仪 和 Rand 1,2,3. 这意味着,你需要提供一个 二维输入 例如,你的预测方法。
reg.predict([[1750, 1]])
返回:
>>> array([1.88])
你面临着这个错误,因为你没有提供第二个值(用于预测方法的 Rand 1,2,3 变量)。) 注意,如果这个变量不重要,你应该从你的x数据中删除它。
这个模型是将两个输入(SAT
和 Rand 1,2,3
)到一个单一的输出(GPA
),因此需要一个由两个元素组成的列表作为有效预测的输入。我猜测 1750
你所提供的东西是为了成为 SAT
值,但您还需要提供 Rand 1,2,3
价值。类似于 [1750, 1]
会工作。