我使用来自UCI repo: http://archive.ics.uci.edu/ml/datasets/Energy+efficiency的数据集然后做下一步:
from pandas import *
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score
from sklearn.cross_validation import train_test_split
dataset = read_excel('/Users/Half_Pint_boy/Desktop/ENB2012_data.xlsx')
dataset = dataset.drop(['X1','X4'], axis=1)
trg = dataset[['Y1','Y2']]
trn = dataset.drop(['Y1','Y2'], axis=1)
然后做模型并交叉验证:
models = [LinearRegression(),
RandomForestRegressor(n_estimators=100, max_features ='sqrt'),
KNeighborsRegressor(n_neighbors=6),
SVR(kernel='linear'),
LogisticRegression()
]
Xtrn, Xtest, Ytrn, Ytest = train_test_split(trn, trg, test_size=0.4)
我正在创建一个预测值的回归模型,但有一个问题。下面是代码:
TestModels = DataFrame()
tmp = {}
for model in models:
m = str(model)
tmp['Model'] = m[:m.index('(')]
for i in range(Ytrn.shape[1]):
model.fit(Xtrn, Ytrn[:,i])
tmp[str(i+1)] = r2_score(Ytest[:,0], model.predict(Xtest))
TestModels = TestModels.append([tmp])
TestModels.set_index('Model', inplace=True)
它显示了线模型的不可哈希类型:'slice'。适合(Xtrn Ytrn[:,我])
如何避免并使其发挥作用?
谢谢!
我想我以前也遇到过类似的问题!在将数据提供给sklearn
估计器之前,尝试将数据转换为numpy数组。它很可能解决了哈希问题。例如:
Xtrn_array = Xtrn.as_matrix()
Ytrn_array = Ytrn.as_matrix()
和使用Xtrn_array和Ytrn_array当你拟合你的数据估计。