gpt4 book ai didi

python - pandas,正确处理行元素内的 numpy 数组

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:12 25 4
gpt4 key购买 nike

我将给出一个最小的示例,其中我将在 pandas.DataFrame 的行元素内创建 numpy 数组。

TL;DR:查看DataFrame的屏幕截图

此代码通过使用 scipy.optimize.brute 来查找某个函数的最小值,该函数返回最小值、找到最小值的变量以及对函数求值的两个 numpy 数组.

import numpy
import scipy.optimize
import itertools

sin = lambda r, phi, x: r * np.sin(phi * x)

def func(r, x):
x0, fval, grid, Jout = scipy.optimize.brute(
sin, ranges=[(-np.pi, np.pi)], args=(r, x), Ns=10, full_output=True)
return dict(phi_at_min=x0[0], result_min=fval, phis=grid, result_at_grid=Jout)


rs = numpy.linspace(-1, 1, 10)
xs = numpy.linspace(0, 1, 10)

vals = list(itertools.product(rs, xs))

result = [func(r, x) for r, x in vals]

# idk whether this is the best way of generating the DataFrame, but it works
df = pd.DataFrame(vals, columns=['r', 'x'])
df = pd.concat((pd.DataFrame(result), df), axis=1)
df.head()

dataframe

我希望这不是我应该这样做的方式,也许应该以某种方式扩展列表。我该如何以正确、美观、干净的方式处理这个问题?

最佳答案

所以,尽管“美丽和干净”有多种解释,但我会给你我的,这应该会给你一些想法。我正在利用多重索引,以便您稍后可以轻松地为评估网格中的每个点选择 phi/result_at_grid 对。我还使用 apply 而不是创建两个数据帧。

import numpy
import scipy.optimize
import itertools

sin = lambda r, phi, x: r * np.sin(phi * x)

def func(row):
"""
Accepts a row of a dataframe (a pd.Series).
df.apply(func, axis=1)
returns a pd.Series with the initial (r,x) and the results
"""
r = row['r']
x = row['x']
x0, fval, grid, Jout = scipy.optimize.brute(
sin, ranges=[(-np.pi, np.pi)], args=(r, x), Ns=10, full_output=True)

# Create a multi index series for the phis
phis = pd.Series(grid)
phis.index = pd.MultiIndex.from_product([['Phis'], phis.index])

# same for result at grid
result_at_grid = pd.Series(Jout)
result_at_grid.index = pd.MultiIndex.from_product([['result_at_grid'], result_at_grid.index])

# concat
s = pd.concat([phis, result_at_grid])

# Add these two float results
s['phi_at_min'] = x0[0]
s['result_min'] = fval

# add the initial r,x to reconstruct the index later
s['r'] = r
s['x'] = x

return s



rs = numpy.linspace(-1, 1, 10)
xs = numpy.linspace(0, 1, 10)

vals = list(itertools.product(rs, xs))
df = pd.DataFrame(vals, columns=['r', 'x'])

# Apply func to each row (axis=1)
results = df.apply(func, axis=1)
results.set_index(['r','x'], inplace=True)
results.head().T # Transposing so we can see the output in one go...

enter image description here

现在您可以选择评估网格点 2 处的所有值,例如

print(results.swaplevel(0,1, axis=1)[2].head()) # Showing only 5 first


Phis result_at_grid
r x
-1.0 0.000000 -1.745329 0.000000
0.111111 -1.745329 0.193527
0.222222 -1.745329 0.384667
0.333333 -1.745329 0.571062
0.444444 -1.745329 0.750415

关于python - pandas,正确处理行元素内的 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40508438/

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