下記のコードを数百倍オーダーで高速化してください。列数は1000程度あります。 target = train["label"] numericcols =…
- User
下記のコードを数百倍オーダーで高速化してください。列数は1000程度あります。 target = train["label"] numeric_cols = train.select_dtypes(include=[np.number]).columns
結果を保存する辞書
correlations = {}
四則演算の関数を定義
operations = { 'add': lambda x, y: x + y, 'sub': lambda x, y: x - y, 'mul': lambda x, y: x * y, 'div': lambda x, y: x / (y + 1e-10) # 除算の際の0除算を避ける }
相関係数の計算
for op_name, op in operations.items(): print(op_name) for col1 in tqdm.tqdm(numeric_cols): for col2 in numeric_cols: if col1 == col2 and op_name in ['sub', 'div']: # 自身との減算や除算はスキップ continue new_col = f"{col1}{col2}{op_name}" train[new_col] = op(train[col1], train[col2]) corr = train[new_col].corr(target) correlations[new_col] = corr train = train.drop(columns=[new_col]) # 一時的に追加した列を削除
結果をDataFrameに変換
corr_df = pd.DataFrame(list(correlations.items()), columns=['Feature', 'Correlation'])
相関係数の絶対値でソート
corr_df = corr_df.reindex(corr_df['Correlation'].abs().sort_values(ascending=False).index)
print(corr_df)
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.