機械学習関数

```
import numpy as np
import matplotlib.pyplot as plt

# インパルス関数の近似
n_impulse = np.linspace(-1, 1, 1000)
sigma = 0.0001  # 狭い幅を設定
impulse_approx = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-n_impulse**2 / (2 * sigma**2))

# ステップ関数の近似
n_step = np.linspace(-10, 10, 1000)
steepness = 10000  # ステップの急さを調整
step_approx = 1 / (1 + np.exp(-steepness * n_step))

# プロット設定
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))

# インパルス関数のプロット
ax1.plot(n_impulse, impulse_approx)
ax1.set_title('Approximate Impulse Function (Gaussian)')
ax1.set_xlabel('Time')
ax1.set_ylabel('Amplitude')
ax1.grid(True)

# ステップ関数のプロット
ax2.plot(n_step, step_approx)
ax2.set_title('Approximate Step Function (Sigmoid)')
ax2.set_xlabel('Time')
ax2.set_ylabel('Amplitude')
ax2.grid(True)

# プロットを表示
plt.tight_layout()
plt.show()
```
```
import numpy as np
import matplotlib.pyplot as plt

# 時間範囲の設定
n = np.linspace(-10, 10, 1000)

# ランプ関数の近似 (シグモイド関数を使用)
steepness = 10.0  # ステップの急さを調整
slope = 1.0       # ランプ関数の傾きを調整

# シグモイド関数によるランプ関数の近似
ramp_approx = slope * n / (1 + np.exp(-steepness * n))

# プロット
plt.plot(n, ramp_approx, label=r'$ramp\_approx = \frac{{slope \cdot n}}{{1 + \exp(-steepness \cdot n)}}$')
plt.title('Approximate Ramp Function (Sigmoid)')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.grid(True)
plt.legend()  # ラベルを表示
plt.show()
```

```

import numpy as np
import matplotlib.pyplot as plt

# 活性化関数の定義
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def relu(x):
    return np.maximum(0, x)

def tanh(x):
    return np.tanh(x)

# 損失関数の定義
def mean_squared_error(y_true, y_pred):
    return np.mean((y_true - y_pred) ** 2)

# 入力データの範囲設定
x = np.linspace(-10, 10, 100)

# 各活性化関数の出力
sigmoid_output = sigmoid(x)
relu_output = relu(x)
tanh_output = tanh(x)

# 期待値 (y_true) としてすべて1を設定
y_true = np.ones_like(x)

# 各活性化関数の出力に対するMSEの計算
sigmoid_loss = mean_squared_error(y_true, sigmoid_output)
relu_loss = mean_squared_error(y_true, relu_output)
tanh_loss = mean_squared_error(y_true, tanh_output)

# プロット
plt.figure(figsize=(12, 8))

# シグモイド関数
plt.subplot(3, 1, 1)
plt.plot(x, sigmoid_output, label=f'Sigmoid (MSE: {sigmoid_loss:.3f})')
plt.title('Sigmoid Function')
plt.xlabel('Input')
plt.ylabel('Output')
plt.legend()
plt.grid(True)

# ReLU関数
plt.subplot(3, 1, 2)
plt.plot(x, relu_output, label=f'ReLU (MSE: {relu_loss:.3f})')
plt.title('ReLU Function')
plt.xlabel('Input')
plt.ylabel('Output')
plt.legend()
plt.grid(True)

# タング関数
plt.subplot(3, 1, 3)
plt.plot(x, tanh_output, label=f'Tanh (MSE: {tanh_loss:.3f})')
plt.title('Tanh Function')
plt.xlabel('Input')
plt.ylabel('Output')
plt.legend()
plt.grid(True)

plt.tight_layout()
plt.show()

```

```
import numpy as np
import matplotlib.pyplot as plt

# 損失関数
def mean_squared_error(y_true, y_pred):
    return np.mean((y_true - y_pred) ** 2)

def cross_entropy(y_true, y_pred):
    return -np.sum(y_true * np.log(y_pred + 1e-15)) / y_true.shape[0]

# 評価関数
def accuracy(y_true, y_pred):
    return np.mean(y_true == y_pred)

def precision(y_true, y_pred):
    true_positive = np.sum((y_true == 1) & (y_pred == 1))
    predicted_positive = np.sum(y_pred == 1)
    return true_positive / predicted_positive if predicted_positive != 0 else 0

def recall(y_true, y_pred):
    true_positive = np.sum((y_true == 1) & (y_pred == 1))
    actual_positive = np.sum(y_true == 1)
    return true_positive / actual_positive if actual_positive != 0 else 0

# データ前処理
def standardize(data):
    mean = np.mean(data, axis=0)
    std_dev = np.std(data, axis=0)
    return (data - mean) / std_dev

def min_max_scale(data):
    min_val = np.min(data, axis=0)
    max_val = np.max(data, axis=0)
    return (data - min_val) / (max_val - min_val)

# 確率関数
def softmax(x):
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum(axis=0)

# サンプルデータを生成
y_true = np.array([0, 1, 1, 0, 1])
y_pred = np.array([0, 1, 0, 0, 1])

# 各関数の計算を実行
mse = mean_squared_error(y_true, y_pred)
ce = cross_entropy(y_true, y_pred)
acc = accuracy(y_true, y_pred)
prec = precision(y_true, y_pred)
rec = recall(y_true, y_pred)

# 結果をプロット
metrics = ['MSE', 'Cross Entropy', 'Accuracy', 'Precision', 'Recall']
values = [mse, ce, acc, prec, rec]

plt.figure(figsize=(10, 6))
plt.bar(metrics, values, color='skyblue')
plt.ylabel('Metric Values')
plt.title('Evaluation Metrics')
plt.show()


```

```
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# サンプルの損失関数 (二次関数)
def loss_function(x, y):
    return x**2 + y**2

# 勾配を計算
def grad_function(params):
    x, y = params
    return np.array([2 * x, 2 * y])

# 勾配降下法
def gradient_descent(X, y, lr=0.1, epochs=100):
    params = np.array([X, y])
    history = [params]
    
    for _ in range(epochs):
        params -= lr * grad_function(params)
        history.append(params)
    
    return np.array(history)

# Adamオプティマイザー
def adam_optimizer(init_params, learning_rate=0.1, beta1=0.9, beta2=0.999, epsilon=1e-8, epochs=100):
    params = init_params
    m = np.zeros_like(params)
    v = np.zeros_like(params)
    t = 0
    history = [params]
    
    for _ in range(epochs):
        t += 1
        grads = grad_function(params)
        m = beta1 * m + (1 - beta1) * grads
        v = beta2 * v + (1 - beta2) * (grads ** 2)
        
        m_hat = m / (1 - beta1 ** t)
        v_hat = v / (1 - beta2 ** t)
        
        params -= learning_rate * m_hat / (np.sqrt(v_hat) + epsilon)
        history.append(params)
    
    return np.array(history)

# 初期パラメータ
init_params = np.array([3.0, 4.0])

# 勾配降下法とAdamオプティマイザーの実行
gd_history = gradient_descent(init_params[0], init_params[1])
adam_history = adam_optimizer(init_params)

# 三次元プロット
fig = plt.figure(figsize=(14, 8))
ax = fig.add_subplot(111, projection='3d')

# グリッドの生成
X = np.linspace(-5, 5, 100)
Y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(X, Y)
Z = loss_function(X, Y)

# 損失関数の面のプロット
ax.plot_surface(X, Y, Z, alpha=0.6, rstride=100, cstride=100, cmap='viridis')

# 勾配降下法の軌跡
ax.plot(gd_history[:, 0], gd_history[:, 1], loss_function(gd_history[:, 0], gd_history[:, 1]), 'ro-', label="Gradient Descent")

# Adamオプティマイザーの軌跡
ax.plot(adam_history[:, 0], adam_history[:, 1], loss_function(adam_history[:, 0], adam_history[:, 1]), 'bo-', label="Adam Optimizer")

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Loss')
ax.set_title('Optimization Paths: Gradient Descent vs Adam Optimizer')
ax.legend()

plt.show()
```


```

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from matplotlib.colors import ListedColormap

# データセットの作成
X, y = datasets.make_circles(n_samples=300, noise=0.2, factor=0.5, random_state=42)

# データのスケーリング
scaler = StandardScaler()
X = scaler.fit_transform(X)

# SVMモデルの訓練
model = SVC(kernel='rbf', gamma=1, C=1)
model.fit(X, y)

# 決定境界のプロット
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01), np.arange(y_min, y_max, 0.01))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.figure(figsize=(10, 6))
plt.contourf(xx, yy, Z, alpha=0.8, cmap=ListedColormap(['#FFAAAA', '#AAAAFF']))
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolor='k', cmap=ListedColormap(['#FF0000', '#0000FF']))
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('SVM Decision Boundary with Circular Data')
plt.show()

```


```

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs

# データセットの作成
X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)

# K-meansクラスタリングの訓練
kmeans = KMeans(n_clusters=4)
kmeans.fit(X)
y_kmeans = kmeans.predict(X)

# クラスタの中心
centers = kmeans.cluster_centers_

plt.figure(figsize=(10, 6))
plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')
plt.scatter(centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75, marker='x', label='Centroids')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('K-Means Clustering')
plt.legend()
plt.show()

```

```
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from mpl_toolkits.mplot3d import Axes3D
from sklearn.neural_network import MLPRegressor

# 目的関数
def objective_function(X):
    x, y, z = X
    return x**2 + y**2 + z**2

# 制約条件
def constraint(X):
    x, y, z = X
    return x + y + z - 1

# 初期値
initial_guess = [0.5, 0.5, 0.5]

# 制約条件の定義
constraints = {'type': 'eq', 'fun': constraint}

# 最適化の実行
result = minimize(objective_function, initial_guess, constraints=constraints)

# 結果の出力
print("Optimal solution:", result.x)
print("Objective function value at optimal solution:", result.fun)

# 三次元プロット
x = np.linspace(-2, 2, 50)
y = np.linspace(-2, 2, 50)
X, Y = np.meshgrid(x, y)
Z = 1 - X - Y  # 制約条件から求めたz

# 目的関数の計算
Z_obj = X**2 + Y**2 + Z**2

# プロットの作成
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z_obj, cmap='viridis', alpha=0.8)

# 最適解のプロット
opt_x, opt_y, opt_z = result.x
opt_obj = objective_function(result.x)
ax.scatter(opt_x, opt_y, opt_obj, color='red', s=100, label='Optimal Solution')

# 機械学習モデルによる予測
# 学習データの生成（例としてランダムに生成）
X_train = np.random.rand(100, 3)
y_train = np.array([objective_function(x) for x in X_train])

# ニューラルネットワークの訓練
model = MLPRegressor(hidden_layer_sizes=(10, 10), max_iter=1000)
model.fit(X_train, y_train)

# グリッドデータで予測
X_grid = np.array([[xi, yi, 1 - xi - yi] for xi in np.linspace(-2, 2, 50) for yi in np.linspace(-2, 2, 50)])
y_grid_pred = model.predict(X_grid)
X_grid, Y_grid = X_grid[:, 0], X_grid[:, 1]
Z_grid = 1 - X_grid - Y_grid
Z_pred = y_grid_pred.reshape(50, 50)

# 予測値のプロット
ax.plot_surface(X_grid.reshape(50, 50), Y_grid.reshape(50, 50), Z_pred, cmap='plasma', alpha=0.5, edgecolor='none', label='ML Prediction')

# 軸のラベルと凡例
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Objective Function Value')
ax.legend()

plt.show()


```

```

import numpy as np

# シグモイド関数とその導関数
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
    return sigmoid(x) * (1 - sigmoid(x))

# ニューラルネットワークのパラメータ
X = np.array([[0.1], [0.2]])  # 入力
W = np.array([[0.3], [0.4]])  # 重み
b = 0.5  # バイアス
y_true = np.array([1])  # 正解ラベル

# フォワードパス
z = np.dot(W.T, X) + b
y_pred = sigmoid(z)

# 損失関数（例：二乗誤差）
loss = 0.5 * np.mean((y_true - y_pred) ** 2)

# チェーンルールを使ったバックプロパゲーション
# 出力層の勾配
loss_derivative = y_pred - y_true
sigmoid_derivative_value = sigmoid_derivative(z)
output_gradient = loss_derivative * sigmoid_derivative_value

# 重みとバイアスの勾配
W_gradient = np.dot(X, output_gradient.T)
b_gradient = np.sum(output_gradient)

print("Output Gradient:\n", output_gradient)
print("Weight Gradient:\n", W_gradient)
print("Bias Gradient:\n", b_gradient)

# 更新（例：学習率0.01）
learning_rate = 0.01
W -= learning_rate * W_gradient
b -= learning_rate * b_gradient

print("Updated Weights:\n", W)
print("Updated Bias:\n", b)
```


```
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor

# Constants
T = 20  # Total number of legs
N = 7   # Total number of animals

# Range for x (number of turtles)
x = np.arange(0, N + 1).reshape(-1, 1)  # Number of turtles

# Calculate y (number of cranes) and total number of legs
y_target = np.array([N - x_i for x_i in x]).flatten()  # Number of cranes
total_legs = 4 * x.flatten() + 2 * y_target  # Total legs

# Prepare the data for machine learning
X = x
y = y_target

# Initialize and train the Decision Tree Regressor model
model = DecisionTreeRegressor()
model.fit(X, y)

# Predict the number of cranes
predicted_cranes = model.predict(X)

# Plotting
plt.figure(figsize=(8, 6))

# Plot valid solutions
valid_indices = total_legs == T
plt.scatter(x.flatten()[valid_indices], y[valid_indices], color='blue', label='Valid Solutions')

# Plot constraint lines
plt.plot(x, (T - 4 * x.flatten()) / 2, 'r--', label=f'4x + 2y = {T}')
plt.plot(x, N - x.flatten(), 'g--', label=f'x + y = {N}')

# Plot predicted solutions
plt.scatter(X.flatten(), predicted_cranes, color='orange', marker='x', label='Predicted Solutions (Decision Tree)')

# Graph decorations
plt.xlabel('Number of Turtles')
plt.ylabel('Number of Cranes')
plt.title('Turtle and Crane Problem with Decision Tree Predictions')
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.grid(True, which='both', linestyle='--', linewidth=0.5)
plt.legend()
plt.show()
```