Neural networks have revolutionized the sector of synthetic intelligence, particularly with the rise of Generative AI lately. Whereas the idea of neural networks is usually seen as advanced, on this submit, I intention to interrupt it down right into a easy, step-by-step information which you can simply perceive — and even check out domestically or utilizing a web-based platform like Google Colab.
I assume that you have already got a primary theoretical understanding of neural networks. If not, we suggest reviewing this post earlier than persevering with.
We’re beginning with a sensible downside: given a set of leads, how can we predict which leads usually tend to convert, permitting the gross sales staff to prioritize their efforts successfully?
It is a real-world problem that companies face day by day. You may be questioning whether or not conventional classification algorithms may very well be a greater match for this job, and you’d be proper.
Nevertheless, on this tutorial, we’re utilizing this downside primarily as a approach to discover and perceive the ideas of Neural Networks. By preserving our studying in a real-world situation, we intention to make the underlying rules extra significant and simpler to understand.
Since we do not need entry to real-world knowledge for this train, we’ll generate random take a look at knowledge, creating 1,000 data with pattern columns corresponding to wage
, age_group
, marital_status
, and others.
Whereas producing this pattern knowledge, we may even assign a label indicating whether or not a lead was transformed or not. To maintain it easy, we’ll use a primary rule: if sure parameters — corresponding to wage being above a threshold, revenue group, and schooling degree — meet particular situations, the lead shall be marked as transformed.
In fact, real-world knowledge could be way more advanced, however this simplified setup is ample to assist us get began and deal with understanding the important thing ideas.
# Step 1: Import Libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import tensorflow as tf
from tensorflow.keras.fashions import Sequential
from tensorflow.keras.layers import Dense, Dropout
import matplotlib.pyplot as plt# Step 2: Set random seed for reproducibility
np.random.seed(42)
tf.random.set_seed(42)
# Step 3: Generate artificial knowledge
knowledge = {
'wage': np.random.regular(50000, 15000, 1000), # Common wage = 50k, SD=15k
'age_group': np.random.selection([1, 2, 3], 1000), # 1: 18-25, 2: 26-35, 3: 36-50
'marital_status': np.random.selection([0, 1], 1000), # 0: Single, 1: Married
'children': np.random.randint(0, 4, 1000), # 0 to three children
'education_level': np.random.selection([1, 2, 3], 1000), # 1: Excessive Faculty, 2: Bachelor, 3: Masters
'occupation_type': np.random.selection([0, 1], 1000), # 0: Salaried, 1: Enterprise
'lead_source': np.random.selection([0, 1, 2], 1000), # 0: Web site, 1: Referral, 2: Commercial
'income_group': np.random.selection([0, 1, 2], 1000), # 0: Low, 1: Center, 2: Excessive
}
# Step 4: Generate labels (whether or not the lead transformed or not)
labels = (
(knowledge['salary'] > 55000).astype(int) +
(np.array(knowledge['income_group']) >= 1).astype(int) +
(np.array(knowledge['education_level']) >= 2).astype(int)
) >= 2
labels = labels.astype(int)
# Step 5: Create a DataFrame
df = pd.DataFrame(knowledge)
df['converted'] = labels
# Step 6: View the primary few rows
df.head()
Separating knowledge right into a coaching dataset and a take a look at dataset.
# Step 7: Separate options (X) and label (y)
X = df.drop('transformed', axis=1)
y = df['converted']# Step 8: Normalize the function values
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Step 9: Prepare-Check Break up (80% coaching, 20% validation)
X_train, X_val, y_train, y_val = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
As soon as we’ve got the enter knowledge prepared (leads with options like wage, age group, and many others.), the subsequent step is to construct the neural community mannequin that can study from this knowledge and predict whether or not a lead will convert.
# Step 10: Construct the mannequin
mannequin = Sequential([
Dense(32, activation='relu', input_shape=(8,)),
Dropout(0.2),
Dense(16, activation='relu'),
Dense(1, activation='sigmoid') # For binary classification
])
- The primary layer should specify
input_shape=(8,)
as a result of we’ve got 8 options. ReLU
or Rectified Linear Unit helps the community study nonlinear relationships. It’s a in style activation operate in deep studying that outputs the enter instantly if it’s constructive, and 0 in any other case.Sigmoid
makes the ultimate output a likelihood between 0 and 1.