-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_selection.py
60 lines (40 loc) · 1.8 KB
/
feature_selection.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
#!/usr/bin/env python
"""
This script loads the feature dataframe, the classifications, and preforms feature selection and splits the data.
input: words.pd, data.csv
output: x_train.npy, x_test.npy, y_train.npy, y_test.npy
"""
# things to do:
# for train and test, run selectKbest scikit feature selection
# split data into data (x_train) and the pos/neg classification (y_train)
import sys, os
import numpy as np
import pandas as pd
num_feats = 1000
words_df = pd.read_pickle('words.df')
data_df = pd.read_csv('data.csv')
words_matrix = words_df.values
x_train = words_matrix[0:500] # first 500 are train
x_test = words_matrix[500:1000] # last 500 are test
y_train = np.zeros((500))
y_train[250:500] = 1
y_test = y_train
assert(np.sum(y_train)==250 and np.sum(y_test)==250)
# now we have x_train, x_test, y_train, y_test we can do feature selection
from sklearn.model_selection import StratifiedKFold, StratifiedShuffleSplit
from sklearn.feature_selection import SelectKBest, f_classif
feats = np.asarray(list(words_df.columns.values)).reshape(1,-1)
# first we make a feature selection object
feat_select_object = SelectKBest(f_classif, k=num_feats)
# now we learn which features are best, while removing unimportant once
x_train = feat_select_object.fit_transform(x_train, y_train)
# now we reduce the x_test to the same features as x_train, note that the feat_select_object never saw the testing set when choosing what features to keep
x_test = feat_select_object.transform(x_test)
# we also want to keep track of which words the features are
feats = feat_select_object.transform(feats)
# now save everything
np.save('data_sets/x_train.npy', x_train)
np.save('data_sets/y_train.npy', y_train)
np.save('data_sets/x_test.npy', x_test)
np.save('data_sets/y_test.npy', y_test)
np.save('data_sets/feats.npy', feats[0])