-
Notifications
You must be signed in to change notification settings - Fork 0
/
poem_classification.py
195 lines (132 loc) · 5.07 KB
/
poem_classification.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
# -*- coding: utf-8 -*-
"""Poem_classification.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1KQSJr8fOS6Onv97j5zV_IbrjS8uPjOAW
"""
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# %matplotlib inline
#Loading Data
data = pd.read_csv('poem_data.csv')
data.head()
data.info()
data.columns
#Count of Each Genre
data['Genre'].value_counts()
sns.countplot(data['Genre'])
#Spacy for textual Preprocessing
import spacy
nlp = spacy.load('en_core_web_sm')
import string
punc = string.punctuation + '“”|”'
def remove_stop_words(text):
doc = nlp(text)
return " ".join([token.lemma_ for token in doc if not token.is_stop])
def to_lower(text):
return text.lower()
def remove_punc(text):
for i in punc:
text = text.replace(i,"")
return text
# Converting poem to lowercase
data['Poem'] = data['Poem'].apply(to_lower)
# Removing Punctuations
data['Poem'] = data['Poem'].apply(remove_punc)
data.head()
# Tokenization and Lemmatization
data['Tokens'] = data['Poem'].apply(remove_stop_words)
data.head()
# EXtracting features and targets
X = data['Tokens']
y = data['Genre']
#Checking Null values
data.isnull().sum()
from sklearn.model_selection import train_test_split
# Splitting Data into training and testing data
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.25,random_state = 100)
# Importing required Libraries
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.preprocessing import FunctionTransformer
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
X_train[0]
# Grid Search CV to find the best parameters
def svc_param_selection(X, y, nfolds):
Cs = [0.001, 0.01, 0.1, 1, 10]
gammas = [0.001, 0.01, 0.1, 1]
param_grid = {'C': Cs, 'gamma' : gammas}
grid_search = GridSearchCV(SVC(kernel = 'linear'), param_grid, cv=nfolds)
grid_search.fit(X, y)
grid_search.best_params_
return grid_search.best_params_
# using TFIDF on X_train
vec = TfidfVectorizer(min_df=2,max_df=0.95)
X_train = vec.fit_transform(X_train)
# Finding best parameter for SVC()
svc_param_selection(X_train,y_train,20)
# Again Random Split of Data into train and test
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.25,random_state = 100)
# Creating Pipeline -->
poem_clf = Pipeline([
("Tfidf", TfidfVectorizer(min_df = 2,max_df = 0.95)),
("ToDense",FunctionTransformer(lambda x: x.todense(), accept_sparse=True)),
("Classifier",LogisticRegression(penalty = 'l1',C = 1))
])
# Fitting Poem_clf pipeline with data
poem_clf.fit(X_train,y_train)
pred = poem_clf.predict(X_test)
from sklearn.metrics import accuracy_score,confusion_matrix
# Confusion Matrix
print(confusion_matrix(y_test,pred))
print(f"Accuracy for Logistic Regression is: {accuracy_score(y_test,pred) * 100}%")
poem_clf2 = Pipeline([
("Tfidf", TfidfVectorizer(min_df = 2,max_df = 0.95)),
("ToDense",FunctionTransformer(lambda x: x.todense(), accept_sparse=True)),
("Classifier",SVC(kernel = 'linear'))
])
poem_clf2.fit(X_train,y_train)
pred2 = poem_clf2.predict(X_test)
print(confusion_matrix(y_test,pred2))
print(f"Accuracy for SVC is: {round(accuracy_score(y_test,pred2) * 100,3)}%")
"""So we see Logistic Regression is better in this case
Let's try to do topic modelling using Latent Dirichlet Allocation
"""
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.25,random_state = 100)
X_train.head()
# Vectorization-->
vect = CountVectorizer(min_df = 2,max_df = 0.95,stop_words = 'english')
dtm = vect.fit_transform(X_train)
dtm2 = vect.transform(X_test)
#n_components = 4 for 4 genres
LDA = LatentDirichletAllocation(n_components = 4)
ans = LDA.fit_transform(dtm)
ans2 = LDA.transform(dtm2)
ans.shape
# Prediction of classes according to LDA
Y_train = ans.argmax(axis = 1)
Y_test = ans2.argmax(axis = 1)
poem_LDA_clf = Pipeline([
("Tfidf", TfidfVectorizer(min_df = 2,max_df = 0.95)),
("ToDense",FunctionTransformer(lambda x: x.todense(), accept_sparse=True)),
("Classifier",SVC(gamma = 0.0001,kernel = 'linear'))
])
poem_LDA_clf.fit(X_train,Y_train)
pred3 = poem_LDA_clf.predict(X_test)
print(f'Accuracy using SVC is: {accuracy_score(Y_test,pred3) * 100}%')
poem_LDA_clf2 = Pipeline([
("Tfidf", TfidfVectorizer(min_df = 2,max_df = 0.95)),
("ToDense",FunctionTransformer(lambda x: x.todense(), accept_sparse=True)),
("Classifier",LogisticRegression(penalty = 'l2'))
])
poem_LDA_clf2.fit(X_train,Y_train)
pred4 = poem_LDA_clf2.predict(X_test)
print(f"Accuracy for Logistic Regression is: {accuracy_score(Y_test,pred4) * 100}%")