やっていること一覧(処理単位・目的・理論説明)
No. | 処理内容 | 数理・理論背景 | 目的・出力 | 可視化 |
---|---|---|---|---|
1 | 反比例回帰 | y = k / x + b の形式 逆数特徴量を使った線形回帰 |
反比例関係(例: 密度と浮力)を回帰モデルで再現 | 散布図+フィット線 |
2 | 比例回帰 | y = a * x + b の形式 一次関数モデル |
比例関係(例: 気温と成長速度)を回帰モデルで再現 | 散布図+フィット線 |
3 | 連立方程式解法 | 2x + y = 50, x - y = 10 行列方程式 Ax = b の解 |
交点(例: 酸素と二酸化炭素の収支点)を求める | 2直線と交点プロット |
4 | 二次関数回帰 | y = ax² + bx + c 放物運動などの二次現象の近似 |
放物運動(例: 投げた石の軌道)のモデル化 | 散布図+二次フィット線 |
5 | 二次方程式の解 | ax² + bx + c = 0 の解 判別式 D = b² - 4ac から求解 |
屈折率モデルなどの実数解確認と計算 | なし(数値出力) |
6 | サイン波時系列 | y = sin(ωt) + ノイズ 周期変動信号の生成 |
波形データ(例: 振動や信号)を再現 | 波形プロット |
7 | dB回帰(ログ特徴量) | dB = 20 * log10(Amplitude) 対数スケールで線形回帰 |
振幅とdBの関係モデル化 | 散布図+フィット線 |
# -*- coding: utf-8 -*-
# Program Name: natural_phenomena_regression_suite.py
# Creation Date: 20250111
# Overview: A comprehensive suite for analyzing inverse proportional, proportional, quadratic, simultaneous equations, sine wave, and dB regression models.
# Usage: To run the program, use the command `python natural_phenomena_regression_suite.py` in the terminal
# --- Install required libraries / 必要なライブラリをインストール ---
# Execute the following in your terminal or notebook if not already installed:
# !pip install numpy matplotlib scikit-learn
# --- Import libraries / ライブラリのインポート ---
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from numpy.linalg import solve
# -----------------------------
# Configuration / 設定値の一括管理
np.random.seed(42)
# Inverse Proportional Model Parameters / 反比例モデルパラメータ
x_min, x_max, x_points = 1, 10, 50
inv_k, inv_b, inv_noise_scale = 5, 2, 0.2
# Proportional Model Parameters / 比例モデルパラメータ
temp_min, temp_max, temp_points = 10, 35, 50
growth_slope, growth_noise_scale = 0.8, 1
# Simultaneous Equations Parameters / 連立方程式パラメータ
coeff_matrix = np.array([[2, 1], [1, -1]])
const_vector = np.array([50, 10])
# Quadratic Model Parameters / 二次モデルパラメータ
time_min, time_max, time_points = 0, 5, 50
quad_a, quad_b, quad_noise_scale = -4.9, 20, 1
# Quadratic Formula Parameters / 二次方程式の係数
a_quad, b_quad, c_quad = 1.33, -2.5, 1.2
# Sine Wave Parameters / サイン波パラメータ
sine_freq, sine_points = 2, 100
sine_noise_scale = 0.1
# dB Regression Parameters / dB回帰パラメータ
amp_min, amp_max, amp_points = 0.1, 10, 50
db_noise_scale = 0.5
# -----------------------------
# 1. Inverse Proportional Regression / 反比例回帰
x_range_inv = np.linspace(x_min, x_max, x_points).reshape(-1, 1)
y_inv = inv_k / x_range_inv.flatten() + inv_b + np.random.normal(0, inv_noise_scale, x_points)
model_inv = LinearRegression().fit(1 / x_range_inv, y_inv)
plt.scatter(x_range_inv, y_inv, label="Observed Data")
plt.plot(x_range_inv, model_inv.predict(1 / x_range_inv), color="red", label="Inverse Fit")
plt.title("Inverse Proportional Regression (y = k / x + b)")
plt.legend()
plt.show()
# -----------------------------
# 2. Proportional Regression / 比例回帰
temperature_range = np.linspace(temp_min, temp_max, temp_points).reshape(-1, 1)
growth_rate = growth_slope * temperature_range.flatten() + np.random.normal(0, growth_noise_scale, temp_points)
model_prop = LinearRegression().fit(temperature_range, growth_rate)
plt.scatter(temperature_range, growth_rate, label="Observed Data")
plt.plot(temperature_range, model_prop.predict(temperature_range), color="green", label="Proportional Fit")
plt.title("Proportional Regression: Temperature vs Growth")
plt.legend()
plt.show()
# -----------------------------
# 3. Simultaneous Equations Plot / 連立方程式プロット
solution = solve(coeff_matrix, const_vector)
x_vals = np.linspace(0, 50, 100)
y1 = 50 - 2 * x_vals
y2 = x_vals - 10
plt.plot(x_vals, y1, label="2x + y = 50")
plt.plot(x_vals, y2, label="x - y = 10")
plt.scatter(solution[0], solution[1], color="red", label=f"Solution (x={solution[0]:.2f}, y={solution[1]:.2f})")
plt.title("Simultaneous Equations Solution")
plt.legend()
plt.show()
# -----------------------------
# 4. Quadratic Regression / 二次回帰
time_range = np.linspace(time_min, time_max, time_points)
height = quad_a * time_range**2 + quad_b * time_range + np.random.normal(0, quad_noise_scale, time_points)
X_quad = np.column_stack([time_range**2, time_range, np.ones_like(time_range)])
model_quad = LinearRegression(fit_intercept=False).fit(X_quad, height)
plt.scatter(time_range, height, label="Observed Data")
plt.plot(time_range, model_quad.predict(X_quad), color="purple", label="Quadratic Fit")
plt.title("Quadratic Regression: Projectile Motion")
plt.legend()
plt.show()
# -----------------------------
# 5. Quadratic Formula / 二次方程式の解
D = b_quad**2 - 4 * a_quad * c_quad
print("\n--- Quadratic Formula Roots ---")
if D >= 0:
root1 = (-b_quad + np.sqrt(D)) / (2 * a_quad)
root2 = (-b_quad - np.sqrt(D)) / (2 * a_quad)
print(f"Roots: {root1:.4f}, {root2:.4f}")
else:
print("No real roots (Discriminant < 0)")
# -----------------------------
# 6. Sine Wave Time Series Plot / サイン波プロット
t = np.linspace(0, 2 * np.pi, sine_points)
sine_wave = np.sin(sine_freq * t) + np.random.normal(0, sine_noise_scale, sine_points)
plt.plot(t, sine_wave)
plt.title("Sine Wave Time Series with Noise")
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.show()
# -----------------------------
# 7. dB Calculation Regression (Log Feature) / dB回帰分析(ログ特徴量)
amplitude = np.linspace(amp_min, amp_max, amp_points)
log_amplitude = np.log10(amplitude).reshape(-1, 1)
db_values = 20 * log_amplitude.flatten() + np.random.normal(0, db_noise_scale, amp_points)
model_db_log = LinearRegression().fit(log_amplitude, db_values)
plt.scatter(amplitude, db_values, label="Observed dB")
plt.plot(amplitude, model_db_log.predict(log_amplitude), color="orange", label="Log Fit")
plt.xlabel("Amplitude")
plt.ylabel("dB")
plt.title("dB Calculation Regression (Log Feature)")
plt.legend()
plt.show()
print("\n--- dB Calculation Regression with Log Feature ---")
print(f"Fitted Coefficient (a): {model_db_log.coef_[0]:.4f}")
print(f"Fitted Intercept (b): {model_db_log.intercept_:.4f}")