在下面的代码中,我试图预测糖尿病的可能性。在本节中,我想计算数据帧中为true的false的百分比,此职责的代码似乎正确,但输出错误。
输入:
真实数:268
错误数:500
预期输出:
是:34.90%-------否:65.10%
34.90 + 65.10 = 100.00
我能得到什么:
是:34.90%-------否:50.00%
34.90 + 50.00!= 100
这太奇怪了!因为我们只有对与错(50%,50%)
这是我的代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv('pima-data-Copy1.csv')
df.isnull().values.any()
dibetes_map = {True:1, False:0}
df['diabetes'] = df['diabetes'].map(dibetes_map)
num_true = len(df.loc[df['diabetes'] == True])
num_false = len(df.loc[df['diabetes'] == False])
print("number of true: {0} ({1:2.2f}%)".format(num_true, (num_true/(num_true + num_false))*100))
print("number of false: {0} ({1:2.2f}%)".format(num_false, (num_false/(num_false + num_false))*100))
答案 0 :(得分:1)
第二行的分母中有num_false + num_false
。以下是一些建议的简化:
df = pd.read_csv('pima-data-Copy1.csv')
# df.isnull().values.any() this isn't used anywhere
# dibetes_map = {True:1, False:0}
# df['diabetes'] = df['diabetes'].map(dibetes_map) # this is redundant as you are comparing with True/False
num_true = df['diabetes'].sum()
total = df['diabetes'].count()
num_false = total - num_true
print("number of true: {0} ({1:2.2f}%)".format(num_true, (num_true / total)*100))
print("number of false: {0} ({1:2.2f}%)".format(num_false, (num_false / total)*100)))