^{}需要它的参数X : numpy array or sparse matrix of shape [n_samples,n_features]
y : numpy array of shape [n_samples, n_targets]
这里有1个“feature”和1个“target”,因此输入的预期形状是(n_samples,1)
如果是这样的话x_values=dataframe[['Brain']]
y_values=dataframe[['Body']]
np.array(dataframe['Brain'],dtype=np.float64).reshape(1,-1)的形状是(n_samples,)。
从dataframe列中选择所需形状的另一个选项是将它们广播到具有新轴的2D数组中x_values=dataframe['Brain'].values[:,np.newaxis]
y_values=dataframe['Body'].values[:,np.newaxis]
注意,为了显示一个好的行,您可能需要对x值进行排序。import pandas as pd
from sklearn import linear_model
from matplotlib import pyplot
import numpy as np
#read data
x = np.random.rand(25,2)
x[:,1] = 2*x[:,0]+np.random.rand(25)
dataframe = pd.DataFrame(x,columns=['Brain','Body'])
x_values=dataframe['Brain'].values[:,np.newaxis]
y_values=dataframe['Body'].values[:,np.newaxis]
body_reg = linear_model.LinearRegression()
body_reg.fit(x_values, y_values)
prediction=body_reg.predict(np.sort(x_values, axis=0))
pyplot.scatter(x_values, y_values)
pyplot.plot(np.sort(x_values, axis=0),prediction)
pyplot.show()