import numpy as np import matplotlib.pyplot as plt x = np.array([[16.46440512, 6.77816537, 4.67693823], [21.45568099, 2.70007973, 10.44515233], [18.08290128, 7.35194022, 5.66627759], [16.34649549, 9.62188545, 2.69405516], [12.70964398, 2.48753144, 0.37018093], [19.37682339, 5.76157334, 1.00874447], [13.12761634, 5.92041931, 10.1908916 ], [26.75319002, 5.72251906, 6.80545267], [28.90988282, 2.23081633, 8.04868817], [11.50324556, 9.52749012, 13.4500694 ], [23.75175114, 4.47125379, 14.85508421], [15.86684759, 8.46408672, 3.25345477], [17.04133683, 6.99479275, 9.94617305], [27.76789915, 2.97436951, 3.94983565], [ 2.13108175, 8.1379782 , 0.30976499], [ 2.61387899, 3.96505741, 11.37567981], [ 0.60655192, 8.81103197, 4.80025726], [24.97859537, 5.81272873, 5.75195841], [23.34470253, 8.81735362, 8.8247567 ], [26.10036445, 6.9253159 , 12.46572683], [29.35855027, 7.2525428 , 9.43472765], [23.97475693, 5.01324382, 13.08975983], [13.84438087, 9.56083635, 4.10313052], [23.41587529, 6.43990199, 11.97070251], [ 3.54823278, 4.23855049, 2.78453916], [19.19763064, 6.06393214, 14.29187485], [ 4.30059862, 0.19193198, 10.31232415], [28.34006751, 3.01574817, 3.23261516], [15.65544965, 6.60173537, 14.21055886], [12.4398582 , 2.90077607, 10.9628371 ], [ 7.93666836, 6.18015429, 3.80912464], [23.22701068, 4.28768701, 3.19967966], [13.68450997, 1.35474064, 7.77301071], [17.05301847, 2.98282326, 0.38494077], [ 0.56369401, 5.69964911, 3.11205113], [18.52906491, 5.90872761, 6.37028203], [18.36287168, 5.74325249, 5.61254971], [18.50801991, 6.5320082 , 6.95363137], [28.31244236, 6.5210327 , 4.16443059], [20.45460897, 4.31418435, 8.8017652 ], [10.78523702, 8.96546596, 12.95783409], [13.11095861, 3.6756187 , 1.76297784], [20.92893588, 4.35864925, 7.76068661], [ 1.80676415, 8.91923355, 1.9810216 ], [20.00300146, 8.06193989, 10.75289522], [20.11913609, 7.03888584, 5.94089554], [ 6.31147683, 1.00226887, 8.48131968], [ 3.86778893, 9.19482614, 2.74919754], [ 9.46285053, 7.142413 , 2.17271639], [10.91132313, 9.98847007, 7.32084421], [17.10590311, 1.49448305, 5.33419107], [13.1580454 , 8.68126057, 14.10647918], [29.65121514, 1.62492935, 11.47987881], [ 3.06134432, 6.15559564, 11.2299543 ], [ 6.26630268, 1.23819983, 13.5557961 ], [ 4.83928554, 8.48008229, 1.25133653], [19.59324976, 8.07318959, 8.28288705], [ 7.59874808, 5.69100739, 8.76714103], [13.98932319, 4.07183297, 14.42904568], [ 7.33276776, 0.69166995, 4.3822129 ], [ 4.76908751, 6.97428773, 3.6124317 ], [ 3.31125423, 4.53542683, 1.50440913], [19.68988768, 7.22055599, 0.24644444], [ 4.14548854, 8.66382326, 13.94293975], [ 5.89747085, 9.75521505, 10.0487482 ], [11.06175512, 8.55803342, 11.77729368], [24.6297969 , 0.11714084, 4.22595159], [ 2.91303827, 3.59978064, 8.79615249], [25.13834722, 7.29990562, 0.95932899], [ 2.88295224, 1.71629677, 7.28441394], [29.29378395, 5.21036606, 14.6624271 ], [14.05953605, 0.54337988, 13.14757868], [29.30283265, 1.99996525, 5.07238428], [18.14536559, 0.18521794, 14.42355232], [22.17790738, 7.93697703, 3.4755244 ], [ 1.17563377, 2.23924688, 14.23978234], [ 8.48420888, 3.45351681, 14.12066557], [ 3.60589684, 9.28081293, 11.98803881], [ 8.88420593, 7.04414402, 9.45671905], [ 3.56183157, 0.3183893 , 13.1143195 ], [ 9.53949538, 1.64694156, 4.39530427], [12.42788984, 6.21478401, 12.73415333], [ 1.92442489, 5.77228589, 9.26815038], [20.77416358, 2.37892821, 0.19855287], [16.99804363, 9.34213998, 5.20850277], [ 7.96168473, 6.13965956, 2.22211291], [15.6974416 , 5.35632803, 14.72744085], [ 2.81821532, 5.89909976, 7.17555461], [17.27839487, 7.3012203 , 7.46087048], [27.87888593, 3.11944995, 9.59208775], [ 9.55706857, 3.98221062, 5.52876909], [20.0223114 , 2.09843749, 2.05350408], [ 3.95393587, 1.86193006, 12.331766 ], [21.48981612, 9.4437239 , 2.84771868], [ 8.68218279, 7.39550795, 7.66978474], [ 5.49574086, 4.90458809, 3.36475543], [17.59538804, 2.27414628, 1.46766727], [ 0.60322639, 2.54356482, 12.93287276], [24.86820088, 0.5802916 , 14.59379234], [ 0.14086429, 4.34416626, 14.41251987]]) y = np.array([ 91.77298893, 133.92791233, 102.56149907, 84.13205351,52.10275592, 82.35734274, 106.34628594, 137.45786345,146.91978203, 121.16079423, 166.32794443, 83.16526552,120.38454281, 124.17065209, 20.82067546, 73.56588341,38.77280458, 125.73366996, 138.31659437, 163.51790917,161.56857823, 158.65170632, 82.04307725, 151.27635183,34.78490268, 148.14128217, 70.67037799, 122.14474373,136.51016164, 105.38258311, 56.56513557, 104.82494713, 91.16864966, 68.89058262, 26.57616316, 106.23105114,101.66564389, 109.91287842, 129.3853054 , 123.83249574,115.50575479, 62.38781453, 120.10270104, 29.1570957 ,135.82138637, 109.65522557, 70.30736594, 40.61813782, 54.75631317, 88.44042849, 92.17628635, 128.92663383,165.55863657, 77.8277196 , 94.62285345, 36.15932395, 121.55231634, 80.71517653, 129.03428344, 52.48419928,45.13337719, 28.22234678, 81.7073653 , 97.25979623,84.12508528, 109.68458531, 112.51862722, 61.07024611, 103.99360444, 52.6514567 , 184.80635931, 119.51817472,133.6206287 , 139.70831601, 106.21450094, 82.09714644,107.4100804 , 85.26179604, 89.56651718, 81.863751 , 60.25666142, 116.45512327, 63.01004847, 79.9975073 , 98.70678568, 49.61055032, 137.07661616, 55.13177645, 108.73829743, 152.51132347, 68.94413498, 86.25953408,81.08208708, 102.78606671, 79.61887351, 44.48966667, 75.30424561, 72.61510598, 163.71740287, 80.45539409]) x.sort() y.sort() print(x) def compute_gradient(x,y,w,b): m = x.shape[0] dw = np.zeros(x.shape[1]) db = 0 for i in range(x.shape[0]): err = (np.dot(x[i],w) +b- y[i]) for j in range(x.shape[1]): dw[j] += err*x[i,j] db = db + err; dw = dw/m db = db/m return dw,db def gradient_descent(x,y,w,b,alpha,iterations): for i in range(iterations): print(i) dw,db = compute_gradient(x,y,w,b) w = w-alpha*dw b = b-alpha*db return w,b w = np.ones(x.shape[1]) b = 1 iter = 100000 alpha = 5.0e-7 gradient_descent(x,y,w,b,alpha,iter) y_predict = np.zeros(x.shape[0]) for i in range(x.shape[0]): y_predict[i] = np.dot(w,x[i]) + b i = np.arange(1,101) plt.plot(i,y,'ro') plt.plot(i,y_predict,'ro') plt.show()
I am new to machine learning, i tried to solve a multi variable linear regression problem, but it is not working for some reasons, please help
I am new to machine learning, i tried to solve a multi variable linear regression problem, but it is not working for some reasons, please help
I want to solve this question without sklearn and only using numpy
New contributor
Yashwanth Koleti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.