csci5451/assignments/01/main.py

38 lines
864 B
Python
Raw Normal View History

2023-10-08 00:49:58 +00:00
# import numexpr as ne
import numpy as np
with open("dataset/small_data.csv", "r") as f:
desc = f.readline().strip()
rows, dimensions = map(int, desc.split(" "))
data = np.loadtxt(f)
print("loaded data")
with open("dataset/small_label.csv", "r") as f:
desc = f.readline().strip()
rows = int(desc)
labels = np.loadtxt(f)
print("loaded labels")
print(data.shape)
print(labels.shape)
2023-10-09 07:51:38 +00:00
w = np.zeros(dimensions)
print(w)
np.set_printoptions(precision=3)
2023-10-08 00:49:58 +00:00
for _ in range(10):
2023-10-09 07:51:38 +00:00
new_w = np.empty(w.shape)
2023-10-08 00:49:58 +00:00
for i in range(dimensions):
data_ni = np.delete(data, i, axis=1)
w_ni = np.delete(w, i)
res = data_ni @ w_ni
x_i = data[:,i]
2023-10-09 07:51:38 +00:00
numer = x_i.transpose() @ (labels - res)
2023-10-08 00:49:58 +00:00
denom = x_i.transpose() @ x_i
new_w[i] = numer / denom
2023-10-09 07:51:38 +00:00
print("new_w", np.round(new_w, 3))
w = new_w