-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknn.py
More file actions
104 lines (87 loc) · 3.8 KB
/
Copy pathknn.py
File metadata and controls
104 lines (87 loc) · 3.8 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import numpy as np
from sklearn.impute import KNNImputer
import matplotlib.pyplot as plt
from utils import (
load_valid_csv,
load_public_test_csv,
load_train_sparse,
sparse_matrix_evaluate,
)
def knn_impute_by_user(matrix, valid_data, k):
"""Fill in the missing values using k-Nearest Neighbors based on
student similarity. Return the accuracy on valid_data.
See https://scikit-learn.org/stable/modules/generated/sklearn.
impute.KNNImputer.html for details.
:param matrix: 2D sparse matrix
:param valid_data: A dictionary {user_id: list, question_id: list,
is_correct: list}
:param k: int
:return: float
"""
nbrs = KNNImputer(n_neighbors=k)
# We use NaN-Euclidean distance measure.
mat = nbrs.fit_transform(matrix)
acc = sparse_matrix_evaluate(valid_data, mat)
print("Validation Accuracy: {}".format(acc))
return acc
def knn_impute_by_item(matrix, valid_data, k):
"""Fill in the missing values using k-Nearest Neighbors based on
question similarity. Return the accuracy on valid_data.
:param matrix: 2D sparse matrix
:param valid_data: A dictionary {user_id: list, question_id: list,
is_correct: list}
:param k: int
:return: float
"""
#####################################################################
# TODO: #
# Implement the function as described in the docstring. #
#####################################################################
nbrs = KNNImputer(n_neighbors=k)
# Transpose the matrix to perform item-based imputation
mat = nbrs.fit_transform(matrix.T).T
acc = sparse_matrix_evaluate(valid_data, mat)
print("Validation Accuracy (Item-based): {}".format(acc))
#####################################################################
# END OF YOUR CODE #
#####################################################################
return acc
def main():
sparse_matrix = load_train_sparse("./data").toarray()
val_data = load_valid_csv("./data")
test_data = load_public_test_csv("./data")
print("Sparse matrix:")
print(sparse_matrix)
print("Shape of sparse matrix:")
print(sparse_matrix.shape)
#####################################################################
# TODO: #
# Compute the validation accuracy for each k. Then pick k* with #
# the best performance and report the test accuracy with the #
# chosen k*. #
#####################################################################
ks = [1, 6, 11, 16, 21, 26]
val_accuracies_user = []
val_accuracies_item = []
for k in ks:
print(f"Running k-NN with k={k}")
val_acc = knn_impute_by_user(sparse_matrix, val_data, k)
val_accuracies_user.append(val_acc)
print(f"Running item-based k-NN with k={k}")
val_acc_item = knn_impute_by_item(sparse_matrix, val_data, k)
val_accuracies_item.append(val_acc_item)
plt.figure()
plt.plot(ks, val_accuracies_user, marker="o", label="User-based")
plt.plot(ks, val_accuracies_item, marker="o", label="Item-based")
plt.xlabel("k")
plt.ylabel("Validation Accuracy")
plt.title("Validation Accuracy for Different k")
plt.legend()
plt.grid(True)
plt.show()
return
#####################################################################
# END OF YOUR CODE #
#####################################################################
if __name__ == "__main__":
main()