2017-09-28 7 views
0

numpyのはTypeError:ufunc「反転」の入力タイプではサポートされていない、と次のコードの入力

def makePrediction(mytheta, myx): 
    # ----------------------------------------------------------------- 
    pr = sigmoid(np.dot(myx, mytheta)) 

    pr[pr < 0.5] =0 
    pr[pr >= 0.5] = 1 

    return pr 

    # ----------------------------------------------------------------- 

# Compute the percentage of samples I got correct: 
pos_correct = float(np.sum(makePrediction(theta,pos))) 
neg_correct = float(np.sum(np.invert(makePrediction(theta,neg)))) 
tot = len(pos)+len(neg) 
prcnt_correct = float(pos_correct+neg_correct)/tot 
print("Fraction of training samples correctly predicted: %f." % prcnt_correct) 

私はこのエラーを取得する:

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-33-f0c91286cd02> in <module>() 
    13 # Compute the percentage of samples I got correct: 
    14 pos_correct = float(np.sum(makePrediction(theta,pos))) 
---> 15 neg_correct = float(np.sum(np.invert(makePrediction(theta,neg)))) 
    16 tot = len(pos)+len(neg) 
    17 prcnt_correct = float(pos_correct+neg_correct)/tot 

TypeError: ufunc 'invert' not supported for the input types, and the inputs 

は、なぜそれが起こって、どのようにされました私はそれを修正することはできますか? documentationから

答えて

1

Parameters:
x : array_like.
Only integer and boolean types are handled."

あなたの元の配列を浮動さ小数点型(sigmoid()の戻り値)。値を0と1に設定しても型は変更されません。 astype(np.int)を使用する必要があります。

neg_correct = float(np.sum(np.invert(makePrediction(theta,neg).astype(np.int)))) 

(未テスト)を使用する必要があります。


これを実行すると、 float()キャストすることも理にかなっています。私はキャストを取り除いて、Pythonに頼って正しいことをしています。それだけで、あなたはまだあなたは、Python 3でそれを行う場合だけでPythonが正しいことを行うようにする

from __future__ import division 

を追加します(それは傷つけることはありません、Pythonの2を使用して(ただし、Pythonの3を使用してください)した場合には
何もしない)。これ(または、いずれにしてもPython 3)を使用すると、コード内の他の数多くのキャストを削除して、可読性を向上させることができます。

関連する問題