-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfClassifierModel.py
More file actions
83 lines (55 loc) · 2.11 KB
/
PdfClassifierModel.py
File metadata and controls
83 lines (55 loc) · 2.11 KB
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
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report, precision_score
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from functions.file_to_text import pdf_to_text
import warnings
import pandas as pd
import joblib
warnings.filterwarnings("ignore", category=DeprecationWarning)
csv_file_path = 'final_dataset.csv'
df = pd.read_csv(csv_file_path)
custom_category_order = {
'Legal': 0,
'Medical': 10,
'Finance': 80,
'Education': 40,
'Business': 50,
'News': 90,
'Technical': 30,
'Creative': 20,
'Scientific': 60,
'Government': 70,
}
start_value = 0
for category in df['Category'].unique():
custom_category_order[category] = start_value
start_value += 10
df['CategoryEncoded'] = df['Category'].map(custom_category_order)
train_data, test_data, train_labels, test_labels = train_test_split(
df["Text"], df['CategoryEncoded'], test_size=0.25, random_state=60
)
text_clf = Pipeline([
('tfidf', TfidfVectorizer()),
('clf', MultinomialNB())
])
parameters = {
'tfidf__ngram_range': [(1, 1), (1, 2)],
'clf__alpha': [0.1, 0.5, 1.0],
}
grid_search = GridSearchCV(text_clf, parameters, cv=5, n_jobs=-1)
grid_search.fit(train_data, train_labels)
best_model = grid_search.best_estimator_
best_model.fit(train_data, train_labels)
predictions = best_model.predict(test_data)
predicted_categories = [next(key for key, value in custom_category_order.items() if value == label) for label in predictions]
accuracy = accuracy_score(test_labels, predictions) * 100
precision = precision_score(test_labels, predictions, average='weighted') * 100
classification_rep = classification_report(test_labels, predictions)
print(f"Accuracy: {accuracy:.2f}%")
print(f"Precision: {precision:.2f}%")
print("\nClassification Report:")
print(classification_rep)
joblib.dump(best_model, 'best_model.pkl')