Reputation: 868
Apologies if I don't get this right the first time, as I am new to both this forum and Python. I am attempting to do logistic regression and would like to calculate the sigmoid function.
Code:
import numpy as np
csv_file_object = csv.reader(open('train.csv', 'rb'))
header = csv_file_object.next()
train_data=[]
for row in csv_file_object:
train_data.append(row[1:])
train_data = np.array(train_data)
X = train_data
X = np.c_[ np.ones(N), X ] # print type(X) gives <type 'numpy.ndarray'>
def sigmoid(z):
s = 1.0 / (1.0 + np.exp**(-1.0 * z))
return s
print sigmoid(X)
Error
When I run this I get the following error:
Traceback (most recent call last): File "C:\Users...", line 63, in
print sigmoid(X)
File "C:\Users...", line 59, in sigmoid
s = 1.0 / (1.0 + np.exp**(-1.0 * z))
TypeError: unsupported operand type(s) for *: 'float' and 'numpy.ndarray'
I have tried switching the 1.0's to 1's and then get 'int' instead of 'float' in the error and using '.astype(np.float)' and other attempts. I have looked for similar questions and have looked at the documentation but have been unable to find a solution (or understand that I was indeed reading a solution!): http://docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html
How to calculate a logistic sigmoid function in Python?
My understanding is the exponential function should perform an element-wise exponentiation for each element in the array.
What am I missing?
Upvotes: 4
Views: 17508
Reputation: 36
Remove the ** and it will be fixed
np.exp
has the power function inside of it that's why you get an error
Upvotes: 2
Reputation: 353549
Aside from the syntax error pointed out by @roippi, you can use the numpy
I/O functions; there's no need to go via the csv module.
Another problem is that the csv
module is giving you strings, and so your train_data
winds up not being an ndarray
with float dtype
(the jargon for what kind of objects are being stored in a numpy array).
For example, something like this should work:
>>> !cat train.csv
a,b,c
1,2,3
2,3,4
4,5,6
>>> train_data = np.loadtxt("train.csv", skiprows=1, delimiter=",")
>>> train_data
array([[ 1., 2., 3.],
[ 2., 3., 4.],
[ 4., 5., 6.]])
>>> np.exp(train_data)
array([[ 2.71828183, 7.3890561 , 20.08553692],
[ 7.3890561 , 20.08553692, 54.59815003],
[ 54.59815003, 148.4131591 , 403.42879349]])
Alternatively, you can simply force a conversion. Starting from what you have, which might be something like this:
>>> train_data
array([['2', '3'],
['3', '4'],
['5', '6']],
dtype='|S1')
(Note the dtype
here.) You can specify the type:
>>> train_data.astype(float)
array([[ 2., 3.],
[ 3., 4.],
[ 5., 6.]])
>>> np.array(train_data, dtype=float)
array([[ 2., 3.],
[ 3., 4.],
[ 5., 6.]])
Upvotes: 0
Reputation: 25974
numpy.exp is a function, and you are trying to apply the exponentiation operator to that function. Python clearly has no idea what you are talking about.
You need to pick either numpy exponentiation or Python exponentiation, not both. Look at the syntax in the documentation that you linked to.
Upvotes: 1