質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

1185閲覧

順序データを入力し、勝敗データに変換してからqNEIを求めるプログラムをかいているのですが、どのように順序データを入力していけばいいかわかりません。

KoyoKIMURA

総合スコア1

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/12/03 07:07

前提・実現したいこと

順序データを入力し、勝敗データに変換してからqNEIを求めるプログラムをかいているのですが、どのように順序データを入力していけばいいかわかりません。

該当のソースコード

python

1ソースコード 2import os 3 4import numpy as np 5import matplotlib.pyplot as plt 6from scipy.stats import zscore 7 8from botorch.models.pairwise_gp import PairwiseGP, PairwiseLaplaceMarginalLogLikelihood 9from botorch.fit import fit_gpytorch_model 10from botorch.acquisition import qNoisyExpectedImprovement 11from botorch.optim import optimize_acqf 12import torch 13import itertools 14from create_function import utility_3D, generate_train_x, generate_comparisons 15from normalization_max_min import norm_max_min 16 17IMAGE_DIR = "./accuracy/qNEI1_qNEI2/image_3D" 18if not os.path.exists(IMAGE_DIR): 19 # ディレクトリが存在しない場合、ディレクトリを作成する 20 os.makedirs(IMAGE_DIR) 21 22 23def observation_max_points(model, results): 24 observation_point = model.posterior(results).mean.tolist() 25 next_x_index = observation_point.index(max(observation_point)) 26 next_x = results[next_x_index] 27 28 return next_x 29 30 31def plot_3d(x, y, image_name): 32 plt.clf() 33 y_2D_12 = y.mean(axis=2) 34 plt.contourf(x, x, y_2D_12) 35 plt.savefig(image_name + "_12.png") 36 plt.clf() 37 y_2D_13 = y.mean(axis=1) 38 plt.contourf(x, x, y_2D_13) 39 plt.savefig(image_name + "_13.png") 40 plt.clf() 41 y_2D_23 = y.mean(axis=0) 42 plt.contourf(x, x, y_2D_23) 43 plt.savefig(image_name + "_23.png") 44 45 46def main(m, loop, noise, bounds, test_f_name, acq_session=10): 47 mse_list = [] 48 points = torch.linspace(0, 1, 11) 49 points_0, points_1, points_2 = torch.meshgrid(points, points, points) 50 points_mesh = torch.stack((points_0, points_1, points_2), dim=-1) 51 val_y = utility_3D(points_mesh).reshape(11, 11, 11).numpy() 52 53 # 検証用の関数(2次元だけ取り出してあとは平均値) 54 image_name = "./accuracy/qNEI1_qNEI2/image_3D/" + test_f_name + "/val_2D" 55 plot_3d(points, val_y, image_name=image_name) 56 57 for i in range(loop): 58 # 1回目の試行 59 train_X = generate_train_x(n=2, dim=3) 60 train_y = utility_3D(train_X) 61 train_comp = generate_comparisons(train_X[0], train_X[1]) 62 63 for j in range(m): 64 if j < m - (acq_session+1): 65 model = PairwiseGP(train_X, train_comp) 66 mll = PairwiseLaplaceMarginalLogLikelihood(model) 67 mll = fit_gpytorch_model(mll) 68 69 pred_y = norm_max_min(model.posterior(points_mesh).mean.squeeze().detach().numpy(), 70 max_=val_y.max(), min_=val_y.min()) 71 72 mse = np.square(val_y - pred_y).mean() 73 mse_list.append(mse) 74 print("MSE", i, j, ":", mse) 75 76 # 予測平均をplot(2次元だけ取り出してあとは平均値) 77 image_name = "./accuracy/qNEI1_qNEI2/image_3D/" + test_f_name + "/pred_y_mean_2D_" + str( 78 i + 1) + "_" + str(j + 1) 79 plot_3d(points, pred_y, image_name=image_name) 80 81 train_X_another = generate_train_x(n=2) 82 train_X = torch.cat((train_X, train_X_another), dim=0) 83 train_comp_another = generate_comparisons(train_X_another[0], train_X_another[1]) 84 train_comp_another = train_comp_another + (2 * j + 1) 85 train_comp = torch.cat((train_comp, train_comp_another), dim=0) 86 train_y = torch.cat((train_y, utility_3D(train_X_another)), dim=0) 87 image_name = "./accuracy/qNEI1_qNEI2/image_3D/" + test_f_name + "/pred_y_mean_2D_" + str( 88 i + 1) + "_" + str(j + 1) 89 plot_3d(points, pred_y, image_name=image_name) 90 91 else: 92 qNEI = qNoisyExpectedImprovement( 93 model=model, 94 X_baseline=train_X.float() 95 ) 96 97 next_X, acq_val = optimize_acqf( 98 acq_function=qNEI, 99 bounds=bounds, 100 q=2, 101 num_restarts=5, 102 raw_samples=256 103 ) 104 105 next_X = torch.cat((next_X[0].reshape(-1, 3), next_X[1].reshape(-1, 3)), dim=0).type(torch.float64) 106 train_X = torch.cat((train_X, next_X), dim=0) 107 108 if utility_3D(next_X[0]) > utility_3D(next_X[1]): 109 preference = torch.Tensor([len(train_X) - 2, len(train_X) - 1]).long().reshape(-1, 2) 110 train_comp = torch.cat((train_comp, preference), dim=0) 111 else: 112 preference = torch.Tensor([len(train_X) - 1, len(train_X) - 2]).long().reshape(-1, 2) 113 train_comp = torch.cat((train_comp, preference), dim=0) 114 115 model = PairwiseGP(train_X, train_comp) 116 mll = PairwiseLaplaceMarginalLogLikelihood(model) 117 mll = fit_gpytorch_model(mll) 118 pred_y = norm_max_min(model.posterior(points_mesh).mean.squeeze().detach().numpy() 119 , max_=val_y.max(), min_=val_y.min()) 120 121 mse = np.square(val_y - pred_y).mean() 122 mse_list.append(mse) 123 print("MSE", i, j, ":", mse) 124 125 # 予測平均をplot(2次元だけ取り出してあとは平均値) 126 image_name = "./accuracy/qNEI1_qNEI2/image_3D/" + test_f_name + "/pred_y_mean_2D_" + str( 127 i + 1) + "_" + str(j + 1) 128 plot_3d(points, pred_y, image_name=image_name) 129 130 number_of_comparisons = [] 131 for k in range(m): 132 number_of_comparisons.append(k) 133 134 mse_list = np.array(mse_list).reshape(loop, -1) 135 rmse_list = np.sqrt(mse_list) 136 rmse_list_mean = rmse_list.mean(axis=0) 137 rmse_list_sd = rmse_list.std(axis=0) 138 139 plt.clf() 140 plt.ylim(0, 1) 141 plt.plot(number_of_comparisons, rmse_list_mean) 142 plt.fill_between(number_of_comparisons, 143 rmse_list_mean - rmse_list_sd, 144 rmse_list_mean + rmse_list_sd, 145 alpha=0.5 146 ) 147 plt.savefig("./accuracy/qNEI1_qNEI2/image_3D/" + test_f_name + "/rmse_mean.pdf") 148

試したこと

if utility_3D(next_X[0]) > utility_3D(next_X[1]):
preference = torch.Tensor([len(train_X) - 2, len(train_X) - 1]).long().reshape(-1, 2)
train_comp = torch.cat((train_comp, preference), dim=0)
else:
preference = torch.Tensor([len(train_X) - 1, len(train_X) - 2]).long().reshape(-1, 2)
train_comp = torch.cat((train_comp, preference), dim=0)

おそらくこの部分をいじるのかと思うのですが...

補足情報(FW/ツールのバージョンなど)

import itertools
import torch
import numpy as np

num = np.array(["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"])
score = np.array([70, 90 ,80 ,50 ,30, 40, 55, 85, 95, 20, 10, 45, 35, 25, 100])
l = list(zip(num, score))

for i in itertools.combinations(l,2):
win = i[1][1] > i[0][1]
lose = not win
print(i[win][0], i[lose][0]) #勝ち 負け

このプログラムのように順序データを勝敗データに変換したいと考えています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問