Data Science Algorithms Day 18. Let’s start with Day 18 today | by Data Analytics | Jun, 2024


Let’s start with Day 18 today

30 Days of Data Science Series: https://t.me/datasciencefun/1708

Let’s learn about Neural Networks

#### Concept
Neural Networks are a set of algorithms, modeled loosely after the human brain, designed to recognize patterns. They interpret sensory data through a kind of machine perception, labeling, or clustering of raw input. The patterns they recognize are numerical, contained in vectors, into which all real-world data, be it images, sound, text, or time series, must be translated.

#### Key Features of Neural Networks
1. Layers: Composed of an input layer, hidden layers, and an output layer.
2. Neurons: Basic units that take inputs, apply weights, add a bias, and pass through an activation function.
3. Activation Functions: Functions applied to the neurons’ output, introducing non-linearity (e.g., ReLU, sigmoid, tanh).
4. Backpropagation: Learning algorithm for training the network by minimizing the error.
5. Training: Adjusts weights based on the error calculated from the output and the expected output.

#### Key Steps
1. Initialize Weights and Biases: Start with small random values.
2. Forward Propagation: Pass inputs through the network layers to get predictions.
3. Calculate Loss: Measure the difference between predictions and actual values.
4. Backward Propagation: Compute the gradient of the loss function and update weights.
5. Iteration: Repeat forward and backward propagation for a set number of epochs or until the loss converges.

#### Implementation

Let’s implement a simple Neural Network using Keras on the Breast Cancer dataset.

##### Example
# Import necessary libraries
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# Load the Breast Cancer dataset
data = load_breast_cancer()
X = data.data
y = data.target

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

# Standardizing the data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Creating the Neural Network model
model = Sequential([
Dense(30, input_shape=(X_train.shape[1],), activation=’relu’),
Dense(15, activation=’relu’),
Dense(1, activation=’sigmoid’)
])

# Compiling the model
model.compile(optimizer=’adam’, loss=’binary_crossentropy’, metrics=[‘accuracy’])

# Training the model
model.fit(X_train, y_train, epochs=50, batch_size=10, validation_split=0.2, verbose=1)

# Making predictions
y_pred = (model.predict(X_test) > 0.5).astype(“int32”)

# Evaluating the model
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
class_report = classification_report(y_test, y_pred)

print(f”Accuracy: {accuracy}”)
print(f”Confusion Matrix:\n{conf_matrix}”)
print(f”Classification Report:\n{class_report}”)

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here