Question: Please help me in understanding this code explain me this code in detail step by step import numpy as np from sklearn.metrics import accuracy_score from

Please help me in understanding this code explain me this code in detail step by step

import numpy as np from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split import pandas as pd

class Perceptron: def __init__(self, learning_rate=0.001, n_iters=1000): self.lr = learning_rate self.n_iters = n_iters self.activation_func = self.activation_function self.bias = None

def fit(self, X, y): samples, features = X.shape self.weights = np.zeros(features) self.bias = 0

y_ = np.where(y > 0, 1, 0) for _ in range(self.n_iters): for idx, x_i in enumerate(X.values): linear_output = np.dot(x_i, self.weights) + self.bias y_predicted = self.activation_func(linear_output) update = self.lr * (y_[idx] - y_predicted) self.weights += update * x_i self.bias += update

def predict(self, X): linear_output = np.dot(X, self.weights) + self.bias y_predicted = self.activation_func(linear_output) return y_predicted

def activation_function(self, x): return np.where(x >= 0, 1, 0)

def accuracy(y_true, y_pred): accuracy = np.sum(y_true == y_pred) / len(y_true) return accuracy

df = pd.read_csv('/Users/Hamza/Desktop/CI CODE LINEAR PERCEPTRON/dataset to be used.csv') for i in range(0,2): if i == 1: print('BloodPressure||SkinThickness||BMI => Output') X = df.iloc[:, [2, 3, 5]] y = df.iloc[:, [-1]] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) p = Perceptron(learning_rate=0.01, n_iters=2000) p.fit(X_train, y_train) predictions = p.predict(X_test) print("Perceptron classification accuracy", accuracy_score(y_test, predictions)) else: print('Glucose||Insulin||DiabetesPedigreeFunction => Output') X = df.iloc[:, [1, 4, 6]] y = df.iloc[:, [-1]] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) p = Perceptron(learning_rate=0.01, n_iters=1500) p.fit(X_train, y_train) predictions = p.predict(X_test) print("Perceptron classification accuracy", accuracy_score(y_test, predictions))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!