7.2.2 データの相互関連付け

corrメソッドを使用して、oml.DataFrameオブジェクト内で可能であれば列を横断してピアソン、スピアマンまたはケンドール相関分析を実行します。

この関数の引数の詳細は、help(oml.DataFrame.corr)を呼び出すか、Oracle Machine Learning for Python APIリファレンスを参照してください。

例7-9 基本的な相関計算の実行

この例では、最初にpandas.DataFrameオブジェクトdfから、一時データベース表およびそれに対応するプロキシoml.DataFrameオブジェクトoml_df1を作成します。次に、列AとBの間で計算された相関を検証します。予期したとおりに1が返されます。Bの値は、要素単位でAの値の2倍です。この例では、さらにdfの値フィールドを変更し、NaNエントリを作成します。次に、一時データベース表および対応するプロキシoml.DataFrameオブジェクトoml_df2を作成します。最後に、skipnaTrue (デフォルト)に設定し、次にFalseに設定してoml_df2corrメソッドを呼び出して結果を比較します。

import oml
import pandas as pd

df = pd.DataFrame({'A': range(4), 'B': [2*i for i in range(4)]})
oml_df1 = oml.push(df)

# Verify that the correlation between column A and B is 1.
oml_df1.corr()

# Change a value to test the change in the computed correlation result.
df.loc[2, 'A'] = 1.5

# Change an entry to NaN (not a number) to test the 'skipna'
# parameter in the corr method.
df.loc[1, 'B'] = None

# Push df to the database using the floating point column type 
# because NaNs cannot be used in Oracle numbers.
oml_df2 = oml.push(df, oranumber=False)

# By default, 'skipna' is True.
oml_df2.corr()
oml_df2.corr(skipna=False)

この例のリスト

>>> import oml
>>> import pandas as pd
>>>
>>> df = pd.DataFrame({'A': range(4), 'B': [2*i for i in range(4)]})
>>> oml_df1 = oml.push(df)
>>>
>>> # Verify that the correlation between column A and B is 1.
... oml_df1.corr()
   A  B
A  1  1
B  1  1
>>>
>>> # Change a value to test the change in the computed correlation result.
... df.loc[2, 'A'] = 1.5
>>>
>>> # Change an entry to NaN (not a number) so to test the 'skipna'
... # parameter in the corr method.
... df.loc[1, 'B'] = None
>>>
>>> # Push df to the database using the floating point column type 
... # because NaNs cannot be used in Oracle numbers.
... oml_df2 = oml.push(df, oranumber=False)
>>>
>>> # By default, 'skipna' is True.
... oml_df2.corr()
          A         B
A  1.000000  0.981981
B  0.981981  1.000000
>>> oml_df2.corr(skipna=False)
     A    B
A  1.0  NaN
B  NaN  1.0