41 lines
2.5 KiB
Python
Executable file
41 lines
2.5 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
x = np.array([140.02910671010955,144.28305637655325,115.17216965047129,174.2908725140191,133.71872589985287,105.10115631256336,128.83111783663017,52.49511578021336,89.2047261676914,135.82255987404903,87.71008893735774,130.3420057698494,105.7399311229496,67.61162834891853,88.776702374284,124.09543839116444,126.81345896226252,124.66228235044524,134.91610658142736,134.77342510021955,106.08439660641662,166.5510574606382,97.78304310622656,106.66244028453148,165.95003615203737,154.80521857599115,141.5055158602476,63.02949535059915,169.53273978680755,142.85136894138446,179.85011085382266,69.42827960553919,162.85638745786787,71.12408150792874,130.02274335689975,66.0965977704274,160.24106981189047,154.9514646342514,123.98309601989713,102.93382863937995,58.991709409167946,140.66574050879328,108.96054874814895,143.86722793114524,162.6297023707218,176.81779565037516,161.25443451103945,51.522830944050256,96.7971483821873,144.89877311512754,])
|
|
y = np.array([42.81605146528971,36.28330545155218,31.68572329595792,76.93511586659231,39.67091689151953,36.75433594698061,43.668607504626735,35.36147584081617,37.15391917695597,54.600834054029775,39.53711133759362,40.70788477452493,31.238140682578088,32.803846883928486,33.26497225482958,32.23737408256554,26.644077012420333,35.07966603555466,27.18589689633954,34.824784598255725,30.995750167590458,87.49142265447308,35.718581012669205,32.94444212403231,89.28741953891095,38.377360047677826,47.934251797831806,33.86693859748784,72.15159937429533,52.39883472306155,113.05473219321803,32.358600372044044,71.84653139192467,36.770050235085684,34.448886069864834,35.7827916825874,59.57702285934876,61.263901673061625,35.90307044444734,36.06547033199114,34.36729122240489,48.69663447942126,35.42784626944333,34.556197150504495,51.345881947334895,82.19073242621832,50.649674506375675,34.96619043312894,38.77250612852849,48.018188311243144,])
|
|
N = len(x)
|
|
|
|
#
|
|
# step 04
|
|
#
|
|
|
|
|
|
X = np.ones((N, 2))
|
|
X[:, 1] = x
|
|
|
|
w = np.linalg.inv(X.T @ X) @ X.T @ y
|
|
|
|
plt.figure()
|
|
plt.scatter(x, y)
|
|
|
|
x_min = np.min(x)
|
|
x_max = np.max(x)
|
|
x_plot = np.linspace(x_min, x_max, 100)
|
|
y_plot = w[0] + w[1] * x_plot
|
|
plt.plot(x_plot, y_plot, color='orange')
|
|
|
|
prediction = w[0] + w[1] * 120
|
|
print(f'f(120) = {prediction}')
|
|
|
|
y_pred = X @ w
|
|
rmse = np.sqrt(np.sum((y - y_pred) ** 2) / N)
|
|
print(f'RMSE = {rmse}')
|
|
|
|
idx = np.argmax(np.abs(y - y_pred))
|
|
max_error = np.abs(y[idx] - y_pred[idx])
|
|
print(f'Maximal error = {max_error}')
|
|
print(f'for (x, y) = ({x[idx]}, {y[idx]})')
|
|
|
|
plt.show()
|