I am practicing implementing logistic regression and have some doubts about the below code which I have taken from the site.
y=data['Survived'] # Target Variable
x=data.drop(['Survived'],axis=1)
from sklearn.model_selection import train_test_split
train_x,test_x,train_y,test_y=train_test_split(x,y,random_state=101,stratify=y)
r=LogisticRegression()
val=lr.fit(train_x,train_y)
pred1=val.predict(test_x)
lr.score(test_x, test_y),pred1[:10]
The output of the above code is:
(0.7757847533632287, array([0, 0, 0, 0, 1, 1, 0, 1, 0, 0], dtype=int64))
My questions:
Why have we used
lr.score(test_x, test_y)and notlr.score(pred1, test_y)to calculate score aspred1is our predicted value ?If I use pred1 -->
lr.score(pred1, test_y)then it throws below error & I want to know the reason of this errorValueError: Expected 2D array, got 1D array instead: array=[0 0 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1 0 0 0 1 1 0 0 0 1 0 1 1 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 1 0 0 0 1 0 0 1 0 1 1 0 0 1 0 0 1 1 0]. Reshape your data either using array.reshape(-1, 1) if your data has a singlefeature or array.reshape(1, -1) if it contains a single sample.
If I use fit_transform() instead of fit() in the model then too I get error
lr=LogisticRegression()
val=lr.fit_transform(train_x,train_y)
pred1=val.predict(test_x)
lr.score(test_x, test_y),pred1[:10]
AttributeError: 'LogisticRegression' object has no attribute 'fit_transform'