-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (43 loc) · 1.47 KB
/
main.py
File metadata and controls
60 lines (43 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Task 1b
import numpy as np
import pandas as pd
# This task is about linear regression: given an input vector x, your goal is to
# predict a value y as a linear function of a set of feature transformations, phi(x)
df = pd.read_csv('C:/Users/Lannan Jiang/PycharmProjects/IML_Task1b/train.csv')
X_train = np.asarray(df.loc[:, 'x1':'x5'])
y_train = np.asarray(df.loc[:, 'y']).reshape(X_train.shape[0], 1)
Id = np.asarray(df.loc[:, 'Id'])
# Step 1: compute phi
# Step 2: use the transformed features to fit the model to find the weight vector
# quadratic
def generate_quadratic(X, degree=2):
X = X ** degree
return X
# Exponential
def generate_exponential(X):
X = np.exp(X)
return X
# Cosine
def generate_cosine(X):
X = np.cos(X)
return X
# linear is just linear
# phi size: 700x21
phi = np.zeros((X_train.shape[0], 21))
# compute phi
for j in range(0, phi.shape[0]):
if j < 5:
phi[:, j] = X_train[:, j]
elif 5 <= j < 10:
phi[:, j] = generate_quadratic(X_train[:, j-5])
elif 10 <= j < 15:
phi[:, j] = generate_exponential(X_train[:, j-10])
elif 15 <= j < 20:
phi[:, j] = generate_cosine(X_train[:, j-15])
elif j == 20:
phi[:, j] = np.ones(X_train.shape[0])
weights = np.linalg.solve(np.dot(phi.T, phi), np.dot(phi.T, y_train))
# write to csv
temp_df = pd.DataFrame(weights)
file = 'C:/Users/Lannan Jiang/PycharmProjects/IML_Task1b/submission.csv'
pd.DataFrame(temp_df).to_csv(file, index=False, header=False)