Professional Documents
Culture Documents
Worksheet - 2
Student Name: Sunil Kumar UID: 22MCI10042
Section/Group: 22MAM2(B) Semester: Fourth
Date of Performance: 04-02-2024 Subject:22CAH-771
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:
plt.figure(figsize=(10,6))
plt.scatter(X[:,0],X[:,1],c=y,cmap='winter',s=100)
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
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
preprocessor = ColumnTransformer(
transformers=[
('num', StandardScaler(), numeric_features),
('cat', OneHotEncoder(), categorical_features)])
# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# Assuming coef_ and intercept_ are obtained from your trained model
coef_ = pipeline.named_steps['classifier'].coef_[0]
intercept_ = pipeline.named_steps['classifier'].intercept_[0]
# Fit label encoder and transform the string labels to numeric values
y_test_encoded = label_encoder.fit_transform(y_test)
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.