2017-03-29 5 views
0

私は以前に発生していなかった問題に直面していますが、いくつかのルールが変更されている可能性があります。TypeError: 'float'オブジェクトはインデックスとして解釈できません

Traceback (most recent call last) 
    <ipython-input-3-f47687e192a7> in <module>() 
      5 n_examples = X.shape[0] 
      6 n_train = n_examples * 0.5 
    ----> 7 train_idx = np.random.choice(range(0,n_examples), size=n_train, replace=False) 
      8 test_idx = list(set(range(0,n_examples))-set(train_idx)) 
      9 X_train = X[train_idx] 

    mtrand.pyx in mtrand.RandomState.choice (numpy/random/mtrand/mtrand.c:18822)() 

    TypeError: 'float' object cannot be interpreted as an index 
+2

ちょうど推測ですが、 'n_train = n_examples * 0.5'はintではないかもしれません。してみてください: 'n_train = int(n_examples * 0.5)'? – fredtantini

答えて

2

この問題は、Pythonに付属のrange関数が原因である可能性があります。引数は整数でなければなりません。 n_examples0.5を掛けたときにn_trainが浮動小数点に変換されます。 int(n_examples * 0.5)のようなintに変換するだけです。これは実際には正しいことです。 11の例をお持ちの場合は、5.5のトレーニングとテストの例があることは意味がありません。

+0

ありがとう〜私の問題は修正されました! – iMatrix

+2

これが正しい答えだったら、Horiaが彼の甘い評判のポイントを手に入れられるようにそれにフラグを立ててください。 –