Loan Prediction - Data Conversion Warning

ravel

#1

I am following the steps given in this article and trying to fit various models using the generic function. However, while doing Logistic Regression and RF, I’m getting below warning.

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().

Tried ravel(), flatten(), etc. but not able to get rid of the warning.

I’ve defined the generic function like this -

def classification_model(model,data,predictors,outcome):  
    model.fit(data[predictors],data[outcome])
    predictions = model.predict(data[predictors]) # predictions on train data
    accuracy = metrics.accuracy_score(predictions, data[outcome])
    print ("Accuracy : %s" % "{0:.3%}".format(accuracy))

#And calling it like 
model = LogisticRegression()
data = df
predictors = ['Credit_History','LoanAmount']
outcome = ['Loan_Status']
classification_model(model,data,predictors,outcome)

#2

Hi Pravin,

Please have a look at the code below:

import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

df = pd.read_csv('train_u6lujuX_CVtuZ9i.csv')

df.LoanAmount.fillna(np.mean(df.LoanAmount), inplace=True)
df.Credit_History.fillna(0, inplace=True)

def classification_model(model,data,predictors,outcome):  
    model.fit(data[predictors],data[outcome].values.ravel())
    predictions = model.predict(data[predictors]) # predictions on train data
    accuracy = accuracy_score(predictions, data[outcome])
    print ("Accuracy : %s" % "{0:.3%}".format(accuracy))

#And calling it like -
model = LogisticRegression()
data = df
predictors = ['Credit_History','LoanAmount']
outcome = ['Loan_Status']
classification_model(model,data,predictors,outcome)

Let me know if this helps.
Cheers!


#3

Thanks Ankit. I’ve tried that already but the warning stays.


#4

Hi @pravinmhaske

This works fine for me. Please restart and check it again, it should work.
Let me know if you still face any problem.


#6

predictions = model.predict(data[predictors])

replace above line with: predictions=as.data.frame(model.predict(data[predictors]))