Using slices in Python
<p>I use the dataset from UCI repo: <a href="http://archive.ics.uci.edu/ml/datasets/Energy+efficiency" rel="nofollow">http://archive.ics.uci.edu/ml/datasets/Energy+efficiency</a>
Then doing next:</p>

<pre><code>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)
</code></pre>

<p>Then do the models and cross validate:</p>

<pre><code>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)
</code></pre>

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

<pre><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)
</code></pre>

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

<p>How can it be avoided and made working?</p>

<p>Thanks!</p>

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

<pre><code>Xtrn_array = Xtrn.as_matrix() 
Ytrn_array = Ytrn.as_matrix()
</code></pre>

<p>and use Xtrn_array and Ytrn_array when you fit your data to estimators. </p>

python pandas scikit-learn slice prediction