Error while implementing logistic regression

Viewed 34

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:

  1. Why have we used lr.score(test_x, test_y) and not lr.score(pred1, test_y) to calculate score as pred1 is our predicted value ?

  2. If I use pred1 --> lr.score(pred1, test_y) then it throws below error & I want to know the reason of this error

     ValueError: 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 single 
    

    feature or array.reshape(1, -1) if it contains a single sample.

  3. 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'
1 Answers

1-) lr.score needs x to predict y' and their true results y as input. this function makes predictions according to given first input test_x and compare results with test_y and gives you a score according to that.

2-) if you give it pred1 as x input it will try to make predictions while using pred1 as x input. seems like x inputs in your dataset are 2d arrays, but pred1 is 1d array. that's why you are getting this error (this function expect x variables as first input not the y').

if you want to get score while using pred1 and test_y;

from sklearn.metrics import accuracy_score

accuracy_score(test_y, pred1)

3-) fit_transform is not available on the updated version of sklearn.linear_model.LogisticRegression. You might be looking at old code.

you can see fit_transform method on the old documentation. But if you check the stable(updated) documentation, you can see that there is no method called fit_transform

Related