0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pandaを使ってデータを整理する方法

0
Last updated at Posted at 2026-06-23

データの読み込み

以下のようにしてデータを読み込む

import pandas as pd

# 1. 各変数の値のリスト(元のdfの並び順の規則 [A -> B -> C] に合わせる)
# ※ C)が一番内側で先に増えるので、productの最後(一番右)にします
var_a_list = [100, 200, 300, 400]  # 4通り
var_b_list = [10, 11, 12, 13, 14]  # 5通り
var_c_list = [1, 2, 3, 4, 5]  # 5通り
# columを設定する方法
df.columns.name = "Var_D"
# 2. 100行分のマルチインデックスを作成して、既存の df に上書きする
df.index = pd.MultiIndex.from_product(
    [var_a_list, var_b_list, var_c_list], names=["Var_A", "Var_B", "Var_C"]
)

# 3. これでパターン1と同じように、名前でピンポイントに1行取り出せるようになります!
target_row = df.loc[(100, 10, 2)]

#4 グラフでの描画
fig, ax = plt.subplots(figsize=(10, 6))
plt.plot(df.columns, df.loc[(100, 10, 2)], marker='o', linestyle='-')
ax.set_title("Extracted Row Plot (Var_A=100, Var_B=10, Var_C=2)")
ax.set_xlabel("Var_D")  
ax.set_ylabel("Value")
plt.show()

補完する方法

import numpy as np
import pandas as pd
from scipy.interpolate import RegularGridInterpolator

# --- 前提条件(各変数の実際の値のグリッド) ---
a_grid = np.array([100, 200, 300, 400])  # Aの格子点 (4)
b_grid = np.array([10, 11, 12, 13, 14])  # Bの格子点 (5)
c_grid = np.array([1, 2, 3, 4, 5])  # Cの格子点 (5)
d_grid = np.linspace(0, 1, 31)  # Dの格子点 (31)

# 1. 渡された df (100行×31列) を、元の4次元配列 (4, 5, 5, 31) の形に変形する
# ※ valuesでNumPy配列にしてから reshape します
data_4d = df.values.reshape(len(a_grid), len(b_grid), len(c_grid), len(d_grid))
print("4次元配列の形状:", data_4d.shape)  # (4, 5, 5, 31) になるはずです

# 2. 4次元の線形補間関数を作成する
# (これで、C++の df → C → B → A の順の線形補間ロジックが自動構築されます)
df_interpolator = RegularGridInterpolator(
    (a_grid, b_grid, c_grid, d_grid), data_4d, method="linear"
)

# 3. 補間したい「未知のピンポイントの条件」を指定して値を計算する
# 例:A=150, B=11.5, C=2.3, D=0.15 のときの実数値を求める
target_point = [150, 11.5, 2.3, 0.15]
estimated_value = df_interpolator(target_point)

print(f"補間された値: {estimated_value[0]}")

固定した値でグラフ作成

import matplotlib.pyplot as plt
import numpy as np

# 1. グラフにしたい「固定する条件(A, B, C)」を決める
# ※格子点の間の「中途半端な値」でも問題なく補間できます!
fixed_A = 150  # 例:Aの格子点 (100と200の間)
fixed_B = 11.5  # 例:Bの格子点 (11と12の間)
fixed_C = 2.3  # 例:Cの格子点 (2と3の間)

# 2. グラフの横軸(D)用に、なめらかな「細かい100分割のデータ」を作る
d_smooth = np.linspace(0, 1, 100)

# 3. 補間関数に渡すための4次元の座標データ(100点分)を組み立てる
# [ [A, B, C, D1], [A, B, C, D2], ... ] という形の配列を作ります
plot_points = np.array(
    [[fixed_A, fixed_B, fixed_C, d] for d in d_smooth]
)

# 4. 100点分の補間計算を一気に実行!
interpolated_values = df_interpolator(plot_points)

# --- ここからグラフ描画 (matplotlib) ---
plt.figure(figsize=(8, 5))

# 補間後のなめらかなデータを青い「線」で描画
plt.plot(
    d_smooth,
    interpolated_values,
    label=f"Interpolated Line (A={fixed_A}, B={fixed_B}, C={fixed_C})",
    color="blue",
    linewidth=2,
)

# 【参考】もし比較用に、固定した条件に一番近い「元の生データ(31点)」も重ねてんてん(プロット)で表示したい場合
# ここでは例として、一番近い格子点 (A=100, B=11, C=2) のデータを引っ張ってきています
# nearest_row = df.loc[(100, 11, 2)].to_numpy()
# plt.scatter(
#     d_grid,
#     nearest_row,
#     color="red",
#     alpha=0.5,
#     label="Original Data (A=100, B=11, C=2)",
# )

# グラフの装飾
plt.title("Interpolation Result along D-axis", fontsize=14)
plt.xlabel("Var_D", fontsize=12)
plt.ylabel("Value", fontsize=12)
plt.grid(True, linestyle="--", alpha=0.6)  # グリッド線
plt.legend()  # 凡例を表示

# グラフを表示
plt.show()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?