python - Python - 从函数中转换列表结果?
问题描述
:Edit: 修正了我的一个误解——我得到的是一个嵌套列表,而不是一个数组。 我正在使用 for 循环中的函数 - 引导一些模型预测。
代码如下所示:
def revenue(product):
revenue = predict * 4500
profit = revenue - 500000
return profit
我将其输入的循环如下所示:
# set up a loop to select 500 random samples and train our region 2 data set
model = LinearRegression(fit_intercept = True, normalize = False)
features = r2_test.drop(['product'],axis=1)
values = []
for i in range(1000):
subsample = r2_test.sample(500,replace=False)
features = subsample.drop(['product'],axis=1)
predict = model2.predict(features)
result = (revenue(predict))
values.append(result)
因此对来自该数据帧的 500 个样本进行 1000 次预测循环:
id f0 f1 f2 product
0 74613 -15.001348 -8.276000 -0.005876 3.179103
1 9753 14.272088 -3.475083 0.999183 26.953261
2 93502 6.263187 -5.948386 5.001160 134.766305
3 33405 -13.081196 -11.506057 4.999415 137.945408
4 16486 12.702195 -8.147433 5.004363 134.766305
5 27901 -3.327590 -2.205276 3.003647 84.038886
6 69620 -11.142655 -10.133399 4.002382 110.992147
7 78940 4.234715 -0.001354 2.004588 53.906522
8 56159 13.355129 -0.332068 4.998647 134.766305
9 73142 1.069227 -11.025667 4.997844 137.945408
10 12663 11.777049 -5.334084 2.003033 53.906522
11 39849 16.320755 -0.562946 -0.001783 0.000000
12 61800 7.736313 -6.093374 3.982531 107.813044
13 72213 6.695604 -0.749449 -0.007630 0.000000
14 5479 -10.985487 -5.605994 2.991130 84.038886
15 6297 -0.347599 -6.275884 -0.003448 3.179103
16 88123 12.300570 2.944454 2.005541 53.906522
17 68352 8.900460 -5.632857 4.994324 134.766305
18 99029 -13.412826 -4.729495 2.998590 84.038886
19 64238 -4.373526 -8.590017 2.995379 84.038886
现在,一旦我有了输出,我想从每次迭代中选择前 200 个预测,我正在使用这个循环:
# calculate the max value of each of the 500 iterations, then total them for the total profit
top_200 = []
for i in range(0,500):
profits = values.nlargest(200,[i],keep = 'all')
top_200.append(profits)
我遇到的问题是 - 当我values
输入 top_200 循环时,我最终得到一个按列选择的 200 个数组:
[ 0 1 2 3 \
628 125790.297387 -10140.964686 -361625.210913 -243132.040492
32 125429.134599 -368765.455544 -249361.525792 -497190.522207
815 124522.095794 -1793.660411 -11410.126264 114928.508488
645 123891.732231 115946.193531 104048.117460 -246350.752024
119 123063.545808 -124032.987348 -367200.191889 -131237.863430
.. ... ... ... ...
但我想把它变成一个数据框,但是,我还没有弄清楚如何做到这一点,同时保留 0 有 200 个值、1 有 200 个值等的结构。
我想我可以做类似的事情:
top_200 = pd.DataFrame(top_200,columns= range(0,500))
它给了我 500 列,但只有第 0 列有任何内容,我最终得到一个 [500,500] 数据框,而不是预期的 200 行乘 500 列。
我相当确定有一个很好的方法可以做到这一点,但到目前为止我的搜索还没有出现任何结果。我也不确定我在寻找什么,所以我不确定我到底在寻找什么。
任何输入将不胜感激!提前致谢。
:进一步编辑: 所以现在我知道我正在获取列表列表,而不是数组,我想我会尝试写入数据框:
# calculate the top 200 values of each of the 500 iterations
top_200 = pd.DataFrame(columns=['profits'])
for i in range(0,500):
top_200.loc[i] = i
profits = values.nlargest(200,[i],keep = 'all')
top_200.append(profits)
top_200.head()
但是我在这里做了一些事情,因为我的结果是:
profits
0 0
1 1
2 2
3 3
4 4
我的预期结果是这样的:
col 1 col2 col3
0 first n_largest first n_largest first n_largest
1 second n_largest second n_largest second n_largest
3 third n_largest third n_largest third n_largest
解决方案
因此,在根据@CygnusX 的推荐问题进行了一些研究之后,我发现我在工作的印象是我有一个数组作为输出,但当然 top-200 = [] 是一个列表,当组合时与 nlargest 给我一个列表列表。
现在我更好地理解了这个问题,我将列表列表转换为数据框,然后转置数据 - 这给了我正在寻找的结果。
# calculate the max value of each of the 500 iterations, then total them for the total profit
top_200 = []
for i in range(0,500):
profits = (values.nlargest(200,[i],keep = 'all')).mean()
top_200.append(profits)
test = pd.DataFrame(top_200)
test = test.transpose()
输出(截图,因为,500列。):
可能有一种更优雅的方式来实现这一点,比如不使用列表而是使用数据框,但是,我无法让 .append 在数据框中按照我想要的方式工作,因为我想保留 200 个最大的列表,而不仅仅是总和或平均值。(该附件非常适合!)
推荐阅读
- java - 如何在 Java 中将占位符添加到随机 Int 然后从该 Int 中提取一个数字?
- javascript - 是否有状态码告诉浏览器硬刷新当前页面?一些3xx?
- proxy - 如何使代理服务器的客户端保持活动状态?
- ios - 我怎样才能推动这两种观点?间隔()不起作用
- git - GitHub 上的 phpMyAdmin 存储库是开发(已卸载)版本。因此,许多文件甚至都不存在。我们如何提交修复那些丢失的文件?
- python - 处理 2 个 Django 表单 - 将数据从一个传递到另一个
- c# - 在 Azure Pipeline 中为 .net 框架控制台应用程序发布单个/组合工件
- mysql - 使用 HammerDB 对 MySQL 进行基准测试
- python - 如何使用伪造的登录凭据在 Python 测试用例中修补 ftplib.FTP?
- delphi - 无法从 SQL 表中查询字符串