csci5451/assignments/01/main.py

38 lines
No EOL
864 B
Python

# 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)
w = np.zeros(dimensions)
print(w)
np.set_printoptions(precision=3)
for _ in range(10):
new_w = np.empty(w.shape)
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]
numer = x_i.transpose() @ (labels - res)
denom = x_i.transpose() @ x_i
new_w[i] = numer / denom
print("new_w", np.round(new_w, 3))
w = new_w