0

I use the dataset from UCI repo: http://archive.ics.uci.edu/ml/datasets/Energy+efficiency Then doing next:

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)

Then do the models and cross validate:

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)

I'm creating a regression model for predicting values but have a problems. Here is the code:

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)

It shows unhashable type: 'slice' for line model.fit(Xtrn, Ytrn[:,i])

How can it be avoided and made working?

Thanks!

Keithx
  • 2,994
  • 15
  • 42
  • 71

1 Answers1

1

I think that I had a similar problem before! Try to convert your data to numpy arrays before feeding them to sklearn estimators. It most probably solve the hashing problem. For instance, You can do:

Xtrn_array = Xtrn.as_matrix() 
Ytrn_array = Ytrn.as_matrix()

and use Xtrn_array and Ytrn_array when you fit your data to estimators.

MhFarahani
  • 960
  • 2
  • 9
  • 19