我收到了这个错误:
ValueError: Found input variables with inconsistent numbers of samples: [6, 4]
我的代码:
from sklearn.datasets import load_bostonfrom sklearn.linear_model import LinearRegressionfrom sklearn.model_selection import train_test_splitimport pandas as pdimport matplotlib.pyplot as pltimport numpy as npfrom sklearn.metrics import mean_squared_error , r2_scoredata = load_boston()boston = pd.DataFrame(data.data , columns=data.feature_names)boston.columns = boston.columns.str.lower()boston['medv'] = data['target']x = boston.lstaty = boston.medvx_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.3,random_state=42)model = LinearRegression()model.fit(x_train,y_train)pred = model.predict(x_test)mean_squared_error('y_test','pred')
回答:
将
mean_squared_error('y_test','pred')
改为
mean_squared_error(y_test,pred)
您比较的是字符串而不是预测值和标签。此外,您没有分配这个计算出的MSE值,也没有打印它等,您可能需要对这个计算值做些什么。