Reputation: 9
I’m working on Logistic Regression. I’m not very familiar with Multiple Logistic Regression coding and procedure, however I tried my best based on Rashida Nasrin Sucky’s in Towards Data Science. Dataset in analysis has 226 rows and three columns and one target with three classes.
I have used Sucky's Python program codes and completed most of steps to classify targets in one of labels, (0 or 1). First I introduced gradient descent function as follows:
def gradient_descent(X, y, theta, alpha, epochs):
m = len(X)
for i in range(0, epochs):
for j in range(0, 3):
theta = pd.DataFrame(theta)
h = hypothesis(theta.iloc[:,j], X)
for k in range(0, theta.shape[0]):
theta.iloc[k, j] -= (alpha/m) * np.sum((h-y.iloc[:, j])*X.iloc[:, k])
theta = pd.DataFrame(theta)
return theta, cost
Then, I entered the following code:
theta = np.zeros([churn_df.shape[1]+1, y1.shape[1]])
theta = gradient_descent(X, y1, theta, 0.02, 1000)
I do not have any errors with the above codes, but error faced when I tried to execute the following step:
output = []
for i in range(0, 3):
theta1 = pd.DataFrame(theta)
h = hypothesis(theta1.iloc[:,i], X)
output.append(h)
output = pd.DataFrame(output)
But, when I tried to execute, I faced with the following error:
ValueError: shapes (2,) and (4,226) not aligned: 2 (dim 0) != 4 (dim 0)
I don't know why it is giving this error. Please find the attached screenshot and if possible propose a specific solution to tackle the problem. If you interested in further information, please file free. Thanks.
Upvotes: 0
Views: 990
Reputation: 41
In the line h = hypothesis(theta1.iloc[:,i], X)
it seems like the size of the vector theta1.iloc[:,i]
and the number of rows in X
are not equal. You can't do matrix multiplication unless you have an n by m matrix and an m by p matrix: the number of columns of the left operand must be the same as the number of rows of the right operand.
The easiest way to find the problem in my opinion would be to add print(theta.shape)
statements throughout your code to see if the shape of theta
changes unexpectedly. Without looking any further, maybe your gradient_descent
function returns theta
with a different shape.
Upvotes: 1