[go: up one dir, main page]

0% found this document useful (0 votes)
50 views10 pages

Deep Learning Perceptron

The document details work done on training perceptrons on classification datasets. It includes generating a synthetic 2D classification dataset and training a perceptron on it to obtain the weights, bias and decision boundary. It also discusses loading a thyroid dataset from Kaggle, preprocessing it, training a perceptron pipeline on it and obtaining the results.

Uploaded by

Avinash Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views10 pages

Deep Learning Perceptron

The document details work done on training perceptrons on classification datasets. It includes generating a synthetic 2D classification dataset and training a perceptron on it to obtain the weights, bias and decision boundary. It also discusses loading a thyroid dataset from Kaggle, preprocessing it, training a perceptron pipeline on it and obtaining the results.

Uploaded by

Avinash Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Deep Learning Lab

Worksheet - 2
Student Name: Sunil Kumar UID: 22MCI10042
Section/Group: 22MAM2(B) Semester: Fourth
Date of Performance: 04-02-2024 Subject:22CAH-771

Aim of the Practical:


Train a Perceptron and evaluate weights and bias.

Task to be done:
2.1) Train a Perceptron for evaluating weights and bias for own created classification dataset.
2.2) Train a Perceptron for evaluating weights and bias for Kaggle classification dataset.

Solution:
Own classification dataset:

from sklearn.datasets import make_classification


import numpy as np
X, y = make_classification(n_samples=100, n_features=2, n_informative=1,n_redundant=0,
n_classes=2, n_clusters_per_class=1,
random_state=41,hypercube=False,class_sep=10)
X
y
import matplotlib.pyplot as plt

plt.figure(figsize=(10,6))
plt.scatter(X[:,0],X[:,1],c=y,cmap='winter',s=100)

"""Below Function returns Weights"""

def perceptron(X,y):

X = np.insert(X,0,1,axis=1)
weights = np.ones(X.shape[1])
lr = 0.1

for i in range(1000):
j = np.random.randint(0,100)
y_hat = step(np.dot(X[j],weights))
weights = weights + lr*(y[j]-y_hat)*X[j]
return weights[0],weights[1:]

def step(z):
return 1 if z>0 else 0

intercept_,coef_ = perceptron(X,y)

print(coef_)
print(intercept_)

m = -(coef_[0]/coef_[1])
b = -(intercept_/coef_[1])

x_input = np.linspace(-3,3,100)
y_input = m*x_input + b

plt.figure(figsize=(10,6))
plt.plot(x_input,y_input,color='red',linewidth=3)
plt.scatter(X[:,0],X[:,1],c=y,cmap='winter',s=100)
plt.ylim(-3,2)

Scatterplot
Perceptron Decision Boundary

Kaggle classification dataset:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from google.colab import drive
drive.mount('/content/gdrive')

df = pd.read_csv("/content/gdrive/MyDrive/Deep Learning/Thyroid_Diff.csv")
df
df.shape
from matplotlib import pyplot as plt
df['Age'].plot(kind='hist', bins=20, title='Age')
plt.gca().spines[['top', 'right',]].set_visible(False)
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
plt.subplots(figsize=(8, 8))
df_2dhist = pd.DataFrame({
x_label: grp['Thyroid Function'].value_counts()
for x_label, grp in df.groupby('Hx Radiothreapy')
})
sns.heatmap(df_2dhist, cmap='viridis')
plt.xlabel('Hx Radiothreapy')
_ = plt.ylabel('Thyroid Function')
for column in df.columns:
unique_values = df[column].unique()
print(f"Unique values of column '{column}': {unique_values}")
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Perceptron
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score

# Separate features (X) and target variable (y)


X = df.drop('Response', axis=1) # Assuming 'Response' is the target variable
y = df['Response']

# Split the dataset 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)

# Preprocessing: one-hot encoding for categorical variables and standardization


numeric_features = X.select_dtypes(include=['int64', 'float64']).columns
categorical_features = X.select_dtypes(include=['object']).columns

preprocessor = ColumnTransformer(
transformers=[
('num', StandardScaler(), numeric_features),
('cat', OneHotEncoder(), categorical_features)])

# Define the pipeline


pipeline = Pipeline(steps=[('preprocessor', preprocessor),
('classifier', Perceptron())])

# Fit the model


pipeline.fit(X_train, y_train)

# Predict on the test set


y_pred = pipeline.predict(X_test)

# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

# Print the learned weights and bias


print("Learned weights:", pipeline.named_steps['classifier'].coef_)
print("Learned bias:", pipeline.named_steps['classifier'].intercept_)
import numpy as np
import matplotlib.pyplot as plt

# Assuming coef_ and intercept_ are obtained from your trained model
coef_ = pipeline.named_steps['classifier'].coef_[0]
intercept_ = pipeline.named_steps['classifier'].intercept_[0]

# Calculate the slope and intercept of the decision boundary line


m = -(coef_[0] / coef_[1])
b = -(intercept_ / coef_[1])

# Generate x values for the decision boundary line


x_input = np.linspace(-3, 3, 100)

# Calculate corresponding y values for the decision boundary line


y_input = m * x_input + b
# Transform the test data using the pipeline's preprocessor
X_test_scaled = pipeline.named_steps['preprocessor'].transform(X_test)

from sklearn.preprocessing import LabelEncoder

# Initialize label encoder


label_encoder = LabelEncoder()

# Fit label encoder and transform the string labels to numeric values
y_test_encoded = label_encoder.fit_transform(y_test)

# Plot the decision boundary line and data points


plt.figure(figsize=(10, 6))
plt.plot(x_input, y_input, color='red', linewidth=3, label='Decision Boundary')
plt.scatter(X_test_scaled[:, 0], X_test_scaled[:, 1], c=y_test_encoded, cmap='winter', s=100,
label='Test Data')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Perceptron Decision Boundary')
plt.legend()
plt.ylim(-3, 2)
plt.show()
Learning outcomes (What I have learnt):

1.Model Selection Mastery: Learn to choose the right machine learning model for a given task,
optimizing accuracy and efficiency.
2.Data Preprocessing Expertise: Master techniques like feature scaling and encoding to enhance
model accuracy by ensuring data quality.
3.Hyperparameter Tuning Skills: Hone the ability to fine-tune model parameters, maximizing
accuracy through methods like grid search and cross-validation.
4.Feature Engineering Proficiency: Develop strategies to create or modify features, boosting
model accuracy by capturing relevant patterns in the data.
5.Evaluation and Interpretation: Acquire skills to accurately evaluate model performance and
interpret predictions, facilitating continuous improvement for enhanced accuracy.

You might also like