Issue
Let's consider data :
import numpy as np
from sklearn.linear_model import LogisticRegression
x=np.linspace(0,2*np.pi,80)
x = x.reshape(-1,1)
y = np.sin(x)+np.random.normal(0,0.4,80)
y[y<1/2] = 0
y[y>1/2] = 1
clf=LogisticRegression(solver="saga", max_iter = 1000)
I want to fit logistic regression where y is dependent variable, and x is independent variable. But while I'm using :
clf.fit(x,y)
I see error
'y should be a 1d array, got an array of shape (80, 80) instead'.
I tried to reshape data by using
y=y.reshape(-1,1)
But I end up with array of length 6400! (How come?)
Could you please give me a hand with performing this regression ?
Solution
Change the order of your operations:
First geneate x and y as 1-D arrays:
x = np.linspace(0, 2*np.pi, 8)
y = np.sin(x) + np.random.normal(0, 0.4, 8)
Then (after y was generated) reshape x:
x = x.reshape(-1, 1)
Edit following a comment as of 2022-02-20
The source of the problem in the original code is that;
x = np.linspace(0,2*np.pi,80)
- generates a 1-D array.x = x.reshape(-1,1)
- reshapes it into a 2-D array, with one column and as many rows as needed.y = np.sin(x) + np.random.normal(0,0.4,80)
- operates on a columnar array and a 1-D array (treated here as a single row array).- the effect is that y is a 2-D array (80 * 80).
- then the attempt to reshape y gives a single column array with 6400 rows.
The proper solution is that both x and y should be initially 1-D (single row) arrays and my code does just this. Then both arrays can be reshaped.
Answered By - Valdi_Bo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.