0
0
ML Pythonml~5 mins

Simple neural network with scikit-learn in ML Python

Choose your learning style9 modes available
Introduction

A simple neural network helps computers learn patterns from data to make predictions. Using scikit-learn makes it easy to create and train these networks without deep math.

When you want to classify handwritten digits like 0-9.
When predicting if an email is spam or not based on its words.
When recognizing simple patterns in small datasets.
When you want a quick model to test ideas before using complex tools.
Syntax
ML Python
from sklearn.neural_network import MLPClassifier

model = MLPClassifier(hidden_layer_sizes=(number_of_neurons,), activation='activation_function', max_iter=number_of_iterations)
model.fit(X_train, y_train)
predictions = model.predict(X_test)

hidden_layer_sizes sets how many neurons are in each hidden layer. For example, (5,) means one hidden layer with 5 neurons.

activation controls how neurons decide to pass signals. Common options: 'relu' or 'logistic'.

Examples
This creates a neural network with one hidden layer of 5 neurons using the ReLU activation function.
ML Python
from sklearn.neural_network import MLPClassifier

model = MLPClassifier(hidden_layer_sizes=(5,), activation='relu', max_iter=200)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
This creates a neural network with two hidden layers: first with 10 neurons, second with 5 neurons, using the logistic activation.
ML Python
from sklearn.neural_network import MLPClassifier

model = MLPClassifier(hidden_layer_sizes=(10, 5), activation='logistic', max_iter=300)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Sample Model

This program trains a simple neural network on the iris flower dataset to classify flower types. It prints the accuracy and predictions on test data.

ML Python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score

# Load iris flower data
iris = load_iris()
X = iris.data
y = iris.target

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Create a simple neural network with one hidden layer of 5 neurons
model = MLPClassifier(hidden_layer_sizes=(5,), activation='relu', max_iter=500, random_state=42)

# Train the model
model.fit(X_train, y_train)

# Predict on test data
predictions = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)

print(f"Test Accuracy: {accuracy:.2f}")
print(f"Predictions: {predictions}")
OutputSuccess
Important Notes

Neural networks may need more iterations (max_iter) to learn well.

Set random_state for reproducible results.

MLPClassifier does not scale data automatically; scaling inputs yourself can improve results.

Summary

A simple neural network learns patterns to classify data.

Use MLPClassifier from scikit-learn to build and train it easily.

Adjust hidden layers and neurons to improve learning.