FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NLP-Algorithms/Algorithms/Text_Classification/train.py at main · UTSAVS26/NLP-Algorithms · GitHub
UTSAVS26
/
NLP-Algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NLP-Algorithms
/
Algorithms
/
Text_Classification
/
train.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
34 lines (27 loc) · 1.04 KB
Breadcrumbs
NLP-Algorithms
/
Algorithms
/
Text_Classification
/
train.py
Copy path
File metadata and controls
34 lines (27 loc) · 1.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
pandas
as
pd
from
sklearn
.
feature_extraction
.
text
import
TfidfVectorizer
from
sklearn
.
model_selection
import
train_test_split
from
sklearn
.
naive_bayes
import
MultinomialNB
from
sklearn
.
pipeline
import
Pipeline
from
sklearn
.
metrics
import
accuracy_score
,
classification_report
import
joblib
from
preprocess
import
preprocess_text
# Load dataset
data
=
pd
.
read_csv
(
'data/text_data.csv'
)
# Preprocess text data
data
[
'text'
]
=
data
[
'text'
].
apply
(
preprocess_text
)
# Split dataset into training and testing sets
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
data
[
'text'
],
data
[
'label'
],
test_size
=
0.2
,
random_state
=
42
)
# Create a pipeline with TfidfVectorizer and MultinomialNB
model
=
Pipeline
([
(
'tfidf'
,
TfidfVectorizer
()),
(
'nb'
,
MultinomialNB
())
])
# Train the model
model
.
fit
(
X_train
,
y_train
)
# Evaluate the model
y_pred
=
model
.
predict
(
X_test
)
print
(
f'Accuracy:
{
accuracy_score
(
y_test
,
y_pred
)
}
'
)
print
(
f'Classification Report:
\n
{
classification_report
(
y_test
,
y_pred
)
}
'
)
# Save the model
joblib
.
dump
(
model
,
'models/text_classifier.pkl'
)
Back
|
FazBrowse Home
|
New Git URL