-
Notifications
You must be signed in to change notification settings - Fork 2
/
final_pre_processing.py
159 lines (73 loc) · 2.55 KB
/
final_pre_processing.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from keras_preprocessing.text import text_to_word_sequence, Tokenizer
from nltk.tokenize import WordPunctTokenizer
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import seaborn as sns
from collections import OrderedDict
# ## File path
# In[2]: User Setting
file_path = 'C:/Users/SJH/OneDrive - korea.ac.kr/문서/MBTI 500.csv'
USER_SAMPLE = 1
# ## Date Load & check
# In[3]:
data = pd.read_csv(file_path, encoding = "UTF-8")
# In[4]:
if USER_SAMPLE :
data = data.groupby('type').sample(frac = 0.2)
data.reset_index(drop = True, inplace = True)
# In[5]:
X, y = data.drop('type', axis = 1), data['type']
# ## Distribution of label and so on
# In[6]:
data.info()
# In[7]:
data.isnull().sum()
# In[8]:
print(X.nunique(), len(X)) # Same
# In[9]:
sns.countplot(x='type', data=data, order=y.value_counts().index)
plt.show()
count = data.groupby('type').count()
count.sort_values(by=['posts'], ascending=False)
print(count, y.value_counts(normalize=True), sep = '\n======================\n')
# ## Top 3000 words
# In[10]:
tokenizer_top_words = Tokenizer(oov_token="<OOV>", split=' ', num_words=3000)
tokenizer_top_words.fit_on_texts(X.iloc[:, 0])
# In[11]:
# only 3000 words encoding
# tmp = X.head(10)
X_tp_words = X.copy()
X_tp_words['tok_tw'] = X_tp_words.apply(lambda v : tokenizer_top_words.texts_to_sequences([v['posts']]), axis = 1)
# In[12]:
X_tp_words['tok_tw'] = X_tp_words.apply(lambda v: np.array(v['tok_tw']).reshape(-1, 1).tolist(), axis = 1)
# In[13]:
X_tp_words['tok_tw_bool']=X_tp_words.apply(lambda v: list(map(lambda t: int(t[0] > 1), v['tok_tw'])), axis = 1)
# ## Words used more than 1000 times
# In[14]:
word_dict = tokenizer_top_words.word_counts
word_dict = OrderedDict(sorted(word_dict.items(), key = lambda t : t[-1],reverse= True))
# In[15]:
word_dict_top = []
for i, (key, value) in enumerate(word_dict.items()) :
if value >= 1000 :
word_dict_top.append(key)
print(f"size is {len(word_dict_top)}")
# In[16]:
# Boolean encoding
X_freq_words = X.copy()
X_freq_words['tok_tw'] = X_freq_words.apply(lambda v : WordPunctTokenizer().tokenize(v['posts']), axis = 1)
# In[17]:
X_freq_words['tok_tw_bool'] = X_freq_words.apply(lambda row : [1 if x in row['tok_tw'] else 0 for x in word_dict_top], axis = 1)
# In[ ]:
X_freq_words['tok_tw_in'] = X_freq_words.apply(lambda row : [1 if x in word_dict_top else 0 for x in row['tok_tw']], axis = 1)
# ## Dataframe print
# In[ ]:
X_tp_words
# In[ ]:
X_freq_words