Created
August 24, 2021 01:36
-
-
Save chizuchizu/ed3cb4b958f05f29654235eac4c7498c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from amplify import IsingPoly, IsingMatrix, gen_symbols, sum_poly, BinaryPoly | |
from amplify import Solver, decode_solution | |
from amplify.client import FixstarsClient | |
import numpy as np | |
import pandas as pd | |
import scipy.sparse as sps | |
from itertools import combinations | |
from tqdm import tqdm | |
from eals import ElementwiseAlternatingLeastSquares | |
from sklearn.metrics import roc_auc_score, accuracy_score | |
import matplotlib.pyplot as plt | |
df = pd.read_csv("sample.csv").iloc[:, :] | |
# 上から1000行使う | |
train_df = df.iloc[:1000, :] | |
# 縦持ち | |
long_df = pd.melt(train_df, id_vars=["id"], value_vars=train_df.iloc[:, 1:].columns) | |
# 数値に変換 | |
long_df["id"] = long_df["id"].apply(lambda x: x[2:]).astype(int) | |
long_df["variable"] = long_df["variable"].apply(lambda x: x[1:]).astype(int) | |
matrix = sps.csr_matrix( | |
(long_df["value"], (long_df["id"], long_df["variable"])), | |
) | |
model = ElementwiseAlternatingLeastSquares() | |
model.fit(matrix, show_loss=True) | |
result = np.zeros( | |
# train_dfはid列があるので-1する | |
(1000, train_df.shape[1] - 1) | |
) | |
# predict | |
for i in range(1000): | |
user_vector = model.user_factors[i] | |
pred_ratings = model.item_factors @ user_vector | |
result[i, :] = pred_ratings | |
score = pd.DataFrame(columns=train_df.iloc[:, 1:].columns) | |
for i in range(train_df.shape[1] - 1): | |
# train_dfはid列があるのでi + 1する | |
true = train_df.iloc[:, i + 1] | |
pred = result[:, i] | |
print(f"\nx{i}") | |
try: | |
# 正例が無いとエラーになるから飛ばす | |
print(roc_auc_score(true, pred)) | |
score.loc["train_auc", f"x{i}"] = roc_auc_score(true, pred) | |
except: | |
continue | |
score.to_csv("eals_result.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment