<jupyter_start><jupyter_text>**Q1: Multiclass Logistic Regression Implement Multiclass Logistic Regression (step-by-step) on Iris dataset using one vs. rest strategy**<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import pandas as pd
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris

data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
X = data.data
y = data.target
df.head()
data<jupyter_output><empty_output><jupyter_text>Step 4: Train/test split<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X,y,test_size=0.3,
                                                   random_state=42)<jupyter_output><empty_output><jupyter_text>Step 5: Optimal β’s using Gradient Descent<jupyter_code>n=1000
alpha=0.01
m,k = X_train.shape
beta = np.zeros(k)
for i in range(n):
    cost_gradient = np.zeros(k)
    z=X_train.dot(beta)
    predicted = 1/(1+np.exp(-z))
    difference = predicted - Y_train
    for j in range(k):
        cost_gradient[j] = np.sum(difference.dot(X_train[:,j]))
    
    for j in  range(k):
        beta[j] = beta[j]-(alpha/m)*cost_gradient[j]
        
print(beta)<jupyter_output>[ 8.63897098  0.65225234 15.32468182  6.36521411]<jupyter_text>Step 6: Predicting and Labeling<jupyter_code>y_predict = 1/(1+np.exp(-(X_test.dot(beta))))

y_label = np.zeros(len(y_predict))

for i in range(len(y_predict)):
    if(y_predict[i]>=0.5):
        y_label[i] = 1<jupyter_output><empty_output><jupyter_text>Step 7: Performance Evaluation<jupyter_code>TP=0
TN=0
FP=0
FN=0
Y_test=np.array(Y_test).reshape(-1,1)
for i in range(len(y_label)):
    if(Y_test[i]==1 and y_label[i]==1):
        TP=TP+1
    if(Y_test[i]==1 and y_label[i]==0):
        FN=FN+1
    if(Y_test[i]==0 and y_label[i]==1):
        FP=FP+1
    if(Y_test[i]==0 and y_label[i]==0):
        TN=TN+1
print(TP,TN,FP,FN)

accuracy=(TP+TN)/(TP+TN+FP+FN)
#For positive class:
precision_pos=TP/(TP+FP)
recall_pos=TP/(TP+FN)
f1_score_pos=2*precision_pos*recall_pos/(precision_pos+recall_pos)
print(precision_pos,recall_pos,f1_score_pos)<jupyter_output>0.40625 1.0 0.5777777777777777<jupyter_text>Using inbuilt function<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris

data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
X = data.data
y = data.target
df.head()

X_train, X_test, Y_train, Y_test = train_test_split(X,y,test_size=0.3,
                                                   random_state=42)

lr=LogisticRegression(solver='sag')
#sag denote Stochastic Average Gradient
model=lr.fit(X_train,Y_train)
Y_label1=model.predict(X_test)
print(metrics.classification_report(Y_test,Y_label1))<jupyter_output>precision    recall  f1-score   support

           0       1.00      1.00      1.00        19
           1       1.00      1.00      1.00        13
           2       1.00      1.00      1.00        13

    accuracy                           1.00        45
   macro avg       1.00      1.00      1.00        45
weighted avg       1.00      1.00      1.00        45
{'fraction_non_alphanumeric': 0.11768311369904995, 'fraction_numerical': 0.05393809377873123, 'mean_word_length': 3.32317880794702, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '660764', 'license': 'Apache-2.0', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1245, 'n_words': 234, 'path': 'Examples/MLK 6.ipynb', 'repo_name': 'ShubhGurukul/Machine-Learning-All-Algorithm-With-Python', 'size': '3263'}