-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrabalho1_am.py
410 lines (293 loc) · 12 KB
/
trabalho1_am.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# -*- coding: utf-8 -*-
"""Trabalho1_AM.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1yftwuiucE_hRhv_XJRm4MewqZ3BOMa6l
---
<p align="left">
<big>
<b>
<pre>
Camila Manara Ribeiro - RA: 760465
Júlia Aparecida Sousa de Oliveira - RA: 769707
Luciana Oliveira de Souza Gomes - RA: 743569
Rafael Vinicius Polato Passador - RA: 790036
</pre>
<br>
Disciplina: Aprendizado de Máquina
<br>
Professor: Prof. Dr. Diego Furtado Silva
<br>
</b>
</big>
</p>
---
<h1 align="center"><b><big>Projeto de Implementação 01- Classificação de nomes por gênero</big></b></h1>
#**Introdução**
<br>
Neste documento será apresentado o código utilizado para implementação do trabalho proposto: um problema representado por dados estruturados e que possa ser resolvido com algoritmos de AM supervisionado vistos em aula
Dessa forma, para realização desta tarefa, escolheu-se um conjunto de nomes rotuládos como masculinos e femininos que serviu como dataset para classificação de gênero, utilizando, para isso, diversas features que serão abordadas durante o código.
Assim sendo, serão apresentados os códigos utilizados, bem como as descrições das criações destes. Ademais, apresentam-se, também, as interpretações e documentações da problemática.
Link para a apresentação:
# IMPORT
"""
#Imports de bibliotecas necessárias que foram utilizadas durante a execução do código
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import f1_score, accuracy_score
from sklearn.metrics import confusion_matrix
"""# CARREGAR O DATASET
Carregamento do dataset com plot gráfico evidenciando o desbalanceamento entre classes
"""
dataset = pd.read_csv('name_gender_dataset.csv')
dataset.pivot_table(index='Gender', aggfunc='size').plot(kind='bar', title = 'Class distribution')
"""# RANDOM OVERSAMPLING
Como se tratava de um dataset com dados desbalanceados, admitiu-se uma estratégia para contornar esse possível entrave que possibilitaria uma classificação tendenciosa, uma vez que, o número de nomes considerados "femininos" era consideravelmente superior aos masculinos.
Nesse contexto, utiliza-mos uma função de Random OverSampling, em que, aleatoriamente, duplica-se exemplos da classe minoritária (nomes masculinos)
"""
def oversampler(dataset):
classes = dataset.Gender.value_counts().to_dict()
maior = max(classes.values())
classes_lista = []
for key in classes:
classes_lista.append(dataset[dataset['Gender'] == key])
classes_exemplo = []
for i in range(1,len(classes_lista)):
classes_exemplo.append(classes_lista[i].sample(maior, replace=True))
dataset_prov = pd.concat(classes_exemplo)
final_dataset = pd.concat([dataset_prov,classes_lista[0]], axis=0)
final_dataset = final_dataset.reset_index(drop=True)
return final_dataset
dataset = oversampler(dataset)
print(dataset)
dataset.head()
dataset.dtypes
M_data = dataset[dataset.Gender == 'M']
F_data = dataset[dataset.Gender == 'F']
print(M_data)
print(F_data)
M_nomes = M_data[M_data.columns[0]].tolist()
F_nomes = F_data[F_data.columns[0]].tolist()
"""Como podemos observar, a distribuição de dados entre as classes está, agora, balanceada"""
dataset.pivot_table(index='Gender', aggfunc='size').plot(kind='bar', title = 'Class distribution')
"""# TREINAMENTO TF-IDF
Nessa seção, apresentaremos o código do treinamento realizado a partir do valor de TF-IDF como feature.
O Tf–idf é uma medida estatística que tem o intuito de indicar a importância de uma palavra de um documento em relação ao dataset, ou seja, calcula-se um valor baseado na sua incidência e admite isso como feature.
Como classificador, utilizou-se o Naive Bayes.
"""
#Transformando os rótulos 'F' e 'M'
dataset_binario = dataset
dataset_binario.Gender.replace({'F':0,'M':1},inplace=True)
dataset_binario.Gender.unique()
#Vetorizando todos os nomes e extraindo seu valor de TF-IDF como feature, futuramente utilizada pelo Naive Bayes
Xfeatures = dataset_binario['Name']
convertor = TfidfVectorizer()
#Features
X = convertor.fit_transform(Xfeatures)
convertor.get_feature_names()
#Rotulos
y = dataset_binario.Gender
#Separando os dados entre treinamento e teste
X_treino, X_teste, y_treino, y_teste = train_test_split(X, y)
#Classificando o dataset a partir das features com o Naive Bayes
classifier = MultinomialNB()
classifier.fit(X_treino,y_treino)
final_results = classifier.predict(X_teste)
print(final_results)
#Calculando acurácia da amostra
print("Accuracy of Model",classifier.score(X_teste,y_teste)*100,"%")
print("Accuracy of Model",classifier.score(X_treino,y_treino)*100,"%")
print(y_teste)
print(final_results)
#Calculando o F1-Score da amotstra
print("F1: ", f1_score(y_teste, final_results))
conf_matrix = confusion_matrix(y_true=y_teste, y_pred=final_results)
#
# Print the confusion matrix using Matplotlib
#
fig, ax = plt.subplots(figsize=(5, 5))
ax.matshow(conf_matrix, cmap=plt.cm.Oranges, alpha=0.3)
for i in range(conf_matrix.shape[0]):
for j in range(conf_matrix.shape[1]):
ax.text(x=j, y=i,s=conf_matrix[i, j], va='center', ha='center', size='xx-large')
plt.xlabel('Predictions', fontsize=18)
plt.ylabel('Actuals', fontsize=18)
plt.title('Confusion Matrix', fontsize=18)
plt.show()
"""# TREINAMENTO COM EXTRAÇÕES DE LETRAS
Nessa seção, apresentaremos o código do treinamento realizado a partir da extração de features gramaticais.
Realizamos a testagem considerando sufixos, prefixos e a combinação de ambos para averiguar qual configuração apresentava melhores resultados e, posteriormente, evidencia-se as features mais relevantes
Como classificador, tambéum utilizou-se o Naive Bayes.
"""
#feature considerando o sufixo
def feature_sufixo(x):
return {'sufixo1': x[-1:],
'sufixo2': x[-2:],
}
feature_sufixo('Josephine')
#feature combinando prefixo e sufixo
def feature_prefixosufixo(x):
return {'sufixo1': x[-1:],
'sufixo2': x[-2:],
'prefixo': x[:2],
}
feature_prefixosufixo('Josephine')
#feature considerando o prefixo
def feature_prefixo(x):
return {
'prefixo': x[0],
'prefixo2': x[:2],
}
feature_prefixo('Josephine')
final_dataset = dataset.iloc[:,[0,1]]
dataset_features = final_dataset
print(dataset_features)
M_data2 = dataset_features[dataset_features.Gender == 1]
F_data2 = dataset_features[dataset_features.Gender == 0]
M_nomes2 = M_data2[M_data2.columns[0]].tolist()
F_nomes2 = F_data2[F_data2.columns[0]].tolist()
"""##Treinamento utilizando o prefixo dos nomes como features para o classificador"""
#Treinamento com as features sendo PREFIXO (rodar somente 1 dos blocos)
nomes_rotulados = ([(Name, 1) for Name in M_nomes2] + [(Name, 0) for Name in F_nomes2])
print(nomes_rotulados)
#random.shuffle(nomes_rotulados)
set_features = [(feature_prefixo(n), Gender) for (n, Gender) in nomes_rotulados]
set_treino, set_teste = set_features[50000:], set_features[:50000]
classifier = nltk.NaiveBayesClassifier.train(set_treino)
print(nltk.classify.accuracy(classifier, set_teste))
classifier.show_most_informative_features(15)
#Treinamento com as features sendo PREFIXO (rodar somente 1 dos blocos)
vetor = []
for (name, tag) in nomes_rotulados:
guess = classifier.classify(feature_prefixo(name))
vetor.append(guess)
print(vetor)
enumerate(vetor)
for i, item in enumerate(vetor):
if item == 'F':
vetor[i] = 0
elif item == 'M':
vetor[i] = 1
print(vetor)
lista_generos = [Gender for (n, Gender) in nomes_rotulados]
print(lista_generos)
enumerate(lista_generos)
for i, item in enumerate(lista_generos):
if item == 'F':
lista_generos[i] = 0
elif item == 'M':
lista_generos[i] = 1
print(lista_generos)
print("F1: ", f1_score(lista_generos, vetor))
conf_matrix = confusion_matrix(y_true=lista_generos, y_pred=vetor)
#
# Print the confusion matrix using Matplotlib
#
fig, ax = plt.subplots(figsize=(5, 5))
ax.matshow(conf_matrix, cmap=plt.cm.Oranges, alpha=0.3)
for i in range(conf_matrix.shape[0]):
for j in range(conf_matrix.shape[1]):
ax.text(x=j, y=i,s=conf_matrix[i, j], va='center', ha='center', size='xx-large')
plt.xlabel('Predictions', fontsize=18)
plt.ylabel('Actuals', fontsize=18)
plt.title('Confusion Matrix', fontsize=18)
plt.show()
"""##Treinamento utilizando a combinação prefixo e sufixo dos nomes como features para o classificador"""
#Treinamento com as features sendo PREFIXO+SUFIXO
nomes_rotulados = ([(Name, 1) for Name in M_nomes2] + [(Name, 0) for Name in F_nomes2])
print(nomes_rotulados)
#random.shuffle(nomes_rotulados)
set_features = [(feature_prefixosufixo(n), Gender) for (n, Gender) in nomes_rotulados]
set_treino, set_teste = set_features[500:], set_features[:500]
classifier = nltk.NaiveBayesClassifier.train(set_treino)
print(nltk.classify.accuracy(classifier, set_teste))
classifier.show_most_informative_features(15)
#Treinamento com as features sendo PREFIXO+SUFIXO
vetor = []
for (name, tag) in nomes_rotulados:
guess = classifier.classify(feature_prefixosufixo(name))
vetor.append(guess)
print(vetor)
enumerate(vetor)
for i, item in enumerate(vetor):
if item == 'F':
vetor[i] = 0
elif item == 'M':
vetor[i] = 1
print(vetor)
lista_generos = [Gender for (n, Gender) in nomes_rotulados]
print(lista_generos)
enumerate(lista_generos)
for i, item in enumerate(lista_generos):
if item == 'F':
lista_generos[i] = 0
elif item == 'M':
lista_generos[i] = 1
print(lista_generos)
print("F1: ", f1_score(lista_generos, vetor))
conf_matrix = confusion_matrix(y_true=lista_generos, y_pred=vetor)
#
# Print the confusion matrix using Matplotlib
#
fig, ax = plt.subplots(figsize=(5, 5))
ax.matshow(conf_matrix, cmap=plt.cm.Oranges, alpha=0.3)
for i in range(conf_matrix.shape[0]):
for j in range(conf_matrix.shape[1]):
ax.text(x=j, y=i,s=conf_matrix[i, j], va='center', ha='center', size='xx-large')
plt.xlabel('Predictions', fontsize=18)
plt.ylabel('Actuals', fontsize=18)
plt.title('Confusion Matrix', fontsize=18)
plt.show()
"""##Treinamento utilizando o sufixo dos nomes como features para o classificador"""
#Treinamento com as features sendo SUFIXO
nomes_rotulados = ([(Name, 1) for Name in M_nomes2] + [(Name, 0) for Name in F_nomes2])
print(nomes_rotulados)
#random.shuffle(nomes_rotulados)
set_features = [(feature_sufixo(n), Gender) for (n, Gender) in nomes_rotulados]
set_treino, set_teste = set_features[50000:], set_features[:50000]
classifier = nltk.NaiveBayesClassifier.train(set_treino)
print(nltk.classify.accuracy(classifier, set_teste))
classifier.show_most_informative_features(15)
#Treinamento com as features sendo SUFIXO
vetor = []
for (name, tag) in nomes_rotulados:
guess = classifier.classify(feature_sufixo(name))
vetor.append(guess)
print(vetor)
enumerate(vetor)
for i, item in enumerate(vetor):
if item == 'F':
vetor[i] = 0
elif item == 'M':
vetor[i] = 1
print(vetor)
lista_generos = [Gender for (n, Gender) in nomes_rotulados]
print(lista_generos)
enumerate(lista_generos)
for i, item in enumerate(lista_generos):
if item == 'F':
lista_generos[i] = 0
elif item == 'M':
lista_generos[i] = 1
print(lista_generos)
print("F1: ", f1_score(lista_generos, vetor))
conf_matrix = confusion_matrix(y_true=lista_generos, y_pred=vetor)
#
# Print the confusion matrix using Matplotlib
#
fig, ax = plt.subplots(figsize=(5, 5))
ax.matshow(conf_matrix, cmap=plt.cm.Oranges, alpha=0.3)
for i in range(conf_matrix.shape[0]):
for j in range(conf_matrix.shape[1]):
ax.text(x=j, y=i,s=conf_matrix[i, j], va='center', ha='center', size='xx-large')
plt.xlabel('Predictions', fontsize=18)
plt.ylabel('Actuals', fontsize=18)
plt.title('Confusion Matrix', fontsize=18)
plt.show()