csvファイルで系列x1~x5とyを読み込む。系列x1~x5から任意の系列をピックアップし、それら系列の合計から新系列を作成する。その新系列と系列yの相関係数が最大となるような組み合わせを求めたい。
import numpy as np
import pandas as pd
import cvxpy
df = pd.read_csv('teratail.csv')
df_x = df.drop('y', axis = 1)
df_y = df['y']
def correlation_calc(x, df_x, df_y):
cnt_rows = len(df_x)
goukei = np.zeros(len(df_x))
for i in range(cnt_rows): goukei[i] = np.dot(x, df_x.iloc[i,:]) corr_coef = np.corrcoef(goukei,df_y) result = corr_coef[0,1] return result
x = cvxpy.Variable(len(df_x), boolean = True)
objective = cvxpy.Maximize(correlation_calc(x, df_x, df_y))
prob = cvxpy.Problem(objective)
prob.solve(solver=cvxpy.ECOS_BB)
result = [round(ix,0) for ix in x.value]
エラーメッセージは下記の通りです。
TypeError Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-4-df78a7bc155b> in <module>
1 x = cvxpy.Variable(len(df_x), boolean = True)
----> 2 objective = cvxpy.Maximize(correlation_calc(x, df_x, df_y))
3
4 prob = cvxpy.Problem(objective)
5 prob.solve(solver=cvxpy.ECOS_BB)
<ipython-input-3-a06e9252fe40> in correlation_calc(x, df_x, df_y)
4
5 for i in range(cnt_rows):
----> 6 goukei[i] = np.dot(x, df_x.iloc[i,:])
7
8 # 相関係数を求める
ValueError: setting an array element with a sequence.
あなたの回答
tips
プレビュー