-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputBoW.py
75 lines (45 loc) · 1.42 KB
/
inputBoW.py
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
# -*- coding: utf-8 -*-
"""
COMP 551 A3
Author: Shatil Rahman
ID: 260606042
This module runs reads from the formatted and preprocessed data files,
and returns them as Binary Bag of Words or Frequency Bag of Words forms in sparse
csr_matrices (and the Y_set target values)
"""
from scipy.sparse import csr_matrix
import numpy as np
from sklearn import preprocessing
def loadBinBoW(datafile_name, n_samples, n_features):
data_file = open(datafile_name, 'r')
Y_set = []
examples = np.zeros((n_samples,n_features), dtype=int)
i = 0
for line in data_file:
X,Y = line.split("\t")
X = X.split()
Y = int(Y)
Y_set.append(Y)
for ID in X:
j = int(ID)
examples[i][j] = 1
i = i + 1
X = csr_matrix(examples)
return X, Y_set
def loadFreqBoW(datafile_name, n_samples, n_features):
data_file = open(datafile_name, 'r')
Y_set = []
examples = np.zeros((n_samples,n_features), dtype=float)
i = 0
for line in data_file:
X,Y = line.split("\t")
X = X.split()
Y = int(Y)
Y_set.append(Y)
for ID in X:
j = int(ID)
examples[i][j] = examples[i][j] + 1
i = i + 1
X = csr_matrix(examples)
preprocessing.normalize(X, norm='l1',axis=1,copy=False)
return X, Y_set