-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
76 lines (59 loc) · 2.35 KB
/
main.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
#members
#utkarsh kumar- [email protected]
# Loading the required packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('fivethirtyeight')
import warnings
warnings.filterwarnings('ignore')
import re
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression, LogisticRegressionCV
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score, f1_score
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.svm import LinearSVC
import nltk
nltk.download('stopwords')
# %matplotlib inline
from sklearn.pipeline import Pipeline
# Loading dataset
df = pd.read_excel('Scraped_Data.xlsx')
df.shape
# Checking for null values
df.isnull().sum()
# Removing rows with null values
df = df[df.category.isnull() == False]
df.shape
# Converting caption to string
df['caption'] = df['caption'].astype(str)
# Cleaning the text data
stemmer = PorterStemmer()
words = stopwords.words("english")
df['caption'] = df['caption'].apply(lambda x: " ".join([stemmer.stem(i) for i in re.sub("[^a-zA-Z0-9]", " ", x).split() if i not in words]).lower())
vectorizer = TfidfVectorizer(min_df=3, stop_words="english", sublinear_tf=True, norm='l2', ngram_range=(1, 2))
# Extracting 1000 best features from the text data
pipe = Pipeline([('vect', vectorizer), ('chi', SelectKBest(chi2, k=1000))])
a = pipe.fit_transform(df['caption'], df['category'])
text_features = a.toarray()
text_features.shape
df['category'] = pd.Categorical(df['category'])
# Creating training and test datasets
X = text_features
y = df['category']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
# Fitting the model
model = LinearSVC(class_weight='balanced', max_iter=5000)
model.fit(X_train, y_train)
# Performing prediction on test dataset
preds = model.predict(X_test)
print('Final prediction score: [%.8f]' % accuracy_score(y_test, preds))
print('Final prediction f1 score: [%.8f]' % f1_score(y_test, preds, average='weighted'))
# Printing classification report and confusion matrix
print(classification_report(y_test, preds))
print(confusion_matrix(y_test, preds))