gpt4 book ai didi

python - 在Python数据框中分割数据并自动获取数组值

转载 作者:行者123 更新时间:2023-12-01 08:23:19 24 4
gpt4 key购买 nike

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.read_csv('D:\ history/segment.csv')
data = pd.DataFrame(data)
data = data.sort_values(['Prob_score'], ascending=[False])

one = len(data)
actualpaid_overall = len(data.loc[data['paidstatus'] == 1])

data_split = np.array_split(data, 10)

data1 = data_split[0]
actualpaid_ten = len(data1.loc[data1['paidstatus'] == 1])
percent_ten = actualpaid_ten/actualpaid_overall

data2 = data_split[1]
actualpaid_twenty = len(data2.loc[data2['paidstatus'] == 1])
percent_twenty = (actualpaid_twenty/actualpaid_overall) + percent_ten

data3 = data_split[2]
actualpaid_thirty = len(data3.loc[data3['paidstatus'] == 1])
percent_thirty = (actualpaid_thirty/actualpaid_overall) + percent_twenty

data4 = data_split[3]
actualpaid_forty = len(data4.loc[data4['paidstatus'] == 1])
percent_forty = (actualpaid_forty/actualpaid_overall) + percent_thirty

data5 = data_split[4]
actualpaid_fifty = len(data5.loc[data5['paidstatus'] == 1])
percent_fifty = (actualpaid_fifty/actualpaid_overall) + percent_forty

data6 = data_split[5]
actualpaid_sixty = len(data6.loc[data6['paidstatus'] == 1])
percent_sixty = (actualpaid_sixty/actualpaid_overall) + percent_fifty

data7 = data_split[6]
actualpaid_seventy = len(data7.loc[data7['paidstatus'] == 1])
percent_seventy = (actualpaid_seventy/actualpaid_overall) + percent_sixty

data8 = data_split[7]
actualpaid_eighty = len(data8.loc[data8['paidstatus'] == 1])
percent_eighty = (actualpaid_eighty/actualpaid_overall) + percent_seventy

data9 = data_split[8]
actualpaid_ninenty = len(data9.loc[data9['paidstatus'] == 1])
percent_ninenty = (actualpaid_ninenty/actualpaid_overall) + percent_eighty

data10 = data_split[9]
actualpaid_hundred = len(data10.loc[data10['paidstatus'] == 1])
percent_hundred = (actualpaid_hundred/actualpaid_overall) + percent_ninenty

array_x = [10,20,30,40,50,60,70,80,90,100]
array_y = [ percent_ten, percent_twenty, percent_thirty, percent_forty,percent_fifty, percent_sixty, percent_seventy, percent_eighty, percent_ninenty, percent_hundred]

plt.xlabel(' Base')
plt.ylabel(' percent')
ax = plt.plot(array_x,array_y)
plt.minorticks_on()
plt.grid(which='major', linestyle='-', linewidth=0.5, color='0.1')
plt.grid( which='both', axis = 'both', linewidth=0.5,color='0.75')

上面是我的 python 代码,我已将数据帧分成 10 个相等的部分并绘制了图表,但我对此不满意,我有两个问题:

  1. array_x = [10,20,30,40,50,60,70,80,90,100] 在这行代码中我已经手动获取了 x 值,是否有任何可能的方法可以自动处理,因为我已经采取 split(data,10) 它应该显示 10 个数组值

  2. 正如我们所看到的,整个 data1,2,3,4...10 不断重复,是否有解决方案将其写入函数或循环中。

任何有关代码的帮助将不胜感激。谢谢

最佳答案

我相信您需要列表理解,并且可以使用更简单的方式进行计数 - bool 掩码的 sumTrue 值是类似于 1 的过程,然后将 list 转换为 numpy 数组并使用 numpy.cumsum :

data = pd.read_csv('D:\ history/segment.csv')
data = data.sort_values('Prob_score', ascending=False)

one = len(data)
actualpaid_overall = (data['paidstatus'] == 1).sum()

data_split = np.array_split(data, 10)

x = [len(x) for x in data_split]
y = [(x['paidstatus'] == 1).sum()/actualpaid_overall for x in data_split]

array_x = np.cumsum(np.array(x))
array_y = np.cumsum(np.array(y))

plt.xlabel(' Base')
plt.ylabel(' percent')
ax = plt.plot(array_x,array_y)
plt.minorticks_on()
plt.grid(which='major', linestyle='-', linewidth=0.5, color='0.1')
plt.grid( which='both', axis = 'both', linewidth=0.5,color='0.75')
<小时/>

示例:

np.random.seed(2019)
N = 1000
data = pd.DataFrame({'paidstatus':np.random.randint(3, size=N),
'Prob_score':np.random.randint(100, size=N)})
#print (data)

data = data.sort_values(['Prob_score'], ascending=[False])

actualpaid_overall = (data['paidstatus'] == 1).sum()

data_split = np.array_split(data, 10)

x = [len(x) for x in data_split]
y = [(x['paidstatus'] == 1).sum()/actualpaid_overall for x in data_split]

array_x = np.cumsum(np.array(x))
array_y = np.cumsum(np.array(y))

print (array_x)
[ 100 200 300 400 500 600 700 800 900 1000]

print (array_y)
[0.09118541 0.18844985 0.27963526 0.38601824 0.49848024 0.61702128
0.72036474 0.81155015 0.9331307 1. ]

关于python - 在Python数据框中分割数据并自动获取数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54473924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com