Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions ML_for_Beginners/LinearRegression/PartC.py
Original file line number Diff line number Diff line change
@@ -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<X.shape[0]):
temp1=np.dot(thetas[0], X[i])
temp2=temp1- Y[i]
RMS += math.pow(temp2,2)
i+=1
for j in range(1, X.shape[1]):
temp=(thetas[0][j])**2
check=(ridge*(temp) + lasso*(abs(thetas[0][j])))
RMS += check/2*X.shape[0]
RMS /= (2*X.shape[0])
iterations+=1
if normal==1:
return RMS, thetas
if lasso!=0:
return RMS,thetas
if ridge!=0:
return RMS,thetas

df=pd.read_csv('data.csv')
X = df['Brain_Weight'].values
Y = df['Body_Weight'].values
x1=np.c_[np.ones(X.shape[0]),X]
# print(x1.shape)
mean = [0]*x1.shape[1]
maximum = [-10000000]*x1.shape[1]
minimum = [10000000]*x1.shape[1]
x1norm = x1.copy()
i=0
while i <(x1.shape[0]):
# only the column with actual values
j=1
mean[j] += x1norm[i][j]/x1.shape[0]
check=x1norm[i][j]
if (x1norm[i][j] > maximum[j]):
maximum[j] = check
if (x1norm[i][j] < minimum[j]):
minimum[j] =check
i+=1

k=0
while k<x1.shape[0]:
j=1
x1norm[k][j] = x1norm[k][j] - mean[j]
check1=maximum[j] - minimum[j]
x1norm[k][j] = x1norm[k][j]/(check1)
k+=1
# print(x1norm)
def graph(choice):
if choice==1:
rms, theta= gradientDescent(x1norm, Y,normal=1)
check5=(maximum[1] - minimum[1])
theta2 = theta[0][1]/check5
theta1 = theta[0][0] - theta2*mean[1]
plt.scatter(X, Y)
axes = plt.gca()
x_vals = np.array(axes.get_xlim())
y_vals = theta1 + theta2 * x_vals
plt.plot(x_vals, y_vals)
plt.title('Scatter plot for Normal Gradient Descent')
plt.xlabel('Brain Weight')
plt.ylabel('Body Weight')
plt.show()
elif choice==2:
rms, theta= gradientDescent(x1norm, Y,ridge=0.1)
check5=(maximum[1] - minimum[1])
theta2 = theta[0][1]/check5
theta1 = theta[0][0] - theta2*mean[1]
plt.scatter(X, Y)
axes = plt.gca()
x_vals = np.array(axes.get_xlim())
y_vals = theta1 + theta2 * x_vals
plt.plot(x_vals, y_vals)
plt.title('Scatter plot for Ridge Gradient Descent')
plt.xlabel('Brain Weight')
plt.ylabel('Body Weight')
plt.show()
else:
rms, theta= gradientDescent(x1norm, Y,lasso=0.7)
check5=(maximum[1] - minimum[1])
theta2 = theta[0][1]/check5
theta1 = theta[0][0] - theta2*mean[1]
plt.scatter(X, Y)
axes = plt.gca()
x_vals = np.array(axes.get_xlim())
y_vals = theta1 + theta2 * x_vals
plt.plot(x_vals, y_vals)
plt.title('Scatter plot for Lasso Gradient Descent')
plt.xlabel('Brain Weight')
plt.ylabel('Body Weight')
plt.show()
print('Enter choice for Gradient Descent')
print('1.Normal 2.RidgeRegularized 3.LassoRegularized' )
input1=int(input())
if input1==1:
graph(1)
elif input1==2:
graph(2)
else:
graph(3)

# print(x1)
# ======================================================================

# ==========================================================
1 change: 1 addition & 0 deletions ML_for_Beginners/LinearRegression/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This repository has code that implements LinearRegression with regularization from scratch using python.
168 changes: 168 additions & 0 deletions ML_for_Beginners/LinearRegression/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
Brain_Weight,Body_Weight
1.0,8.429337501253315
1.6,10.51622485105815
2.2,12.339744042559786
2.8000000000000003,19.217968745418617
3.4000000000000004,19.749753099850192
4.0,20.870851165656457
4.6000000000000005,23.821834197373732
5.200000000000001,28.25714507500938
5.800000000000001,32.02395312331614
6.4,23.72207465613001
7.000000000000001,40.88706492165431
7.600000000000001,32.173100245002075
8.200000000000001,38.924977394858914
8.8,44.784152179277044
9.400000000000002,47.88179649821765
10.000000000000002,42.301880217989144
10.600000000000001,51.392283372814916
11.200000000000001,50.02608171785642
11.8,69.1264734682581
12.400000000000002,43.2717139737902
13.000000000000002,49.978282129342865
13.600000000000001,57.36919808211232
14.200000000000003,76.19985111659967
14.800000000000002,87.17420938790532
15.400000000000002,88.64977384718975
16.0,65.42422805419027
16.6,68.0350415440881
17.200000000000003,98.84469923444162
17.800000000000004,79.61612659014075
18.400000000000002,89.90804708497112
19.000000000000004,91.05868486914645
19.6,91.81582543559142
20.200000000000003,66.42501458547926
20.800000000000004,114.1780060000467
21.400000000000002,114.72832232041631
22.000000000000004,92.7587433024462
22.6,128.56238028882933
23.200000000000003,118.79558215844521
23.800000000000004,101.26781637686868
24.400000000000002,110.32654535502952
25.000000000000004,119.80000000000003
25.600000000000005,122.55994092534604
26.200000000000003,127.2942563207219
26.800000000000004,128.7838874513495
27.400000000000006,129.22661335317832
28.000000000000004,137.23532884340943
28.600000000000005,133.8677251419253
29.200000000000003,132.9995176499797
29.800000000000004,141.76247369065763
30.400000000000006,129.96612762166652
31.000000000000004,149.51684013184948
31.600000000000005,157.00916281446777
32.2,165.89173687823117
32.800000000000004,129.6150471806473
33.400000000000006,151.4449475556998
34.00000000000001,162.22021707570545
34.60000000000001,163.5908331258433
35.2,177.85977229846404
35.800000000000004,174.7399978102262
36.400000000000006,173.35180569538315
37.00000000000001,196.2845719949171
37.60000000000001,186.0005921176151
38.2,188.39190636090288
38.800000000000004,197.46035734088946
39.400000000000006,182.00480909120648
40.00000000000001,173.6279819941465
40.60000000000001,183.2151219808904
41.2,175.69591117554117
41.800000000000004,176.39770283514744
42.400000000000006,229.6551295612347
43.00000000000001,191.91217985668558
43.60000000000001,230.92384733276657
44.2,196.39550054451226
44.800000000000004,234.59684873820942
45.400000000000006,189.02657498675777
46.00000000000001,218.14685141822648
46.60000000000001,200.99133805553217
47.20000000000001,229.8417909155177
47.800000000000004,217.0272036042009
48.400000000000006,231.1783698952044
49.00000000000001,214.18980914831027
49.60000000000001,221.60856531740188
50.20000000000001,238.0073251669907
50.800000000000004,240.50500555217104
51.400000000000006,241.6163187899625
52.00000000000001,247.4681564637112
52.60000000000001,241.65656196087923
53.20000000000001,253.8406575058408
53.80000000000001,255.72640045216093
54.400000000000006,255.9315732527974
55.00000000000001,260.76283679315435
55.60000000000001,263.01110536207887
56.20000000000001,259.2065205785753
56.80000000000001,277.9891119547556
57.400000000000006,274.2183994005689
58.00000000000001,281.4482136720826
58.60000000000001,287.21717545343773
59.20000000000001,291.86961132160087
59.80000000000001,293.1737490742374
60.07700002, 100.927658196754
60.400000000000006,299.93356126189167
61.00000000000001,291.45360012239126
61.60000000000001,304.4382074672925
62.20000000000001,276.3012397677959
62.80000000000001,295.7121195288604
63.400000000000006,305.07609455616733
64.0,307.58703459013816
64.60000000000001,303.1847249171093
65.2,309.4881584965749
65.80000000000001,297.2321629554691
66.4,343.23277879088675
67.00000000000001,280.47649257292863
67.60000000000001,293.4510858685041
68.20000000000002,321.40436855280603
68.80000000000001,338.06711549660463
69.4,328.1029041056382
70.00000000000001,334.3607835363164
70.60000000000001,349.59637439492934
71.20000000000002,322.8907098649025
71.80000000000001,331.6019310700573
72.4,318.51783041479024
73.00000000000001,318.79980576218935
73.60000000000001,290.5488240719017
74.20000000000002,346.2076029362015
74.80000000000001,326.81887450112805
75.4,356.78964794087
76.00000000000001,358.9929135926017
76.60000000000001,364.1137600258971
77.20000000000002,366.0241755816051
77.80000000000001,364.55467319794496
78.4,365.1957441320635
79.00000000000001,374.31985150679174
79.60000000000001,371.38340130902884
80.20000000000002,384.8444030232315
80.80000000000001,381.1673354686829
81.4,376.37417419155634
82.00000000000001,377.1573177637777
82.60000000000001,390.05796194250985
83.20000000000002,410.6884609284761
83.80000000000001,400.8694496075399
84.4,418.0638008455297
85.00000000000001,409.2500513592809
85.60000000000001,407.67566308551125
86.20000000000002,418.72819245784314
86.80000000000001,413.39522594752384
87.4,428.1227328827845
88.00000000000001,432.9131332633474
88.60000000000001,423.87711853910344
89.20000000000002,421.38515644224384
89.80000000000001,412.36013650301686
90.40000000000002,435.08962625053834
91.00000000000001,437.44750848518805
91.60000000000001,421.15202039088297
92.20000000000002,438.82590662840266
92.80000000000001,449.9052452705142
93.40000000000002,423.9039865498326
94.00000000000001,421.3626895013748
94.60000000000001,480.48612141637915
95.20000000000002,443.1061433376697
95.80000000000001,436.81613839388746
96.40000000000002,458.01504447328927
97.00000000000001,470.6110468364924
97.60000000000001,430.9284381391649
98.20000000000002,470.86835262163953
98.80000000000001,484.8748874740794
99.40000000000002,475.10319451317406
100.008675894355, 120.5368757
Loading