FrontPage
B3 前期授業スケジュール
月曜日 火曜日 水曜日 木曜日 金曜日
1-2 研究会
3-4 卒論1 研究会
5-6 卒論1 ディジタル信号処理 卒論1 卒論1
7-8 技術者倫理
9-10 研究会 &size(px){Text you want to change};
11-12
&ref(): File not found: "ダッシュストーム.jpg" at page "中島";
&ref(): File not found: "ダッシュストーム.jpg" at page "中島";
&ref(): File not found: "ダッシュストーム.jpg" at page "中島";
&ref(): File not found: "ダッシュストーム.jpg" at page "中島";
&ref(): File not found: "ダッシュストーム.jpg" at page "中島";
&ref(): File not found: "ダッシュストーム.jpg" at page "中島";
&ref(): File not found: "栗松.jpg" at page "中島";
&ref(): File not found: "栗松.jpg" at page "中島";
&ref(): File not found: "栗松.jpg" at page "中島";
&ref(): File not found: "栗松.jpg" at page "中島";
&ref(): File not found: "栗松.jpg" at page "中島";
&ref(): File not found: "栗松.jpg" at page "中島";
メモ
やること
from sklearn.linear_model import ElasticNet
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import make_regression
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
# CSVファイルのパスを指定してください
file_path = "C:/Users/nt011/Desktop/研究/富山取引のほう富山市のみ/変数選択/変数作成後新.csv"
# CSVファイルの読み込み
df = pd.read_csv(file_path, encoding='cp932') # 文字コードが異なる場合は、'utf-8' を他のエンコーディングに変更してください
# 特徴量とターゲットの分割
X = df.drop(columns=['取引価格(㎡単価)']) # 取引価格(㎡単価)をyとして分離
y = df['取引価格(㎡単価)'] # ターゲット変数
# 🔥 標準化の実施
scaler_X = StandardScaler()
scaler_y = StandardScaler()
X_scaled = scaler_X.fit_transform(X) # Xの標準化
y_scaled = scaler_y.fit_transform(y.values.reshape(-1, 1)).ravel() # yの標準化
# データ分割
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled, test_size=0.2, random_state=42)
# 訓練データの一部を使用
X_sample, _, y_sample, _ = train_test_split(X_train, y_train, test_size=0.8, random_state=42)
# 2. パラメータグリッドの設定
param_grid = {
'alpha': np.logspace(-5, 0, 10), # alpha1の探索範囲 (正則化パラメータ)
'l1_ratio': np.linspace(0.0001, 1.0, 10) # l1_ratio1の探索範囲 (L1とL2の比率)
}
# パラメータ名を変更(alpha -> alpha1, l1_ratio -> l1_ratio1)
param_grid = {'alpha1': param_grid['alpha'], 'l1_ratio1': param_grid['l1_ratio']}
# 3. ElasticNetモデルの初期化
elastic_net = ElasticNet(max_iter=10000, random_state=42)
# 4. グリッドサーチCVの設定
grid_search = GridSearchCV(
estimator=elastic_net,
param_grid={'alpha': param_grid['alpha1'], 'l1_ratio': param_grid['l1_ratio1']}, # 変更した名前に対応
cv=5, # 5分割交差検証
scoring='neg_mean_squared_error', # 評価指標: 平均二乗誤差の負値
verbose=1,
n_jobs=-1 # 並列実行
)
# 5. グリッドサーチの実行
grid_search.fit(X_sample, y_sample)
# 6. 最適なパラメータとスコアの取得
best_params = grid_search.best_params_
best_params_renamed = {'alpha1': best_params['alpha'], 'l1_ratio1': best_params['l1_ratio']}
best_score = grid_search.best_score_
print("最適なパラメータ:")
print(best_params_renamed)
print("最良のスコア (平均二乗誤差の負値):")
print(best_score)