From 5554c9b39e50d3605f370740a64667a6d836a171 Mon Sep 17 00:00:00 2001 From: Kanishk Rana <32814336+Guardian99@users.noreply.github.com> Date: Mon, 21 Oct 2019 11:10:14 +0530 Subject: [PATCH 1/8] Create LinearRegression From Scratch --- ML_For_Beginners/LinearRegression From Scratch | 1 + 1 file changed, 1 insertion(+) create mode 100644 ML_For_Beginners/LinearRegression From Scratch diff --git a/ML_For_Beginners/LinearRegression From Scratch b/ML_For_Beginners/LinearRegression From Scratch new file mode 100644 index 0000000..7c912f9 --- /dev/null +++ b/ML_For_Beginners/LinearRegression From Scratch @@ -0,0 +1 @@ +This code is written for implementing LinearRegression with regularization parameter from scratch. From 28c24711ecc86919b7b14664c3801f3538d2a5cd Mon Sep 17 00:00:00 2001 From: Kanishk Rana <32814336+Guardian99@users.noreply.github.com> Date: Mon, 21 Oct 2019 11:10:43 +0530 Subject: [PATCH 2/8] Delete LinearRegression From Scratch --- ML_For_Beginners/LinearRegression From Scratch | 1 - 1 file changed, 1 deletion(-) delete mode 100644 ML_For_Beginners/LinearRegression From Scratch diff --git a/ML_For_Beginners/LinearRegression From Scratch b/ML_For_Beginners/LinearRegression From Scratch deleted file mode 100644 index 7c912f9..0000000 --- a/ML_For_Beginners/LinearRegression From Scratch +++ /dev/null @@ -1 +0,0 @@ -This code is written for implementing LinearRegression with regularization parameter from scratch. From 0f2e7aa064c8d042c5c6af9264235fe8be416c27 Mon Sep 17 00:00:00 2001 From: Kanishk Rana <32814336+Guardian99@users.noreply.github.com> Date: Mon, 21 Oct 2019 11:12:47 +0530 Subject: [PATCH 3/8] Create README --- ML_for_Beginners/LinearRegression/README | 1 + 1 file changed, 1 insertion(+) create mode 100644 ML_for_Beginners/LinearRegression/README diff --git a/ML_for_Beginners/LinearRegression/README b/ML_for_Beginners/LinearRegression/README new file mode 100644 index 0000000..d8b8e1b --- /dev/null +++ b/ML_for_Beginners/LinearRegression/README @@ -0,0 +1 @@ +This repository has code that implements LinearRegression with regularization from scratch using python. From 37025a17acec3273896bad1a8bf89c11c0c70c50 Mon Sep 17 00:00:00 2001 From: Kanishk Rana <32814336+Guardian99@users.noreply.github.com> Date: Mon, 21 Oct 2019 11:14:01 +0530 Subject: [PATCH 4/8] Add files via upload --- ML_for_Beginners/LinearRegression/PartC.py | 139 +++++++++++++++++ ML_for_Beginners/LinearRegression/data.csv | 168 +++++++++++++++++++++ 2 files changed, 307 insertions(+) create mode 100644 ML_for_Beginners/LinearRegression/PartC.py create mode 100644 ML_for_Beginners/LinearRegression/data.csv diff --git a/ML_for_Beginners/LinearRegression/PartC.py b/ML_for_Beginners/LinearRegression/PartC.py new file mode 100644 index 0000000..ec83fb1 --- /dev/null +++ b/ML_for_Beginners/LinearRegression/PartC.py @@ -0,0 +1,139 @@ +import pandas as pd +import numpy as np +import matplotlib.pyplot as plt +import warnings +import math +import copy +from sklearn.model_selection import train_test_split, cross_val_score +from sklearn.tree import DecisionTreeClassifier +from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score +from sklearn.feature_selection import SelectFromModel +from sklearn.cluster import KMeans +# ====================================================================== +def gradientDescent(X, Y, learning_rate = 0.5, max_iter=1000,normal=0, lasso = 0, ridge = 0): + RMS = 0 + thetas = np.zeros([1, X.shape[1]]) + iterations=0 + thetas_temp = np.zeros([1, X.shape[1]]) + while iterations<(max_iter): + for j in range(X.shape[1]): + thetas_temp[0][j] = thetas[0][j] + if (j == 0): + pass + else: + if (thetas_temp[0][j] > 0): + check1=(1*lasso + thetas[0][j]*2*ridge) + thetas_temp[0][j] -= check1/(2*X.shape[0]) + else: + check2=((-1)*lasso + 2*ridge*thetas[0][j]) + thetas_temp[0][j] -= check2/(2*X.shape[0]) + for i in range(X.shape[0]): + for j in range(X.shape[1]): + thetas_temp[0][j] -= ((learning_rate*((np.dot(thetas[0], X[i])) - Y[i]))/(X.shape[0]))*X[i][j] + for j in range(X.shape[1]): + thetas[0][j] = thetas_temp[0][j] + i=0 + while (i maximum[j]): + maximum[j] = check + if (x1norm[i][j] < minimum[j]): + minimum[j] =check + i+=1 + +k=0 +while k Date: Mon, 21 Oct 2019 11:22:09 +0530 Subject: [PATCH 5/8] README File --- ML_for_Beginners/MNIST/README | 1 + 1 file changed, 1 insertion(+) create mode 100644 ML_for_Beginners/MNIST/README diff --git a/ML_for_Beginners/MNIST/README b/ML_for_Beginners/MNIST/README new file mode 100644 index 0000000..d6a72e5 --- /dev/null +++ b/ML_for_Beginners/MNIST/README @@ -0,0 +1 @@ +This code implements Logistic Regression on MNIST dataset. From 9467bc15044a71b7a3dd3e7ea10a74c2e99b52e4 Mon Sep 17 00:00:00 2001 From: Kanishk Rana <32814336+Guardian99@users.noreply.github.com> Date: Mon, 21 Oct 2019 11:22:39 +0530 Subject: [PATCH 6/8] Add files via upload --- ML_for_Beginners/MNIST/MNIST.py | 95 +++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 ML_for_Beginners/MNIST/MNIST.py diff --git a/ML_for_Beginners/MNIST/MNIST.py b/ML_for_Beginners/MNIST/MNIST.py new file mode 100644 index 0000000..5ded826 --- /dev/null +++ b/ML_for_Beginners/MNIST/MNIST.py @@ -0,0 +1,95 @@ +import pandas as pd +import numpy as np +import matplotlib.pyplot as plt +import math +from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score,roc_curve +from sklearn.feature_selection import SelectFromModel +from sklearn.linear_model import LogisticRegression +from sklearn.preprocessing import MinMaxScaler,StandardScaler +# ================================ + +trainingimages=np.load('trainingimages.npy') +traininglabel=np.load('traininglabel.npy') +testimages=np.load('testingimages.npy') +testlabels=np.load('testinglabels.npy') +print(testlabels) +print(len(testlabels)) +scaler = StandardScaler() +scaler.fit(trainingimages) +trainingimages = scaler.transform(trainingimages) +testimages = scaler.transform(testimages) +# model1=LogisticRegression(multi_class='ovr',solver='saga',penalty='l1',max_iter=10) +model2=LogisticRegression(multi_class='ovr',solver='lbfgs',penalty='l2',max_iter=10) +# model1.fit(trainingimages,traininglabel) +model2.fit(trainingimages,traininglabel) + +pred2=model2.predict_proba(testimages) +length=testlabels.shape[0] +print(length) +Encoding = np.zeros([length, 10]) +for i in range(length): + Encoding[i][testlabels[i]] = 1 +Encoding =Encoding.T; +pred2 = pred2.T + +score1=[] +fpr1=[] +tpr1=[] + +for i in range (10): + fpr, tpr, thresholds = roc_curve(Encoding[i], pred2[i]) + print(fpr) + print(tpr) + fpr1.append(fpr) + tpr1.append(tpr) + plt.plot(fpr, tpr, label=str(i) + ":" + str(roc_auc_score(Encoding[i], pred2[i]))) + score1.append(roc_auc_score(Encoding[i], pred2[i])) +plt.xlabel('False +ve Rate') +plt.ylabel('True +ve Rate') +plt.title('ROC Curve') +plt.legend(loc="lower right") +plt.plot([0, 1], [0, 1]) +plt.xlim([0.0, 1.0]) +plt.ylim([0.0, 1.0]) +plt.show() + + +def modelchosen(model,testimages,trainingimages,testlabels,traininglabel): + + misclassified_test = 0 + wrongtest = [0]*10 + totaltest = [0]*10 + y_pred_test = model.predict(testimages) + for i in range(y_pred_test.shape[0]): + if (y_pred_test[i] != testlabels[i]): + misclassified_test += 1 + wrongtest[testlabels[i]]=wrongtest[testlabels[i]]+1 + totaltest[testlabels[i]] += 1 + + for i in range(10): + print('Class: ',i,'Accuracy: ', 100*((totaltest[i] - wrongtest[i])/totaltest[i])) + + print("Accuracy for test with model:", 100*((y_pred_test.shape[0] - misclassified_test)/y_pred_test.shape[0])) + + + misclassified_training = 0 + wrongtrain = [0]*10 + totaltrain = [0]*10 + y_pred_training = model.predict(trainingimages) + for i in range(y_pred_training.shape[0]): + if (y_pred_training[i] != traininglabel[i]): + misclassified_training += 1 + wrongtrain[traininglabel[i]]+=1 + totaltrain[traininglabel[i]]+=1 + + for i in range(10): + print('Class: ',i,'Accuracy: ', 100*((totaltrain[i] - wrongtrain[i])/totaltrain[i])) + print("Accuracy for train with model:", 100*((y_pred_training.shape[0] - misclassified_training)/y_pred_training.shape[0])) + +print('Check Accuracy for 1.L1 2.L2') +input1=int(input()) +if input1==1: + modelchosen(model1,testimages,trainingimages,testlabels,traininglabel) +else: + modelchosen(model2,testimages,trainingimages,testlabels,traininglabel) + From c2c6102d3269f7e418065c75ffac7ed47b5259a9 Mon Sep 17 00:00:00 2001 From: Kanishk Rana <32814336+Guardian99@users.noreply.github.com> Date: Thu, 24 Oct 2019 00:41:41 +0530 Subject: [PATCH 7/8] Create README --- .../Absolute and Relative Errors/Stirling Approximation/README | 1 + 1 file changed, 1 insertion(+) create mode 100644 Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/README diff --git a/Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/README b/Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/README new file mode 100644 index 0000000..83014f4 --- /dev/null +++ b/Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/README @@ -0,0 +1 @@ +The code in python implements stirling approximation and plots the relative and absolute error using Matplotlib. From 863a7bc3b49d97665c8772a05e350ff4c581f25b Mon Sep 17 00:00:00 2001 From: Kanishk Rana <32814336+Guardian99@users.noreply.github.com> Date: Thu, 24 Oct 2019 00:42:02 +0530 Subject: [PATCH 8/8] Add files via upload --- .../approximationfactorial.npy | Bin 0 -> 208 bytes .../inbuiltfactorial.npy | Bin 0 -> 168 bytes .../Stirling Approximation/q2.py | 50 ++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/approximationfactorial.npy create mode 100644 Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/inbuiltfactorial.npy create mode 100644 Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/q2.py diff --git a/Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/approximationfactorial.npy b/Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/approximationfactorial.npy new file mode 100644 index 0000000000000000000000000000000000000000..e3956c56742b4a078ad234b6da04de561cf5c1df GIT binary patch literal 208 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+i=qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I$-20EHL3bhL411|Q0FBPgyZ|w#1G+x+l`DdTFYj3c7gt&w4<<5;$8qFOJ z?X%^vYK(Qb&*>_^z@W{+zRV&twPdqHSxC?FGu4kBX1kxNJ^WGBk>!Qa`OPKXjsSZa BKOq1B literal 0 HcmV?d00001 diff --git a/Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/inbuiltfactorial.npy b/Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/inbuiltfactorial.npy new file mode 100644 index 0000000000000000000000000000000000000000..7559a4e05de02587a3b04701dea1b22b8ba39f66 GIT binary patch literal 168 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlWC%^qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I$-20EHL3bhL411?4e1_mY|W&>ggAg%!73qXMl!VC-zbAh;%m4PANoB;sg C;2>H6 literal 0 HcmV?d00001 diff --git a/Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/q2.py b/Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/q2.py new file mode 100644 index 0000000..f43aef5 --- /dev/null +++ b/Plots For Beginners/Absolute and Relative Errors/Stirling Approximation/q2.py @@ -0,0 +1,50 @@ +# ================================== +from __future__ import division +import numpy as np +import scipy as sp +import matplotlib.pyplot as plt +import numpy.linalg as npla +import scipy.linalg as spla +import seaborn as sns +import math +# ================================== + +# inbuiltfactorial=[] +# for i in range(1,11): +# inbuiltfactorial.append(math.factorial(i)) +# print('inbuiltfactorial= ',inbuiltfactorial) + +# approximationfactorial=[] +# for i in range(1,11): +# value=math.sqrt(2*math.pi*i)*((i/math.e)**i) +# approximationfactorial.append(value) +# print('approximationfactorial= ',approximationfactorial) + + +# nparray1=np.asarray(inbuiltfactorial) +# np.save('inbuiltfactorial', nparray1) + +# nparray2=np.asarray(approximationfactorial) +# np.save('approximationfactorial', nparray2) + +# ====================================================== + +inbuiltfactorial=np.load('inbuiltfactorial.npy') +approximationfactorial=np.load('approximationfactorial.npy') + +absoluteerror=np.subtract(inbuiltfactorial,approximationfactorial) +relativeerror=np.divide(absoluteerror,inbuiltfactorial) + +range1=list(range(1,11)) +print(range1) + +plt.scatter(range1,absoluteerror, c='b', marker='o') +plt.ylabel('Absolute Error') +plt.xlabel('X') +plt.title('Absolute Error for Stirling Approximation') +plt.show() +plt.scatter(range1, relativeerror, c='r', marker='x') +plt.ylabel('Relative Error') +plt.xlabel('X') +plt.title('Relative Error for Stirling Approximation') +plt.show() \ No newline at end of file