57]:
import numpy as np
from [Link] import mean_squared_error
58]:
# used to lock the data points generated randomly - same data points in each run
[Link](0)
# Generate feature matrix X (100 Samples, 3 features)
X = [Link](100,3)
print("Feature Matrix:\n",X)
58]:
Feature Matrix:
[[ 1.76405235 0.40015721 0.97873798]
[ 2.2408932 1.86755799 -0.97727788]
[ 0.95008842 -0.15135721 -0.10321885]
[ 0.4105985 0.14404357 1.45427351]
[ 0.76103773 0.12167502 0.44386323]
[ 0.33367433 1.49407907 -0.20515826]
[ 0.3130677 -0.85409574 -2.55298982]
[ 0.6536186 0.8644362 -0.74216502]
[ 2.26975462 -1.45436567 0.04575852]
[-0.18718385 1.53277921 1.46935877]
[ 0.15494743 0.37816252 -0.88778575]
[-1.98079647 -0.34791215 0.15634897]
[ 1.23029068 1.20237985 -0.38732682]
[-0.30230275 -1.04855297 -1.42001794]
[-1.70627019 1.9507754 -0.50965218]
[-0.4380743 -1.25279536 0.77749036]
[-1.61389785 -0.21274028 -0.89546656]
[ 0.3869025 -0.51080514 -1.18063218]
[-0.02818223 0.42833187 0.06651722]
[ 0.3024719 -0.63432209 -0.36274117]
[-0.67246045 -0.35955316 -0.81314628]
[-1.7262826 0.17742614 -0.40178094]
[-1.63019835 0.46278226 -0.90729836]
[ 0.0519454 0.72909056 0.12898291]
[ 1.13940068 -1.23482582 0.40234164]
[-0.68481009 -0.87079715 -0.57884966]
[-0.31155253 0.05616534 -1.16514984]
[ 0.90082649 0.46566244 -1.53624369]
[ 1.48825219 1.89588918 1.17877957]
[-0.17992484 -1.07075262 1.05445173]
[-0.40317695 1.22244507 0.20827498]
[ 0.97663904 0.3563664 0.70657317]
[ 0.01050002 1.78587049 0.12691209]
[ 0.40198936 1.8831507 -1.34775906]
[-1.270485 0.96939671 -1.17312341]
[ 1.94362119 -0.41361898 -0.74745481]
[ 1.92294203 1.48051479 1.86755896]
[ 0.90604466 -0.86122569 1.91006495]
[-0.26800337 0.8024564 0.94725197]
[-0.15501009 0.61407937 0.92220667]
[ 0.37642553 -1.09940079 0.29823817]
[ 1.3263859 -0.69456786 -0.14963454]
[-0.43515355 1.84926373 0.67229476]
[ 0.40746184 -0.76991607 0.53924919]
[-0.67433266 0.03183056 -0.63584608]
[ 0.67643329 0.57659082 -0.20829876]
[ 0.39600671 -1.09306151 -1.49125759]
[ 0.4393917 0.1666735 0.63503144]
[ 2.38314477 0.94447949 -0.91282223]
[ 1.11701629 -1.31590741 -0.4615846 ]
[-0.06824161 1.71334272 -0.74475482]
[-0.82643854 -0.09845252 -0.66347829]
[ 1.12663592 -1.07993151 -1.14746865]
[-0.43782004 -0.49803245 1.92953205]
[ 0.94942081 0.08755124 -1.22543552]
[ 0.84436298 -1.00021535 -1.5447711 ]
[ 1.18802979 0.31694261 0.92085882]
[ 0.31872765 0.85683061 -0.65102559]
[-1.03424284 0.68159452 -0.80340966]
[-0.68954978 -0.4555325 0.01747916]
[-0.35399391 -1.37495129 -0.6436184 ]
[-2.22340315 0.62523145 -1.60205766]
[-1.10438334 0.05216508 -0.739563 ]
[ 1.5430146 -1.29285691 0.26705087]
[-0.03928282 -1.1680935 0.52327666]
[-0.17154633 0.77179055 0.82350415]
[ 2.16323595 1.33652795 -0.36918184]
[-0.23937918 1.0996596 0.65526373]
[ 0.64013153 -1.61695604 -0.02432612]
[-0.73803091 0.2799246 -0.09815039]
[ 0.91017891 0.31721822 0.78632796]
[-0.4664191 -0.94444626 -0.41004969]
[-0.01702041 0.37915174 2.25930895]
[-0.04225715 -0.955945 -0.34598178]
[-0.46359597 0.48148147 -1.54079701]
[ 0.06326199 0.15650654 0.23218104]
[-0.59731607 -0.23792173 -1.42406091]
[-0.49331988 -0.54286148 0.41605005]
[-1.15618243 0.7811981 1.49448454]
[-2.06998503 0.42625873 0.67690804]
[-0.63743703 -0.39727181 -0.13288058]
[-0.29779088 -0.30901297 -1.67600381]
[ 1.15233156 1.07961859 -0.81336426]
[-1.46642433 0.52106488 -0.57578797]
[ 0.14195316 -0.31932842 0.69153875]
[ 0.69474914 -0.72559738 -1.38336396]
[-1.5829384 0.61037938 -1.18885926]
[-0.50681635 -0.59631404 -0.0525673 ]
[-1.93627981 0.1887786 0.52389102]
[ 0.08842209 -0.31088617 0.09740017]
[ 0.39904635 -2.77259276 1.95591231]
[ 0.39009332 -0.65240858 -0.39095338]
[ 0.49374178 -0.11610394 -2.03068447]
[ 2.06449286 -0.11054066 1.02017271]
[-0.69204985 1.53637705 0.28634369]
[ 0.60884383 -1.04525337 1.21114529]
[ 0.68981816 1.30184623 -0.62808756]
[-0.48102712 2.3039167 -1.06001582]
[-0.1359497 1.13689136 0.09772497]
[ 0.58295368 -0.39944903 0.37005589]]
59]:
# Generate true weights and bias(used to create target values)
w_true = [Link](3,1)
b_true = [Link](1)
print("True weights:\n",w_true)
print("\nTrue Bias:\n",b_true)
59]:
True weights:
[[-1.30652685]
[ 1.65813068]
[-0.11816405]]
True Bias:
[-0.6801782]
60]:
# Add small Gaussian noise to make data realistic
noise = [Link](100,1) * 0.1
# Initialize model weights and bias (random start point)
w = [Link](3,1)
b = [Link](1)
print("Model weights:\n",w)
print("\nModel bias:\n",b)
60]:
Model weights:
[[-1.76853845]
[ 0.35548179]
[ 0.81451982]]
Model bias:
[0.05892559]
61]:
# create target values
y_true = X @ w_true + b_true + noise
print("Target Values:\n",y_true)
61]:
Target Values:
[[-2.37046035]
[-0.44190301]
[-2.29369335]
[-1.2843077 ]
[-1.45581271]
[ 1.36953066]
[-2.21711046]
[ 0.09467133]
[-6.1752915 ]
[ 1.85923761]
[-0.18916193]
[ 1.32186209]
[-0.25233214]
[-1.88474304]
[ 4.83781007]
[-2.28772272]
[ 1.109523 ]
[-1.9744494 ]
[ 0.08646454]
[-2.17338333]
[-0.41742757]
[ 1.88569894]
[ 2.30851651]
[ 0.67131238]
[-4.33435071]
[-1.06662561]
[ 0.03240067]
[-1.02236875]
[ 0.45704829]
[-2.46353569]
[ 1.58302914]
[-1.38814086]
[ 2.07672439]
[ 2.12147195]
[ 2.6573509 ]
[-3.65112843]
[-0.85149398]
[-3.56301419]
[ 0.81983808]
[ 0.31019108]
[-3.07427174]
[-3.5751754 ]
[ 2.8387721 ]
[-2.53720898]
[ 0.38662104]
[-0.54831468]
[-2.91021179]
[-1.19670585]
[-1.98343448]
[-4.33593864]
[ 2.2727013 ]
[ 0.26261913]
[-3.99154327]
[-1.20975616]
[-1.67861375]
[-3.19727663]
[-1.74580545]
[ 0.40143759]
[ 1.9893796 ]
[-0.50266423]
[-2.42304023]
[ 3.4668716 ]
[ 0.91754921]
[-4.91093467]
[-2.65431165]
[ 0.61357156]
[-1.21869787]
[ 1.27921546]
[-4.11061406]
[ 0.73488255]
[-1.43132833]
[-1.53896763]
[-0.23189509]
[-2.32622957]
[ 0.88525803]
[-0.44274097]
[-0.29581182]
[-0.94621191]
[ 1.72358297]
[ 2.54886862]
[-0.48651344]
[-0.77111843]
[-0.39802229]
[ 2.02059215]
[-1.31203359]
[-2.61113523]
[ 2.59727154]
[-1.02283158]
[ 2.06539468]
[-1.48435055]
[-6.05916633]
[-2.30157688]
[-1.19203431]
[-3.56722197]
[ 2.88433978]
[-3.26667433]
[ 0.59153899]
[ 3.78215766]
[ 1.44767692]
[-2.11225955]]
62]:
# Compute predictions
y_pred = X @ w + b
print("Predicted values:\n",y_pred)
62]:
Predicted values:
[[-2.12141872]
[-4.03630954]
[-1.75922084]
[ 0.56850582]
[-0.88221024]
[-0.16717785]
[-2.87782298]
[-1.39424082]
[-4.43495203]
[ 2.13166437]
[-0.80379409]
[ 3.56571321]
[-2.00495201]
[-0.93581462]
[ 3.35487336]
[ 1.0216122 ]
[ 2.10816543]
[-1.7685566 ]
[ 0.31521072]
[-0.99695742]
[ 0.45805938]
[ 2.84773597]
[ 2.36749221]
[ 0.33129572]
[-2.06739119]
[ 0.48900151]
[-0.31915366]
[-2.61998711]
[-0.93901223]
[ 0.85536835]
[ 1.37616059]
[-0.96609848]
[ 0.77857276]
[-1.08035874]
[ 1.69489777]
[-4.13429399]
[-1.29441148]
[-0.29381351]
[ 1.589714 ]
[ 1.30251655]
[-0.7546935 ]
[-2.6556254 ]
[ 2.03348837]
[-0.49614833]
[ 0.74491478]
[-1.10206863]
[-2.24464984]
[-0.14166044]
[-4.56352411]
[-2.7603116 ]
[ 0.18205807]
[ 0.94509963]
[-3.25210531]
[ 2.22782781]
[-2.58718026]
[-3.04816783]
[-1.17942569]
[-0.73044209]
[ 1.47592517]
[ 1.1307245 ]
[-0.32803267]
[ 2.90845024]
[ 1.428225 ]
[-2.91202392]
[ 0.13938201]
[ 1.30742982]
[-3.59243494]
[ 1.40691113]
[-1.66778417]
[ 1.38372429]
[-0.79751579]
[ 0.21407864]
[ 2.06406031]
[-0.48797107]
[-0.20503892]
[ 0.1917956 ]
[-0.12920066]
[ 1.07728441]
[ 3.59866766]
[ 4.42265593]
[ 0.93680072]
[-0.8894066 ]
[-2.25773365]
[ 2.36859176]
[ 0.25763254]
[-2.55447901]
[ 2.10704233]
[ 0.70045391]
[ 3.97713785]
[-0.12863228]
[-0.03928011]
[-1.1813281 ]
[-2.50958132]
[-2.80055371]
[ 2.06222903]
[-0.40290484]
[-1.20985149]
[ 0.86523708]
[ 0.78310097]
[-0.81262941]]
63]:
# Implement Mean Squared Error manually
mse = [Link]((y_true - y_pred) ** 2)
print("Manual Mean Squared Error:",mse)
63]:
Manual Mean Squared Error: 2.7921768788245624
64]:
# Validating Mean Squared Error using sklearn
print("Sklearn MSE:",mean_squared_error(y_true,y_pred))
64]:
Sklearn MSE: 2.7921768788245624
46]:
# Gradient Calculation for MSE
# Calculating number of data points
n = [Link][0]
# Gradient wrt weights
dw = (-2/n) * (X.T @ (y_true - y_pred))
# Gradient wrt bias
db = (-2/n) * ([Link](y_true - y_pred))
print("Gradient wrt weights:\n",dw)
print("\nGradient wrt bias:\n",db)
46]:
Gradient wrt weights:
[[-0.51243406]
[-2.39208641]
[ 1.61081182]]
Gradient wrt bias:
0.9165054465320326
Short Explanations for Results
Feature Matrix (X): A matrix with 100 samples and 3 features was generated using random values to simulate input data.
True Weights and Bias: Random values were chosen to create the target values.
Model Weights and Bias: Separate random values were initialized to represent the starting parameters of the model.
Target Values (y_true): Computed using the true weights and bias, with added Gaussian noise to make the dataset realistic.
Predicted Values (y_pred): Calculated using the model’s weights and bias, representing the model’s current guess.
Mean Squared Error (MSE):
Manual MSE was computed by averaging squared differences between predictions and true values.
Sklearn MSE matched the manual result, confirming the correctness of the manual formula.
Gradients for MSE:
Weights (dw): Show how much each weight contributes to the error and the direction to update them.
→
Negative weight should increase.
→
Positive weight should decrease.
Bias (db): Shows how the bias affects the error.
→
Positive bias should decrease.
→
Negative bias should increase.