2013-09-01 53 views
14

這裏是我的代碼:NumPy的AttributeError的:「浮動」對象有沒有屬性「EXP」

def sigmoid(X, T): return (1.0/(1.0 + np.exp(-1.0*np.dot(X, T)))) 

而此行給我的錯誤「AttributeError的:‘浮動’對象有沒有屬性‘EXP’」。 X,T是Numpy ndarray。

+4

看起來您已將'np'重新賦值爲float值。 –

+4

'X'或'T'偶然會被'object'的dtype而不是'float64'創建? – user2357112

+1

不,重新分配沒有發生。類型(X)是numpy ndarray,類型(X [0] [0])是float –

回答

11

也許有一些錯誤X的輸入值和/或T.從有問題的功能工作正常:

import numpy as np 
from math import e 

def sigmoid(X, T): 
    return 1.0/(1.0 + np.exp(-1.0 * np.dot(X, T))) 

X = np.array([[1, 2, 3], [5, 0, 0]]) 
T = np.array([[1, 2], [1, 1], [4, 4]]) 

print X.dot(T) 
print 
# Just to see if values are ok 
print [1./(1. + e ** el) for el in [-5, -10, -15, -16]] 
print 
print sigmoid(X, T) 

結果:

[[15 16] 
[ 5 10]] 

[0.9933071490757153, 0.9999546021312976, 0.999999694097773, 0.9999998874648379] 

[[ 0.99999969 0.99999989] 
[ 0.99330715 0.9999546 ]] 

也許它是你輸入的D型陣列。更改X到:

X = np.array([[1, 2, 3], [5, 0, 0]], dtype=object) 

給出:

Traceback (most recent call last): 
    File "/[...]/stackoverflow_sigmoid.py", line 24, in <module> 
    print sigmoid(X, T) 
    File "/[...]/stackoverflow_sigmoid.py", line 14, in sigmoid 
    return 1.0/(1.0 + np.exp(-1.0 * np.dot(X, T))) 
AttributeError: exp 
+3

是的謝謝。我不知道dtype,只使用type(X)。我做了X = X.astype(float),它工作。 –

+0

所以像mean()這樣的數組操作不能在數組中使用dype = object?我想知道爲什麼? – gcamargo

+2

這個錯誤信息是非常誤導性的:問題是'dtype'實際上是'numpy.object',但是消息說''numpy.float64'沒有'log10'屬性,或者任何算術方法 – deeenes

4

您轉換類型np.dot(X, T)到FLOAT32這樣的:

z=np.array(np.dot(X, T),dtype=np.float32)

def sigmoid(X, T): 
    return (1.0/(1.0 + np.exp(-z))) 

希望它最終將工作!

相關問題