2016-11-09 10 views
1

pandas.DataFrameの行要素内にnumpy配列を作成する最小の例を示します。pandasは、行要素内の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

は、私は、これは私がこれを行うことになっていますし、多分何とかリストを展開する方法ではないことを期待しています。これを正しく、美しく、きれいに処理するにはどうすればよいですか?

+2

ない質問は、現在の形で応答可能であることを確認してください例えば評価格子点2ですべての値を選択することができます - あなたは出力が期待する何をすべきかのように見える? "美しくてきれいな"は、見る人の目の中にとてもあることがあります... –

答えて

1

「美しく清潔」は解釈の対象となりますが、私はあなたに私のことを教えてくれるでしょう。私はマルチインデックスを利用していますので、後で評価グリッドの各ポイントに対してphi/result_at_gridのペアを簡単に選択することができます。 2つのデータフレームを作成する代わりに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

今、あなたは

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 
+1

ありがとう、このことは間違いなくこの問題に対処する方法のインスピレーションを与えてくれます! :) – johnbaltis

関連する問題