Entering new data to sklearn model with pickle

Viewed 73

I'm a sklearn beginner and I've trained an logreg algorithm that I would like to feed a new row into to get a prediction. This is the algo:

X=dataframe[['Age', 'DailyRate',
       'DistanceFromHome', 'Education', 'EnvironmentSatisfaction', 'HourlyRate',
       'JobInvolvement', 'JobLevel', 'JobSatisfaction',
        'MonthlyIncome', 'MonthlyRate', 'NumCompaniesWorked',
       'PercentSalaryHike', 'PerformanceRating',
       'RelationshipSatisfaction', 'StockOptionLevel',
       'TotalWorkingYears', 'TrainingTimesLastYear', 'WorkLifeBalance',
       'YearsAtCompany', 'YearsInCurrentRole', 'YearsSinceLastPromotion',
       'YearsWithCurrManager', 'Gender', 'BusinessTravel', 'EducationField']]
y=dataframe[['Attrition']].values.ravel()
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42069)
log_reg=LogisticRegression(C=1000,max_iter=10000)
log_reg.fit(X_train,y_train)

print('Second Attempt at Logistic Regression:')
print('Traning Model accruracy scores: {:.3f}'.format(log_reg.score(X_train,y_train)))
print('Test Model accruracy scores: {:.3f}'.format(log_reg.score(X_test,y_test)))

filename = 'model1.sav'
pickle.dump(log_reg, open('model1.sav', 'wb'))

loaded_model = pickle.load(open('model1.sav', 'rb'))
result = loaded_model.score(X_test, y_test)
print(result)

Essentially I would like to feed in a new row of made up data (something like :(0, 41, Yes, 1, 1102 etc)) and have the algorithm make a prediction on it. Any help would be amazing, thank you very much for reading!

1 Answers

You use the predict method.

pred = loaded_model.predict([0, 41, Yes, 1, 1102])
print(pred)
Related