線形回帰(日記)


```Python
# -*- coding: utf-8 -*-
# Program Name: complete_beauty_data_analysis_suite.py
# Description: Comprehensive analysis suite for beauty product data including simulation, regression, statistics, hypothesis testing, model comparison, feature engineering, and export.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.svm import SVR
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import cross_val_score, GridSearchCV, KFold
from sklearn.metrics import r2_score
import xgboost as xgb

# --- 1. データ生成 / Data Simulation ---
np.random.seed(42)
X = np.linspace(1000, 10000, 50).reshape(-1, 1)  # 値段 / Price (1000 to 10000 yen)
y_true = 0.003 * X.flatten() + 30               # 真の関係式 / True relationship
y = y_true + np.random.normal(0, 2, X.shape[0])  # ノイズを付加 / Add noise

# 平均・分散を表示 / Display mean and variance
print("Mean of Moisture:", np.mean(y))
print("Variance of Moisture:", np.var(y))

# --- 2. 線形回帰 / Linear Regression ---
model_lr = LinearRegression()
model_lr.fit(X, y)
y_pred = model_lr.predict(X)

# 結果表示 / Display results
print(f"\nLinear Regression Coefficient: {model_lr.coef_[0]}")
print(f"Linear Regression Intercept: {model_lr.intercept_}")
print(f"Linear Regression R2 Score: {r2_score(y, y_pred)}")

# グラフ表示 / Plot results
plt.scatter(X, y, label='Observed Data')
plt.plot(X, y_pred, color='red', label='Linear Fit')
plt.xlabel('Price (Yen)')
plt.ylabel('Moisture (%)')
plt.title('Price vs Moisture with Linear Fit')
plt.legend()
plt.show()

# --- 3. 統計要約 / Statistical Summary ---
summary = pd.Series(y).describe()
print("\n--- Statistical Summary ---")
print(summary)

# --- 4. 仮説検定 / Hypothesis Testing ---
t_stat, p_value = stats.ttest_1samp(y, 50)
print("\n--- Hypothesis Testing ---")
print(f"T-statistic: {t_stat:.4f}")
print(f"P-value: {p_value:.4f}")

# --- 5. 複数モデル比較 / Model Comparison ---
models = {
    'Ridge': Ridge(),
    'Lasso': Lasso(),
    'KNN': KNeighborsRegressor(),
    'RandomForest': RandomForestRegressor(),
    'GradientBoosting': GradientBoostingRegressor(),
    'SVR': SVR(),
    'XGBoost': xgb.XGBRegressor()
}

print("\n--- Model Comparison ---")
for name, model in models.items():
    model.fit(X, y)
    score = r2_score(y, model.predict(X))
    print(f"{name}: R2 Score = {score:.4f}")

# --- 6. クロスバリデーション / Cross-Validation ---
kf = KFold(n_splits=5, shuffle=True, random_state=42)
print("\n--- 5-Fold Cross-Validation ---")
for name, model in models.items():
    cv_scores = cross_val_score(model, X, y, cv=kf, scoring='r2')
    print(f"{name}: Mean R2 Score = {np.mean(cv_scores):.4f}")

# --- 7. 特徴量拡張 / Feature Expansion ---
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)
print("\n--- Expanded Feature Names ---")
print(poly.get_feature_names_out(['Price']))

# --- 8. ハイパーパラメータチューニング / Hyperparameter Tuning ---
param_grid = {'alpha': np.logspace(-3, 3, 7)}
grid = GridSearchCV(Ridge(), param_grid, cv=5, scoring='r2')
grid.fit(X, y)
print("\n--- Ridge Hyperparameter Tuning ---")
print(f"Best alpha: {grid.best_params_['alpha']}")
print(f"Best R2 Score: {grid.best_score_:.4f}")

# --- 9. クロスバリデーション（10-Fold） / Cross-Validation (10-Fold) ---
kf_10 = KFold(n_splits=10, shuffle=True, random_state=42)
print("\n--- 10-Fold Cross-Validation ---")
for name, model in models.items():
    cv_scores = cross_val_score(model, X, y, cv=kf_10, scoring='r2')
    print(f"{name}: Mean R2 Score = {np.mean(cv_scores):.4f}")


```