简体   繁体   English

为什么我的逻辑回归 model 的准确率 go 超过 100%?

[英]Why does my accuracy go over 100% on my logistic regression model?

I am working on a dataset that is a collection of several medical predictor variables and one target variable, used to classify whether a patient has diabetes or not.我正在研究一个数据集,该数据集是几个医学预测变量和一个目标变量的集合,用于对患者是否患有糖尿病进行分类。 I am building my model without using scikit learn / sklearn library.我正在构建我的 model 而不使用 scikit learn / sklearn 库。 I have attached the link to dataset below.我已将链接附加到下面的数据集。

https://www.kaggle.com/uciml/pima-indians-diabetes-database https://www.kaggle.com/uciml/pima-indians-diabetes-database

I have trained and tested mode but I keep getting over 100% accuracy.我已经训练和测试了模式,但我的准确率一直在 100% 以上。 I am very beginner in this field, therefore I apologize if I have made silly mistakes.我是这个领域的初学者,因此如果我犯了愚蠢的错误,我深表歉意。 Below is my code ( and I only use Glucose and DiabetesPedigreeFunction) to classify.下面是我的代码(我只使用 Glucose 和 DiabetesPedigreeFunction)进行分类。

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    %matplotlib inline
    df = pd.read_csv('diabetes.csv')
    df.head()

    df.drop(['BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 
    'Pregnancies', 'Age'], axis = 1, inplace=True)
    df

    positive = df[df['Outcome'].isin([1])]
    negative = df[df['Outcome'].isin([0])]

    fig, ax = plt.subplots(figsize=(12,8))
    ax.scatter(positive['DiabetesPedigreeFunction'],positive['Glucose'], 
    s=50, c='b', marker='o', label='Diabetes')
    ax.scatter(negative['DiabetesPedigreeFunction'],negative['Glucose'], 
    s=50, c='r', marker='x', label='Not Diabetes')
    ax.legend()

    def sigmoid(x):
      return 1/(1 + np.exp(-x))

    
    nums = np.arange(-10, 10, step=1)
    fig, ax = plt.subplots(figsize=(12,8))
    ax.plot(nums, sigmoid(nums), 'r')

    def cost(theta, X, y):
        theta = np.matrix(theta)
        X = np.matrix(X)
        y = np.matrix(y)
        first = np.multiply(-y, np.log(sigmoid(X * theta.T)))
        second = np.multiply((1 - y), np.log(1 - sigmoid(X * theta.T)))
        return np.sum(first - second) / (len(X))

        X.shape, theta.shape, y.shape
        
        cost(theta, X, y)

        def gradient(theta, X, y):
            theta = np.matrix(theta)
            X = np.matrix(X)
            y = np.matrix(y)

            parameters = int(theta.ravel().shape[1])
            grad = np.zeros(parameters)

            error = sigmoid(X * theta.T) - y

            for i in range(parameters):
                term = np.multiply(error, X[:,i])
                grad[i] = np.sum(term) / len(X)

            return grad

    gradient(theta, X, y)
    import scipy.optimize as opt
    result = opt.fmin_tnc(func=cost, x0=theta, fprime=gradient, args=(X, 
    y))

    cost(result[0], X, y)

    def predict(theta, X):
        probability = sigmoid(X * theta.T)
        return [1 if x >= 0.5 else 0 for x in probability]

    theta_min = np.matrix(result[0])
    predictions = predict(theta_min, X)
    correct = [1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0 
    for (a, b) in zip(predictions, y)]
    accuracy = (sum(map(int, correct)) % len(correct))
    print ('accuracy = {}%'.format(accuracy))

my accuracy is 574%.我的准确率是 574%。 I need some feedback.我需要一些反馈。 Thanks in advance.提前致谢。

You used mod instead of division.您使用 mod 而不是除法。

Accuracy should be computed like this:准确度应该这样计算:

accuracy = sum(correct) / len(correct)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的逻辑回归模型准确率达到 100%? - Why am I getting 100% accuracy for my logistic regression model? 为什么我的逻辑回归的准确性这么小? - Why the accuracy of my logistic regression is so small? 如何提高我的逻辑回归 model 的准确度和精度? - How to Increase accuracy and precision for my logistic regression model? 为什么该模型在 SVM、随机森林分类器和逻辑回归方面获得 100% 的准确率? - Why is the model getting 100% accuracy for SVM, Random-forest Classifier and Logistic Regression? 为什么我自己的逻辑回归实现与 sklearn 不同? - why does my own implementation of logistic regression differ from sklearn? 为什么我的逻辑回归只产生一个 class? - Why does my logistic regression yield only one class? 为什么 Dropout 会降低我的 model 精度? - Why does Dropout deteriorates my model accuracy? 为什么我的逻辑回归分数总是 1.0? - Why my Logistic Regression Score is always 1.0? 为什么我在 Tensorflow 中的逻辑回归分类器没有学习? - Why is my logistic regression classifier in Tensorflow not learning? 使用Python,使我的逻辑回归测试精度更接近我的训练准确度 - Making my logistic regression testing accuracy closer to my training accuracy with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM