# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# least square solution(more equations less solutions)
# A(N*M) , W(M*1) , Y(N*1)
# N>M  (NO EXACT SOLUTION)
# Aw=y
# w=inv((A.T).A).(A.T).y

# +
# minimum norm solution(more parameters less equations)
# A(N*M) , W(M*1) , Y(N*1)
# N<M (MANY SOLUTION EXISTS)
# Aw=y
# w=(A.T).inv(A.A.T).y

# +
from sklearn.datasets import load_iris
import numpy as np
import keras
np.random.seed(100)

iris = load_iris()

# +
print('KEYS',iris.keys())
print()

X=iris["data"]
Y=iris["target"]

print(X[:3])
print(Y[:3])


# +
names=iris["target_names"]
features=iris["feature_names"]

print(names)
print(features)
# -

isamples = np.random.randint(len(Y) , size=5)
isamples

print(X.shape)
print(Y.shape)

print(X[isamples])
print(Y[isamples])

# +
#one-hot encoding

from keras.utils import to_categorical
ny = len(np.unique(Y))
Y = to_categorical(Y[:] , num_classes=ny)

print(X[isamples])
print(Y[isamples])
print()
print(ny)

# +
#training-testing split

from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix

X_train , X_test , Y_train , Y_test = train_test_split(X,Y,test_size=0.2,random_state=1)

# +
#data_normalization

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train) #computes mean and std

X_train=scaler.transform(X_train) #perform transformation
X_test = scaler.transform(X_test)

# +
#defining function to add 1's column

addcol = lambda x: np.concatenate((x,np.ones((x.shape[0],1))),axis=1)

# +
nx,ny=X_train.shape

def findWeights(A,Y):
    
    w = np.linalg.inv(A.T.dot(A)).dot(A.T.dot(Y))
    return w


# -

A = addcol(X_train)
Y = Y_train
w = findWeights(A,Y)
yd = A.dot(w)


def evaluate(y,yd):
    print(confusion_matrix(yd,y))


yd = np.argmax(yd,axis=1)
y = np.argmax(Y,axis=1)
evaluate(y,yd)

A = addcol(X_test)
Y = Y_test
w = findWeights(A,Y)
ynew = A.dot(w)

yd = np.argmax(ynew,axis=1)
y = np.argmax(Y,axis=1)
evaluate(y,yd)

addSqlcol = lambda x: np.concatenate((x, x**2, np.ones((x.shape[0], 1))), axis = 1)

A = addSqlcol(X_train)
Y = Y_train
w = findWeights(A,Y)
yd = A.dot(w)
yd = np.argmax(yd,axis=1)
y = np.argmax(Y,axis=1)
evaluate(y,yd)

A = addSqlcol(X_test)
Y = Y_test
w = findWeights(A,Y)
yd = A.dot(w)
yd = np.argmax(yd,axis=1)
y = np.argmax(Y,axis=1)
evaluate(y,yd)

StarcoderdataJupyterScriptsDedupFiltered