-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintrovert_or_extrovert.py
154 lines (102 loc) · 3.8 KB
/
introvert_or_extrovert.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
# -*- coding: utf-8 -*-
"""Introvert or Extrovert.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1zDMXThpUMCjkPj36nSb633HnSp3_Gq1F
"""
import numpy as np
import pandas as pd
from nltk.corpus import stopwords
import nltk
nltk.download('stopwords')
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import train_test_split
import tensorflow as tf
data = pd.read_csv(r"/content/mbti_1.csv")
data
data.info()
"""# Data Preprocessing"""
data['type'].unique()
# Shows unique values
def preprocess_inputs(df):
texts = df['posts'].copy()
labels = df['type'].copy()
# Process text data
# Stopwords are the English words which does not add much meaning to a sentence
stop_words = stopwords.words('english')
texts = [text.lower() for text in texts]
texts = [text.split() for text in texts]
texts = [[word.strip() for word in text] for text in texts]
texts = [[word for word in text if word not in stop_words]for text in texts]
vocab_length = 1000
tokenizer = Tokenizer(num_words = vocab_length)
tokenizer.fit_on_texts(texts)
texts = tokenizer.texts_to_sequences(texts)
# Creating a reverse dictionary
reverse_word_map = dict(map(reversed, tokenizer.word_index.items()))
# Function takes a tokenized sentence and returns the words
def sequence_to_text(list_of_indices):
# Looking up words in dictionary
words = [reverse_word_map.get(letter) for letter in list_of_indices]
return(words)
# Creating texts
my_texts = list(map(sequence_to_text, texts))
max_seq_length = np.max([len(text) for text in texts])
texts = pad_sequences(texts, maxlen = max_seq_length, padding = 'post')
# Process label data
label_values = [
'INFJ', 'ENTP', 'INTP', 'INTJ', 'ENTJ', 'ENFJ', 'INFP', 'ENFP',
'ISFP', 'ISTP', 'ISFJ', 'ISTJ', 'ESTP', 'ESFP', 'ESTJ', 'ESFJ'
]
label_mapping = {label:np.int(label[0] == 'E') for label in label_values}
labels = labels.replace(label_mapping)
labels = np.array(labels)
return texts, labels, max_seq_length, vocab_length, label_mapping, my_texts
texts, labels, max_seq_length, vocab_length, label_mapping, my_texts = preprocess_inputs(data)
print("Text sequences:\n", texts.shape)
print("\nLabels:\n", labels.shape)
print("\nMax sequence length:\n", max_seq_length)
print("\nVocab length:\n", vocab_length)
print("\nLabel mapping:\n", label_mapping)
texts_train, texts_test, labels_train, labels_test = train_test_split(texts, labels, train_size=0.7, random_state=123)
texts
"""# Training"""
embedding_dim = 512
inputs = tf.keras.Input(shape=(max_seq_length,))
embedding = tf.keras.layers.Embedding(
input_dim = vocab_length,
output_dim = embedding_dim,
input_length = max_seq_length)(inputs)
gru = tf.keras.layers.Bidirectional(
tf.keras.layers.GRU(
units=256,
return_sequences=True
)
)(embedding)
flatten = tf.keras.layers.Flatten()(gru)
outputs = tf.keras.layers.Dense(1, activation='sigmoid')(flatten)
model = tf.keras.Model(inputs, outputs)
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=[
'accuracy',
tf.keras.metrics.AUC(name='auc')
]
)
history = model.fit(
texts_train,
labels_train,
validation_split=0.2,
batch_size=32,
epochs=5,
callbacks=[
tf.keras.callbacks.ModelCheckpoint('./model.h5', save_best_only=True, save_weights_only=True)
]
)
model.load_weights('./model.h5')
model.evaluate(texts_test, labels_test)
predicted_arr = model.predict(texts[:10])
for i in range(0,10):
print(predicted_arr[i]*100,"==>", my_texts[i])