# Logistic regression score is negative

I am trying to implement logistic regression algorithm. I am using sklearn for this purpose. When I am printing the accuracy its printing negative value.

Code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
import scipy
from scipy.stats import spearmanr
import sklearn
from sklearn.preprocessing import scale
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
from sklearn import preprocessing

student.columns=['score1','score2','res']

#seperate input and output data
X=student.ix[:,(0,1)].values
student_data_names=['score1','score2']

y=student.ix[:,2].values
#print("all done")

#check missing values
#print(student.isnull().sum())

#check if output contains other than 0 or 1
#plt.show(sb.countplot(x='res', data=student))

#print(student.info())

X = scale(X)
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2)

LogReg=LogisticRegression(C=2.0)
LogReg.fit(X_train,y_train)
dat1=LogReg.predict(X_test)

print(r2_score(y_test,dat1))


Output:

-1.1818181818181817


Each time the output is different but everytime it is negative. How can I get best and correct accuracy result. • I am confused. You are doing a Logistic Regression and using r2_score to quantify the quality of your prediction? Logistic Regression is for binary classification, and your problem seems to be a binary classification (positive and negative), but r2_score is for regression!! You have to use classification metrics: scikit-learn.org/stable/modules/… – TwinPenguins Nov 4 '18 at 18:17

Perhaps try a different algorithm/ model or tune the parameters.

It is possible for r2_score to be negative.

There are cases where the computational definition of $$R^2$$ can yield negative values, depending on the definition used. This can arise when the predictions that are being compared to the corresponding outcomes have not been derived from a model-fitting procedure using those data. Even if a model-fitting procedure has been used, $$R^2$$ may still be negative, for example when linear regression is conducted without including an intercept, or when a non-linear function is used to fit the data. In cases where negative values arise, the mean of the data provides a better fit to the outcomes than do the fitted function values, according to this particular criterion. Since the most general definition of the coefficient of determination is also known as the Nash–Sutcliffe model efficiency coefficient, this last notation is preferred in many fields, because denoting a goodness-of-fit indicator that can vary from $$-\infty$$ to $$1$$ (i.e., it can yield negative values) with a squared letter is confusing.
• From your code, it seems you are invoking sklearn.metrics.r2_score correctly, i.e. r2_score(y_true, y_pred).