<jupyter_start><jupyter_text>Simple Linear Regression ( Minimal  example) Elements of the model in supervized learning:    1. inputs    2. weights    3. baises    4. outputs    5. targets  Import the relevant libraries<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D<jupyter_output><empty_output><jupyter_text>Generate random input data to train on<jupyter_code>observations = 1000

xs = np.random.uniform(low=-10,high=10,size=(observations,1))
zs = np.random.uniform(low=-10,high=10,size=(observations,1))

inputs = np.column_stack((xs,zs))
print(inputs.shape)<jupyter_output>(1000, 2)<jupyter_text>Create the targets we will aim at> Use function `targets= f(x,z) = 2*x -3*z +5+noise`<jupyter_code>noise = np.random.uniform(-1,1,(observations,1))
targets = 2*xs - 3*zs + 5 +noise
print(targets.shape)<jupyter_output>(1000, 1)<jupyter_text>Plot the  training dataThe plot is to see that there is a strong trend that our model should learn to reproduce.<jupyter_code>targets = targets.reshape(observations,)
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(111, projection='3d')
ax.plot(xs,zs,targets)
ax.set_xlabel('xs')
ax.set_ylabel('zs')
ax.set_zlabel('targets')
ax.view_init(azim=100)
plt.show()
targets = targets.reshape(observations,1)<jupyter_output><empty_output><jupyter_text>Initialize variables<jupyter_code>init_range = 0.1

weights = np.random.uniform(-init_range,init_range, size=(2,1))

biases = np.random.uniform(-init_range, init_range, size=1)

print(weights)
print(biases)<jupyter_output>[[0.08486642]
 [0.02119972]]
[0.01795859]<jupyter_text>Set a learning rate<jupyter_code>learning_rate = 0.02<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>for i in range(200):
    output = np.dot(inputs, weights) + baises
    deltas = output - targets
    
    loss = np.sum(deltas**2)/2/observations
    
    print(loss)
    
    deltas_scaled = deltas / observations
    
    weights = weights - learning_rate * np.dot(inputs.T,deltas_scaled)
    biases = biases - learning_rate * np.sum(deltas_scaled)<jupyter_output>207.9350688089806
25.889624420199052
3.3522324162571744
0.5615630495063927
0.21596222394385367
0.17315785007873796
0.1678558792900589
0.16719910856081782
0.16711774854501557
0.1671076693909431
0.16710642071657938
0.1671062660190119
0.16710624685330855
0.16710624447881295
0.1671062441846269
0.1671062441481788
0.16710624414366299
0.16710624414310352
0.16710624414303418
0.16710624414302563
0.16710624414302452
0.16710624414302436
0.1671062441430244
0.16710624414302444
0.16710624414302447
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.16710624414302439
0.16710624414302439
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.16710624414302436
0.16710624414302439
0.16710624414302444
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441430244
0.1671062441[...]<jupyter_text>Print weights and biases and see if we have worked correctly<jupyter_code>print(weights, baises)<jupyter_output>[[ 2.00408548]
 [-3.00187395]] [4.91199414]<jupyter_text>Plot last outputs vs targetsSince they are the last ones at end of the training, they represent the final model accuracy The closer this plot is to 45 degree line, the closertarget and output values are.<jupyter_code>plt.plot(output, targets)
plt.xlabel('output')
plt.ylabel('targets')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10333425186001653, 'fraction_numerical': 0.28051804904932487, 'mean_word_length': 6.626050420168068, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '493323', 'license': 'MIT', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1432, 'n_words': 254, 'path': 'Simple Linear Regression. Minimal  example.ipynb', 'repo_name': 'rsanimesh/Udemy-Complete-Data-Science-Bootcamp', 'size': '3629'}