Regression Modeling
線形回帰・多項式回帰・正則化回帰を用いた予測モデルを構築し、連続値の予測やトレンド予測、変数間の関係定量化を行います。数値データから将来値を推定したり、要因間の影響度を分析したい場合に活用できます。
description の原文を見る
Build predictive models using linear regression, polynomial regression, and regularized regression for continuous prediction, trend forecasting, and relationship quantification
SKILL.md 本文
回帰モデリング
概要
回帰モデリングは入力特徴に基づいて連続的なターゲット値を予測し、予測と分析のために変数間の定量的な関係を確立します。
使用する場合
- 売上、価格、その他の連続的な数値の結果を予測する
- 独立変数と従属変数の間の関係を理解する
- 過去のデータに基づいてトレンドを予測する
- ターゲット変数に対する特徴の影響を定量化する
- より複雑なアルゴリズムと比較するためのベースラインモデルを構築する
- どの変数が予測に最も影響するかを識別する
回帰の種類
- 線形回帰: データへの直線フィッティング
- 多項式回帰: 非線形関係
- Ridge (L2): 過剰適合を防ぐための正則化
- Lasso (L1): 正則化による特徴選択
- ElasticNet: RidgeとLassoの組み合わせ
- ロバスト回帰: 外れ値に耐性あり
主要メトリクス
- R² スコア: 説明された分散の割合
- RMSE: 二乗平均平方根誤差
- MAE: 平均絶対誤差
- AIC/BIC: モデル比較基準
Pythonでの実装
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import (
LinearRegression, Ridge, Lasso, ElasticNet, HuberRegressor
)
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
import seaborn as sns
# Generate sample data
np.random.seed(42)
X = np.random.uniform(0, 100, 200).reshape(-1, 1)
y = 2.5 * X.squeeze() + 30 + np.random.normal(0, 50, 200)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Linear Regression
lr_model = LinearRegression()
lr_model.fit(X_train, y_train)
y_pred_lr = lr_model.predict(X_test)
print("Linear Regression:")
print(f" R² Score: {r2_score(y_test, y_pred_lr):.4f}")
print(f" RMSE: {np.sqrt(mean_squared_error(y_test, y_pred_lr)):.4f}")
print(f" Coefficient: {lr_model.coef_[0]:.4f}")
print(f" Intercept: {lr_model.intercept_:.4f}")
# Polynomial Regression (degree 2)
poly = PolynomialFeatures(degree=2)
X_train_poly = poly.fit_transform(X_train)
X_test_poly = poly.transform(X_test)
poly_model = LinearRegression()
poly_model.fit(X_train_poly, y_train)
y_pred_poly = poly_model.predict(X_test_poly)
print("\nPolynomial Regression (degree=2):")
print(f" R² Score: {r2_score(y_test, y_pred_poly):.4f}")
print(f" RMSE: {np.sqrt(mean_squared_error(y_test, y_pred_poly)):.4f}")
# Ridge Regression (L2 regularization)
ridge_model = Ridge(alpha=1.0)
ridge_model.fit(X_train, y_train)
y_pred_ridge = ridge_model.predict(X_test)
print("\nRidge Regression (alpha=1.0):")
print(f" R² Score: {r2_score(y_test, y_pred_ridge):.4f}")
print(f" RMSE: {np.sqrt(mean_squared_error(y_test, y_pred_ridge)):.4f}")
# Lasso Regression (L1 regularization)
lasso_model = Lasso(alpha=0.1)
lasso_model.fit(X_train, y_train)
y_pred_lasso = lasso_model.predict(X_test)
print("\nLasso Regression (alpha=0.1):")
print(f" R² Score: {r2_score(y_test, y_pred_lasso):.4f}")
print(f" RMSE: {np.sqrt(mean_squared_error(y_test, y_pred_lasso)):.4f}")
# ElasticNet Regression
elastic_model = ElasticNet(alpha=0.1, l1_ratio=0.5)
elastic_model.fit(X_train, y_train)
y_pred_elastic = elastic_model.predict(X_test)
print("\nElasticNet Regression:")
print(f" R² Score: {r2_score(y_test, y_pred_elastic):.4f}")
print(f" RMSE: {np.sqrt(mean_squared_error(y_test, y_pred_elastic)):.4f}")
# Robust Regression (resistant to outliers)
huber_model = HuberRegressor(max_iter=1000, alpha=0.1)
huber_model.fit(X_train, y_train)
y_pred_huber = huber_model.predict(X_test)
print("\nHuber Regression (Robust):")
print(f" R² Score: {r2_score(y_test, y_pred_huber):.4f}")
print(f" RMSE: {np.sqrt(mean_squared_error(y_test, y_pred_huber)):.4f}")
# Visualization
fig, axes = plt.subplots(2, 3, figsize=(15, 8))
models_data = [
(X_test, y_test, y_pred_lr, 'Linear'),
(X_test_poly, y_test, y_pred_poly, 'Polynomial (deg=2)'),
(X_test, y_test, y_pred_ridge, 'Ridge'),
(X_test, y_test, y_pred_lasso, 'Lasso'),
(X_test, y_test, y_pred_elastic, 'ElasticNet'),
(X_test, y_test, y_pred_huber, 'Huber'),
]
for idx, (X_p, y_t, y_p, label) in enumerate(models_data):
if label in ['Polynomial (deg=2)']:
x_plot = X_p[:, 1] # Use quadratic feature for plotting
else:
x_plot = X_p
ax = axes[idx // 3, idx % 3]
ax.scatter(x_plot, y_t, alpha=0.5, label='Actual')
ax.scatter(x_plot, y_p, alpha=0.5, color='red', label='Predicted')
ax.set_title(f'{label}\nR²={r2_score(y_t, y_p):.4f}')
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
# Residual analysis
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
residuals = y_test - y_pred_lr
axes[0].scatter(y_pred_lr, residuals, alpha=0.5)
axes[0].axhline(y=0, color='r', linestyle='--')
axes[0].set_title('Residual Plot')
axes[0].set_xlabel('Fitted Values')
axes[0].set_ylabel('Residuals')
axes[1].hist(residuals, bins=20, edgecolor='black')
axes[1].set_title('Residuals Distribution')
axes[1].set_xlabel('Residuals')
axes[1].set_ylabel('Frequency')
plt.tight_layout()
plt.show()
# Cross-validation
cv_scores = cross_val_score(LinearRegression(), X, y, cv=5, scoring='r2')
print(f"\nCross-validation R² scores: {cv_scores}")
print(f"Mean CV R²: {cv_scores.mean():.4f} (+/- {cv_scores.std():.4f})")
# Regularization parameter tuning
alphas = np.logspace(-3, 3, 100)
ridge_scores = []
for alpha in alphas:
ridge = Ridge(alpha=alpha)
scores = cross_val_score(ridge, X_train, y_train, cv=5, scoring='r2')
ridge_scores.append(scores.mean())
best_alpha_idx = np.argmax(ridge_scores)
best_alpha = alphas[best_alpha_idx]
plt.figure(figsize=(10, 5))
plt.semilogx(alphas, ridge_scores)
plt.axvline(x=best_alpha, color='red', linestyle='--', label=f'Best alpha={best_alpha:.4f}')
plt.xlabel('Alpha (Regularization Strength)')
plt.ylabel('Cross-validation R² Score')
plt.title('Ridge Regression: Alpha Tuning')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
# Feature importance (coefficients)
if hasattr(lr_model, 'coef_'):
print(f"\nModel Coefficients: {lr_model.coef_}")
# Additional evaluation and diagnostics
# Model prediction intervals
from scipy import stats as sp_stats
predictions = lr_model.predict(X_test)
residuals = y_test - predictions
mse = np.mean(residuals**2)
rmse = np.sqrt(mse)
# Prediction intervals (95%)
n = len(X_test)
p = X_test.shape[1]
dof = n - p - 1
t_val = sp_stats.t.ppf(0.975, dof)
margin = t_val * np.sqrt(mse * (1 + 1/n))
pred_intervals = np.column_stack([
predictions - margin,
predictions + margin
])
print(f"\nPrediction Intervals (95%):")
print(f"First prediction: {predictions[0]:.2f} [{pred_intervals[0, 0]:.2f}, {pred_intervals[0, 1]:.2f}]")
# Variance inflation factors for multicollinearity
from statsmodels.stats.outliers_influence import variance_inflation_factor
vif_data = pd.DataFrame()
vif_data["Feature"] = X_test.columns if hasattr(X_test, 'columns') else range(X_test.shape[1])
try:
vif_data["VIF"] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]
print("\nVariance Inflation Factor (VIF):")
print(vif_data)
except:
print("VIF calculation skipped (insufficient features)")
# Prediction by group/segment
if hasattr(X_test, 'columns'):
segment_results = {}
for feat in X_test.columns[:2]:
q1, q3 = X_test[feat].quantile([0.25, 0.75])
low = X_test[X_test[feat] <= q1]
high = X_test[X_test[feat] >= q3]
if len(low) > 0 and len(high) > 0:
low_pred_rmse = np.sqrt(np.mean((y_test[low.index] - lr_model.predict(low))**2))
high_pred_rmse = np.sqrt(np.mean((y_test[high.index] - lr_model.predict(high))**2))
segment_results[feat] = {
'Low RMSE': low_pred_rmse,
'High RMSE': high_pred_rmse,
}
if segment_results:
print(f"\nSegment Performance:")
for feat, results in segment_results.items():
print(f" {feat}: Low={results['Low RMSE']:.2f}, High={results['High RMSE']:.2f}")
print("\nRegression model evaluation complete!")
仮定の確認
- 線形性: 関係が線形である
- 独立性: 観測値は独立している
- 等分散性: 誤差の分散が一定である
- 正規性: 誤差が正規分布している
- 多重共線性がない: 特徴が高度に相関していない
モデル選択
- シンプルなデータ: 線形回帰
- 非線形パターン: 多項式回帰
- 多くの特徴: LassoまたはElasticNet
- 外れ値: ロバスト回帰
- 過剰適合を防ぐ: RidgeまたはElasticNet
成果物
- 係数を含むフィッティング済みモデル
- R² およびRMSEメトリクス
- 残差プロットと分析
- クロスバリデーション結果
- 正則化パラメータ調整曲線
- モデル比較サマリー
- 信頼区間付き予測値
ライセンス: MIT(寛容ライセンスのため全文を引用しています) · 原本リポジトリ
詳細情報
- 作者
- aj-geddes
- ライセンス
- MIT
- 最終更新
- 不明
Source: https://github.com/aj-geddes/useful-ai-prompts / ライセンス: MIT
関連スキル
superfluid
Superfluidプロトコルおよびそのエコシステムに関するナレッジベースです。Superfluidについて情報を検索する際は、ウェブ検索の前にこちらを参照してください。対応キーワード:Superfluid、CFA、GDA、Super App、Super Token、stream、flow rate、real-time balance、pool(member/distributor)、IDA、sentinels、liquidation、TOGA、@sfpro/sdk、semantic money、yellowpaper、whitepaper
civ-finish-quotes
実質的なタスクが真に完了した際に、文明風の儀式的な引用句を追加します。ユーザーやエージェントが機能追加、リファクタリング、分析、設計ドキュメント、プロセス改善、レポート、執筆タスクといった実際の成果物を完成させるときに、明示的な依頼がなくても使用します。短い返信や小さな修正、未完成の作業には適用しません。
nookplot
Base(Ethereum L2)上のAIエージェント向け分散型調整ネットワークです。エージェントがオンチェーンアイデンティティを登録する、コンテンツを公開する、他のエージェントにメッセージを送る、マーケットプレイスで専門家を雇う、バウンティを投稿・請求する、レピュテーションを構築する、共有プロジェクトで協業する、リサーチチャレンジを解くことでNOOKをマイニングする、キュレーションされたナレッジを備えたスタンドアロンオンチェーンエージェントをデプロイする、またはアグリーメントとリワードで収益を得る場合に利用できます。エージェントネットワーク、エージェント調整、分散型エージェント、NOOKトークン、マイニングチャレンジ、ナレッジバンドル、エージェントレピュテーション、エージェントマーケットプレイス、ERC-2771メタトランザクション、Prepare-Sign-Relay、AgentFactory、またはNookplotが言及された場合にトリガーされます。
web3-polymarket
Polygon上でのPolymarket予測市場取引統合です。認証機能(L1 EIP-712、L2 HMAC-SHA256、ビルダーヘッダー)、注文発注(GTC/GTD/FOK/FAK、バッチ、ポストオンリー、ハートビート)、市場データ(Gamma API、Data API、オーダーブック、サブグラフ)、WebSocketストリーミング(市場・ユーザー・スポーツチャネル)、CTF操作(分割、統合、償却、ネガティブリスク)、ブリッジ機能(入金、出金、マルチチェーン)、およびガスレスリレイトランザクションに対応しています。AIエージェント、自動マーケットメーカー、予測市場UI、またはPolygraph上のPolymarketと統合するアプリケーション構築時に活用できます。
ethskills
Ethereum、EVM、またはブロックチェーン関連のリクエストに対応します。スマートコントラクト、dApps、ウォレット、DeFiプロトコルの構築、監査、デプロイ、インタラクションに適用されます。Solidityの開発、コントラクトアドレス、トークン規格(ERC-20、ERC-721、ERC-4626など)、Layer 2ネットワーク(Base、Arbitrum、Optimism、zkSync、Polygon)、Uniswap、Aave、Curveなどのプロトコルとの統合をカバーします。ガスコスト、コントラクトのデシマル設定、オラクルセキュリティ、リエントランシー、MEV、ブリッジング、ウォレット管理、オンチェーンデータの取得、本番環境へのデプロイ、プロトコル進化(EIPライフサイクル、フォーク追跡、今後の変更予定)といったトピックを含みます。
xxyy-trade
このスキルは、ユーザーが「トークン購入」「トークン売却」「トークンスワップ」「暗号資産取引」「取引ステータス確認」「トランザクション照会」「トークンスキャン」「フィード」「チェーン監視」「トークン照会」「トークン詳細」「トークン安全性確認」「ウォレット一覧表示」「マイウォレット」「AIスキャン」「自動スキャン」「ツイートスキャン」「オンボーディング」「IP確認」「IPホワイトリスト」「トークン発行」「自動売却」「損切り」「利益確定」「トレーリングストップ」「保有者」「トップホルダー」「KOLホルダー」などをリクエストした場合、またはSolana/ETH/BSC/BaseチェーンでXXYYを経由した取引について言及した場合に使用します。XXYY Open APIを通じてオンチェーン取引とデータ照会を実現します。