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