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?