الگوریتم شبکه های عصبی Neural Networks
در ادامه کد تصویر بالا رو قرار داده ام می توانید کپی کنید تغییر بدید و استفاده کنید.
# Import required libraries
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
import matplotlib.pyplot as plt
import numpy as np
# Create a non-linear 2D dataset shaped like moons
X, y = make_moons(n_samples=300, noise=0.2, random_state=42)
# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
# Initialize a neural network model with 2 hidden layers (10 and 5 neurons)
mlp = MLPClassifier(hidden_layer_sizes=(10, 5), max_iter=2000, random_state=42)
# Train the model using the training data
mlp.fit(X_train, y_train)
# Create a mesh grid to visualize decision boundaries
x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
xx, yy = np.meshgrid(
np.linspace(x_min, x_max, 500),
np.linspace(y_min, y_max, 500)
)
# Predict the class for each point in the mesh grid
Z = mlp.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# Plot the decision boundary and original data points
plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, Z, alpha=0.3) # decision regions
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', s=50) # input points
plt.title("Neural Network Classification (MLPClassifier)")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.tight_layout()
plt.show()
💻@voidcompile
>>Click here to continue<<
