# machine learning multiple linear regression[br]# b x1 x2 x3[br]X=[[[br]1,12.08,2.92,2.8,81.4,1.16,2.31,4165.196191],[[br]2,12.38,3.53,3.22,46.17,0.89,1.88,3561.146205],[[br]3,14.02,2.59,4.01,72.8,0.89,2.72,4284.348574],[[br]4,17.67,6.75,2.63,39.81,1.08,1.88,5098.680869],[[br]5,17.52,4.57,3.18,10.94,1.51,1.9,3406.132832],[[br]6,14.53,4.81,2.69,53.6,0.94,1.67,4395.763259],[[br]7,13.49,3.6,2.93,63.71,0.8,1.85,4104.400989],[[br]8,11.58,3.03,3.25,53,0.69,1.93,3496.742701],[[br]9,12.52,2.72,2.43,65.77,0.95,1.98,4025.851153],[[br]10,13.25,3.94,3.71,66.2,1.14,2.65,4285.026122],[[br]11,15.04,4.39,2.22,61.11,1.08,1.77,4691.546305],[[br]12,16.19,6.3,2.29,49.1,1.53,1.86,5128.20297],[[br]13,16.82,5.42,2.8,66.65,1.17,1.98,5113.940775],[[br]14,17.39,4.95,2.55,56.43,1.87,2.19,5546.431951],[[br]15,13.83,5.26,2.51,66.22,0.18,1.64,4323.568513],[[br]16,11.34,2.72,3.43,58.03,0.57,2.15,3539.984263],[[br]17,19.55,8.41,3.08,35.49,1.34,1.95,5425.900608],[[br]18,15.58,3.67,2.53,29.43,1.35,1.88,3796.037029],[[br]19,14.73,5,3.03,67.42,0.9,1.98,4534.585915],[[br]20,12.47,2.53,3.54,48.82,0.9,2.54,3803.411386],[[br]21,18.08,5.7,2.38,53.56,1.26,2.09,5778.851488],[[br]22,16.32,4.06,2.41,46.78,1.21,1.72,4771.307836],[[br]23,12.62,2.39,2.38,15.68,1,1.48,2486.075498],[[br]24,10.03,1.88,3.29,37.74,0.38,1.7,2515.440716],[[br]25,18.66,5.37,1.39,53.35,1.77,1.71,6046.257538],[[br]26,7.27,2.01,3.41,54.56,-0.04,1.68,2351.289487],[[br]27,15.78,5,2.55,52.64,1.37,2.04,4971.979256],[[br]28,15.73,6.42,3.74,31.9,1.1,2.15,3999.613909],[[br]29,12.38,2.18,4.09,31.4,0.72,2.07,2760.485982],[[br]30,12.89,2.41,2.9,51.24,0.81,1.97,3825.661812],[[br]31,18.48,5.93,2.85,46.89,1.45,2.45,5859.748328],[[br]32,14.74,3.1,2.45,36.86,0.92,1.59,3775.981178],[[br]33,12.55,3.22,3.43,56.93,0.79,2.27,3907.28923],[[br]34,13.58,2.39,2.95,52.16,1.13,2.2,4118.233273],[[br]35,11.76,2.94,4.18,61.21,0.43,2.45,3621.261108],[[br]36,11.98,3.67,3.17,27.12,0.54,1.69,2718.652945],[[br]37,13.56,3.13,3.59,42.96,0.48,1.93,3608.47473],[[br]38,16.55,5.7,2.28,63.39,1.33,1.96,5249.625543],[[br]39,15.22,3.67,3.52,42.24,1.14,2.26,4239.53187],[[br]40,14.54,3.67,2.04,66.27,0.87,1.64,4471.241364],[[br]41,14.37,5.93,2.98,47.62,0.97,1.87,4363.202854],[[br]42,15.4,3.9,2.86,46.29,1.01,1.99,4495.004769],[[br]43,10.14,3.78,4.26,61.89,-0.19,1.97,3098.436639],[[br]44,11.09,2.56,3.16,29.62,0.83,1.96,2677.297348],[[br]45,18.37,5.93,2.56,44.22,2.07,2.34,5779.876876],[[br]46,14.71,4.35,2.39,26.33,0.66,1.15,3210.90138],[[br]47,9.29,1.67,2.82,63.86,0.3,1.6,2967.240201],[[br]48,11.85,3.46,3.26,24.76,0.08,1.38,2415.825125],[[br]49,18.59,5.1,2.91,49.2,1,1.9,5563.040771],[[br]50,18.18,4.39,3.27,45.48,1.24,2.25,5337.145786],[[br]51,18.72,5.75,2.57,44.74,1.59,1.96,5670.537767],[[br]52,20.96,9.21,2.45,33.84,1.53,1.84,5899.353594],[[br]53,15.31,3.35,2.76,55.2,0.8,1.97,4601.071319],[[br]54,11.98,2.75,4.63,60.31,0.76,2.87,3741.129167],[[br]55,18.16,7.1,2.12,41.55,1.97,1.97,5605.978153],[[br]56,13.61,3.53,2.75,55.04,0.76,1.74,4079.994302],[[br]57,13.67,3.13,2.89,28.69,0.62,1.73,3146.528143],[[br]58,15.21,4.1,3.11,32.37,0.78,1.81,3691.832452],[[br]59,16.1,3.25,2.16,26.77,1.09,1.29,3542.532883],[[br]60,13,4.66,3.1,64.54,0.2,1.71,3969.558019],[[br]61,18.32,5.7,3.1,57.18,1.3,2.48,5851.024387],[[br]62,14.96,2.16,2.25,34.65,1.07,1.67,3733.769597],[[br]63,18.21,5.7,3.34,51.06,1.12,2.28,5620.911404],[[br]64,15.07,5.42,3.04,39.39,1.07,2.17,4319.662079],[[br]65,17.75,4.66,2.72,59.32,1.67,2.35,5620.445827],[[br]66,15.77,7.03,4.1,44.54,0.04,1.97,4431.905847],[[br]67,14.19,3.29,2.69,28.7,1.22,1.83,3376.2837],[[br]68,13.8,3.29,3.15,46.08,1.28,2.07,3999.97933],[[br]69,14.84,3.97,3.43,48.8,1.04,2.11,4336.465709],[[br]70,12.54,3.49,3.59,58.03,0.47,2.03,3767.10242],[[br]71,10.13,2.39,3.49,63.79,0.16,1.93,3180.611366],[[br]72,14.17,3.94,2.92,59.3,0.91,1.91,4320.554389],[[br]73,14.85,5,3.63,44.93,0.72,2.05,4220.557084],[[br]74,16.62,4.62,2.31,31.41,1.1,1.54,4087.529805],[[br]75,17.85,5.81,2.96,59.16,0.48,1.74,5321.469922],[[br]76,18.73,7.46,2.88,53.39,1.42,2.14,6053.071368],[[br]77,17.2,6.36,2.26,48.84,1.47,1.9,5446.810082],[[br]78,10.49,1.62,3.77,52.31,0.46,2.37,3190.699612],[[br]79,13.53,4.53,2.31,67.51,0.8,1.67,4276.410007],[[br]80,18.04,3.9,2.47,57.53,1.15,2.01,5518.256409],[[br]81,14.97,2.75,2.87,54.85,1.17,2.2,4555.146788],[[br]82,16.18,4.01,3.04,41.4,1.22,2.01,4489.29306],[[br]83,15.07,3.39,3.39,56.7,0.38,1.88,4319.787412],[[br]84,13.69,3.86,3.51,69.53,0.6,2.04,4113.145433],[[br]85,10.15,1.72,2.97,40.51,0.59,1.76,2734.773959],[[br]86,13.45,2.51,3.25,58.3,1.04,2.33,4142.406714],[[br]87,15.94,3.82,3.81,37.13,1.36,2.43,4208.174933],[[br]88,14.88,4.81,3.3,63.31,0.95,2.03,4519.923344],[[br]89,11.12,1.88,3.09,58.97,0.52,1.85,3364.213781],[[br]90,13.5,2.66,3.94,36.68,1.17,2.6,3562.635128],[[br]91,13.72,4.01,3.41,40.24,0.84,2.18,3806.229207],[[br]92,7.38,1.88,2.54,42.24,0.38,1.28,2119.315516],[[br]93,20.21,8.33,2.5,23.78,1.47,1.82,4877.71473],[[br]94,10.69,2.66,3.49,84.33,0.39,1.97,3591.891958],[[br]95,15.75,3.6,2.82,70.68,0.95,2.04,4700.224408],[[br]96,17.96,4.26,2.87,49.41,0.92,2.01,5350.949293],[[br]97,16.95,4.14,2.3,61.43,1.47,2.05,5278.785405],[[br]98,19.73,7.85,2.63,49.91,1.59,2.11,6455.048131],[[br]99,11.91,4.48,2.27,47.38,0.84,1.57,3671.944944],[[br]100,15.53,5.42,2.64,64.49,1.55,2.1,4948.929165],[[br]101,14.44,2.92,2.34,68.76,0.98,1.97,4464.894985],[[br]102,13.37,4.66,3.04,52.23,0.86,1.72,4003.874956],[[br]103,10.5,3.22,3.69,61.09,0.45,2.17,3376.059883],[[br]104,18.9,4.57,2.65,18.63,1.06,1.54,3856.991967],[[br]105,17.89,4.22,1.86,34.13,1.75,1.62,4689.43224],[[br]106,15.32,3.71,3.29,55.99,1.35,2.42,4748.315024],[[br]107,13.48,2.61,2.93,55.3,0.5,1.7,3890.428004],[[br]108,17.17,3.82,2.35,38.08,1.43,1.97,4733.965648],[[br]109,20.86,6.3,2.53,40.57,2.08,2.43,6480.140811],[[br]110,15.19,5.05,3.11,57.97,1.15,2.16,4751.960471],[[br]111,14.95,3.29,2.69,75.73,0.58,1.8,4429.601197],[[br]112,12.1,5.1,3.97,63.24,0.38,1.93,3695.605482],[[br]113,18.24,4.31,2.85,44.53,1.39,2.06,5334.209437],[[br]114,19.86,6.82,2.77,45.44,1.33,2.08,6171.948872],[[br]115,19.54,5.93,3.23,37.4,0.82,1.8,5188.165252],[[br]116,18.37,4.95,2.45,42.26,1.4,2.06,5429.61122],[[br]117,12.89,4.26,4.09,66.21,0.81,2.77,4190.949492],[[br]118,15.2,3.49,3.2,42.63,1.52,2.23,4321.08436],[[br]119,12.23,2.39,3.02,40.67,1.02,1.84,3278.519165],[[br]120,17.88,5.58,3.2,49.61,1.23,2.37,5568.129512],[[br]121,15.79,4.22,3.01,63.24,0.48,2.07,4761.541276],[[br]122,12.38,2.48,2.63,33.48,1.17,1.73,3096.041435],[[br]123,18.98,9.78,2.69,46.4,1.11,1.67,5984.91987],[[br]124,13.76,3.86,3.46,52.36,0.5,1.89,3987.451637],[[br]125,23.55,8.25,1.28,14.24,2.18,1.24,5107.668954],[[br]126,14.86,3.9,2.64,60.5,1.33,1.95,4590.775041],[[br]127,16.43,5.99,2.62,35.5,1.13,1.72,4394.908724],[[br]128,15.86,2.94,2.74,51.49,1.62,2.15,4801.169597],[[br]129,12.98,5.26,3.25,60.51,0.93,1.98,4118.586934],[[br]130,17.21,4.62,2.79,20.12,1.52,1.93,3762.933398],[[br]131,15.34,3.06,2.11,50.65,1.17,1.71,4583.194189],[[br]132,17.57,4.44,2.3,44.57,1.46,1.98,5257.439724],[[br]133,10.74,1.72,3.33,62.94,0.31,1.89,3258.271193],[[br]134,18.79,4.48,3.57,35.74,0.93,2.3,4896.3969],[[br]135,12.83,2.69,3.67,17.2,0.61,2.01,2508.507049],[[br]136,15.35,4.66,3.07,26.63,1.53,2.05,3655.497757],[[br]137,14.88,5.16,3.47,51.29,0.38,1.77,4300.222781],[[br]138,14.69,3.63,2.39,62.93,1.04,1.93,4566.400754],[[br]139,7.77,1.48,2.8,40.24,0.37,1.43,2107.139414],[[br]140,15.87,6.82,3.2,32.82,0.58,1.64,3989.012964],[[br]141,18.75,5.16,1.45,58.05,1.37,1.55,5903.37822],[[br]142,15.63,4.66,2.85,40.83,0.84,1.7,4260.004869],[[br]143,18.06,8,2.6,56.29,1.64,2.18,5996.900444],[[br]144,12.05,2.36,3.21,42.36,0.5,1.77,3196.99447],[[br]145,14.44,4.53,2.81,65.32,1.16,1.93,4476.367366],[[br]146,16.69,5.05,3.44,36.22,1.48,2.52,4605.799987],[[br]147,15.8,2.72,3.29,42.13,0.43,1.97,4180.788056],[[br]148,16.84,5.1,3.16,60.43,1.6,2.6,5400.374809],[[br]149,13.46,4.57,3.04,56.59,0.1,1.53,3950.292582],[[br]150,14.63,7.1,3.02,21.47,1.26,1.79,3418.470532],[[br]151,12.2,2.92,3.36,28.87,0.82,1.69,2705.868622],[[br]152,10.5,2.56,3.11,54.54,0.45,1.82,3219.49199],[[br]153,18.68,6.17,2.5,43.35,1.66,1.87,5607.51549],[[br]154,16.88,6.62,2.45,65.82,0.63,1.5,5083.318118],[[br]155,15.42,4.06,2.8,18.98,1.5,1.93,3341.157722],[[br]156,13.01,3.6,2.38,50.41,0.48,1.55,3867.438514],[[br]157,17.92,5.37,2.3,57.84,1.33,1.92,5629.970179],[[br]158,15.97,3.19,2.63,52.55,1.41,2.09,4859.067707],[[br]159,13.32,3.19,2.45,57.86,1.05,1.74,4092.730535],[[br]160,18.1,9.58,3.34,56.09,1.02,2.15,5898.169102],[[br]161,16.21,4.01,2.77,59.83,1.21,2.32,5085.588965],[[br]162,18,5.58,1.71,28.46,1.74,1.49,4470.882859],[[br]163,17.86,6.96,2.5,26.22,0.28,0.93,3923.997348],[[br]164,11.6,1.73,2.18,53.54,0.6,1.5,3458.846102],[[br]165,17.98,4.26,2.94,25.17,0.9,1.86,4013.751035],[[br]166,16.61,3.49,1.83,60.13,1.54,1.86,5175.59912],[[br]167,19.06,9.49,3.2,55.53,1.57,2.39,6395.708841],[[br]168,12.83,3.56,3.15,29.87,1.09,2.09,3182.815944],[[br]169,18.96,5.81,2.17,29.28,1.58,1.95,4862.060183],[[br]170,11.71,1.88,4.01,44.03,0.04,1.84,2940.975811],[[br]171,11.91,5.42,3.83,28.94,0.38,1.78,2791.133133],[[br]172,17.44,5.7,3.44,57.33,1.33,2.38,5449.314018],[[br]173,10.95,2.23,3.67,47.06,0.12,1.93,3031.432666],[[br]174,12.94,2.64,2.88,38.29,1.25,1.94,3460.397749],[[br]175,15.72,3.78,2.51,35.09,1.42,1.95,4158.294202],[[br]176,14.02,4.48,3.01,62.59,1.04,2.16,4441.251847],[[br]177,14.49,4.85,3,22.54,1.08,1.99,3333.963492],[[br]178,11.8,2.92,3.64,35.35,0.35,1.76,2819.977847],[[br]179,19.29,5,2.82,58.56,1.23,1.97,5880.785643],[[br]180,7.22,1.42,3.6,63.09,-0.03,1.67,2362.269336],[[br]181,15.7,3,3.41,46.86,0.14,1.93,4290.072266],[[br]182,14.48,3.82,3.56,47.44,0.67,2.11,4144.2208],[[br]183,9.88,2.72,3.64,55.19,0.52,2.16,3141.540928],[[br]184,18.8,6.05,3.23,74.07,1.66,2.54,5692.716924],[[br]185,18.07,3.78,2.89,28.67,1.52,2.1,4319.373276],[[br]186,15.68,5.37,3.77,57.85,0.72,2.35,4807.05583],[[br]187,15.28,3.49,2.29,34.78,1.28,1.61,3899.879873],[[br]188,10.88,3.32,4.09,55.09,0.81,2.4,3413.73739],[[br]189,16.14,5.05,3.16,38.03,0.97,2.05,4385.713419],[[br]190,16.12,4.1,2.42,35.84,1.48,1.84,4291.702607],[[br]191,13.52,3.86,3.45,31.57,1.51,2.22,3407.341602],[[br]192,6.55,1.13,4.13,56.69,0.28,2.1,2187.774422],[[br]193,20.37,6.69,2.37,37.76,1.4,1.99,5899.275409],[[br]194,12.92,3.9,3.68,71.4,0.11,1.91,3855.930544],[[br]195,17.13,5.99,2.88,51.45,1.77,2.28,5489.491177],[[br]196,11.95,3.13,2.97,67.18,0.8,2.06,3847.571003],[[br]197,17.99,9.87,3.38,44.32,0.98,2.08,5601.227131],[[br]198,12.12,2.27,3.52,57.07,-0.04,1.73,3409.575363],[[br]199,15.55,4.48,2.48,58.25,1.89,2.35,5087.592149],[[br]200,20.89,7.54,3.23,46.17,1.71,2.27,6662.622385]][br][br][br]#functions matrix (list) handling passing numpy[br]def matrix_mult(A,B):[br] if len(A[0])==len(B):[br] return [[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*B)] for X_row in A][br] else:[br] raise Exception("matrix row %3d x col %3d" % (len(A[0]),len(B)))[br][br]def matrix_transpose(X):[br] return [[X[j][i] for j in range(len(X))] for i in range(len(X[0]))][br][br]def matrix_add(X,Y):[br] return [[X[i][j] + Y[i][j] for j in range(len(X[0]))] for i in range(len(X))][br] [br]def matrix_scalar(X,y):[br] return [[y*X[i][j] for j in range(len(X[0]))] for i in range(len(X))][br] [br]def identity(N):[br] R = [][br] for row in range(N):[br] R.append([0]*N)[br] R[row][row] = 1[br] return R[br][br]def matrix_inv(AM):[br] IM = identity(len(AM))[br] for fd in range(len(AM)):[br] fdScaler = 1.0 / AM[fd][fd][br] for j in range(len(AM)):[br] AM[fd][j] *= fdScaler[br] IM[fd][j] *= fdScaler[br] for i in list(range(len(AM)))[0:fd] + list(range(len(AM)))[fd+1:]:[br] crScaler = AM[i][fd][br] for j in range(len(AM)):[br] AM[i][j] = AM[i][j] - crScaler * AM[fd][j][br] IM[i][j] = IM[i][j] - crScaler * IM[fd][j][br] return IM[br][br]def flatten(xss):[br] return [x for xs in xss for x in xs][br]#---- function matrix[br][br][br]# y = b + w1 x1+ w2 x2 + w3 x3 -> wi wights (m)[br]Y = matrix_transpose(X)[br]Y = matrix_transpose([Y[7]])[br]X = matrix_transpose(X)[br]X = X[1:7][br]X.append([1]*200)[br]X = matrix_transpose(X)[br][br]def average(X):[br] s=0[br] for j in range(len(X[0])): [br] for i in range(len(X)):[br] s=s+X[i][j] [br] return s/(len(X)*len(X[0]))[br] [br]def predict(X, w):[br] return matrix_mult(X, w)[br][br]def loss(X, Y, w):[br] mm = (matrix_add(predict(X, w), Y))[br] mm = [[mm[i][j] ** 2 for j in range(len(mm[0]))] for i in range(len(mm))][br] return average(mm)[br][br]def gradient(X, Y, w):[br] mm = matrix_mult(matrix_transpose(X), matrix_add(predict(X, w),Y))[br] return matrix_scalar(mm, 2 / len(X))[br][br]def train(X, Y, w, iterations, lr):[br] print("train(X,YY,m=",w,"iterations=",iterations,"lr=",lr)[br] print("Iteration %4d => Loss: %.15f" % (0, loss(X, Y, w)))[br] for i in range(iterations):[br] #ll = loss(X, Y, w)[br] w = matrix_add(w,matrix_scalar(gradient(X, Y, w) , -lr))[br] #lll = loss(X, Y, w)[br] #print("Iteration %4d => Loss: %.15f" % (i, lll))[br] #if abs(ll - lll) < 0.00000001:[br] # break[br] print("Iteration %4d => Loss: %.15f" % (iterations, loss(X, Y, w)))[br] return w[br]print(X) [br]YY = matrix_scalar(Y,(-1))[br]m = matrix_transpose([[0]*len(X[0])])[br]#m[6][0]=-1230[br][br]#simple trainer for multiple linear regression function[br]m = train(X, YY, m, iterations=200000 , lr=0.0001)[br][br][br]print("\nWeights: %s" % matrix_transpose(m))[br]id = identity(len(X[0]))[br]print("\nNormalengleichung (X^T X)^-1 X^T Y" ) [br]NN=matrix_mult(matrix_inv(matrix_mult(matrix_transpose(X),X)),matrix_mult(matrix_transpose(X),Y))[br]print("Weights:",matrix_transpose(NN))[br][br]print("\nA few predictions:")[br]for i in range(5):[br] print("X[%d]= -> %.4f (Y label: %.1f)" % (i, predict([X[i]], m)[0][0], Y[i][0]))[br][br][br]"""[br]X,Y [br]https://aegis4048.github.io/mutiple_linear_regression_and_visualization_in_python#fig-3]https://aegis4048.github.io/mutiple_linear_regression_and_visualization_in_python#fig-3[br][br][br]"""[br]