I am trying to solve on my own the first part of the second exercise (logistic regression): https://github.com/jdwittenauer/ipython-notebooks.
I am stuck when calculating the result
variable. This is the last part of the error message:
File "<ipython-input-51-39288db7a045>", line 55, in ComputeCost
h=sigmoid(np.dot(X,theta))
File "<__array_function__ internals>", line 6, in dot
ValueError: shapes (3,) and (100,1) not aligned: 3 (dim 0) != 100 (dim 0)
Here's a piece of my code (sorry for my poor programming skills, I am a beginner):
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy.optimize as opt
data=pd.read_csv('ex2data1.txt', names=['exam1','exam2','admitted'])
def sigmoid(z):
g=1/(1+np.exp(-z))
return g
def whichisy(data, col):
if not isinstance(data, pd.DataFrame):
raise ValueError('Data is not a pandas DataFrame')
y=data.iloc[:,col]
X=data.drop(columns=data.columns[col])
X.insert(0,'Ones', 1)
X=np.array(X)
y=np.array(y)
y=np.reshape(y, (len(y),1))
return X,y
X,y=whichisy(data,2)
def ComputeCost(X, y, theta):
h=sigmoid(np.dot(X,theta))
cost=1/len(y)*np.sum(-y*np.log(h)-(1-y)*np.log(1-h))
return cost
def Gradient(X, y, theta):
g=np.zeros(len(theta))
prodotto=sigmoid(np.dot(X,theta))-y
for k in range(len(theta)):
g[k]=1/len(y)*np.sum(prodotto*np.reshape(X[:,k],(len(X),1)))
return g
theta=np.zeros([np.shape(X)[1],1])
result=opt.fmin_tnc(ComputeCost, theta, fprime=Gradient, args=(X,y))
I saw and understood the code in the solutions of this exercise, but I would like to solve it using my version, but I don't get where I'm wrong.
I have unsuccessfully tried to flatten the returned value of my gradient function (as suggested here matrices are not aligned Error: Python SciPy fmin_bfgs) and the 'y' array (How to get dimensions right using fmin_cg in scipy.optimize).
Thanks!
Edit 1: After placing theta
as the first argument when defining ComputeCost
and Gradient
functions, the fmin_tnc
function runs but it fails the minimization.
result=opt.fmin_tnc(ComputeCost, theta, Gradient, args=(X,y))
And returns: (array([4.60628149e-05, 5.53178320e-03, 5.18798215e-03]), 45, 4)
.
So rc (Return Code) is not 1 as it should be.
I've also tried to run result=opt.minimize(ComputeCost, theta, method='TNC', jac=Gradient, args=(X,y))
to check if the problem were in the fmin_tnc
function, but the returned value is the same.
try move 'theta' ahead of 'X' and 'y' in
def
ofComputeCost
andGradient
e.g.