import numpy as np
import matplotlib.pyplot as plt
# サンプルデータの生成
train_sizes = np.linspace(0.1, 1.0, 10)
train_scores_1 = np.array([0.6, 0.65, 0.75, 0.78, 0.8, 0.82, 0.83, 0.84, 0.85, 0.86])
train_scores_2 = np.array([0.55, 0.6, 0.7, 0.73, 0.76, 0.78, 0.79, 0.8, 0.81, 0.82])
train_scores_3 = np.array([0.5, 0.55, 0.65, 0.68, 0.7, 0.72, 0.73, 0.74, 0.75, 0.76])
train_scores_means = np.mean([train_scores_1, train_scores_2, train_scores_3], axis=0)
train_scores_std = np.std([train_scores_1, train_scores_2, train_scores_3], axis=0)
# 学習曲線のプロット
plt.figure(figsize=(10, 6))
plt.plot(train_sizes, train_scores_means, "o-", color="b", label="Training score")
plt.fill_between(
train_sizes,
train_scores_means - train_scores_std,
train_scores_means + train_scores_std,
alpha=0.1,
color="b",
)
plt.title("Learning Curve")
plt.xlabel("Training Data Proportion")
plt.ylabel("Accuracy")
plt.ylim(0, 1)
plt.legend()
plt.grid()
plt.show()
Convergence graph for evolutionary algorithms can be plotted using the following code:
import numpy as np
import matplotlib.pyplot as plt
# Generate sample data for three algorithms
generations = np.arange(1, 101)
algorithm_1 = np.exp(-0.05 * generations) + 0.05 * np.random.rand(100)
algorithm_2 = np.exp(-0.03 * generations) + 0.05 * np.random.rand(100)
algorithm_3 = np.exp(-0.04 * generations) + 0.05 * np.random.rand(100)
# shape of the data
print("Generations shape:", generations.shape)
print("Algorithm 1 shape:", algorithm_1.shape)
print("Algorithm 2 shape:", algorithm_2.shape)
print("Algorithm 3 shape:", algorithm_3.shape)
# Plotting the convergence graph
plt.figure(figsize=(10, 6))
plt.plot(generations, algorithm_1, label="Algorithm 1", color="r")
plt.plot(generations, algorithm_2, label="Algorithm 2", color="g")
plt.plot(generations, algorithm_3, label="Algorithm 3", color="b")
plt.title("Convergence Graph")
plt.xlabel("Number of Function Evaluations")
plt.ylabel("Function Value")
plt.ylim(0, 1)
plt.legend()
plt.grid()
plt.show()
Generations shape: (100,)
Algorithm 1 shape: (100,)
Algorithm 2 shape: (100,)
Algorithm 3 shape: (100,)