-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_trainer.py
More file actions
71 lines (61 loc) · 1.92 KB
/
model_trainer.py
File metadata and controls
71 lines (61 loc) · 1.92 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
#%%
import pandas as pd
import numpy as np
import sklearn
import kaggle
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline
from pathlib import Path
import joblib
import os
#%%
# store model version
sklearn_version = sklearn.__version__
# %%
# download data from Kaggle in /datasets folder
dataset = 'ashishpatel26/sentimental-analysis-nlp'
file_name = "train_data.csv"
kaggle.api.dataset_download_file(dataset, file_name=file_name,
path=r'datasets')
#%%
# import data
sentim_data = pd.read_csv("datasets/train_data.csv", names = ["label", "text"],sep = "\t")
sentim_data.sample(10) # inspect sample
# %%
# assign features/target and ttsplit
X = sentim_data.text
y = sentim_data.label
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# %%
# instantiate tf-idf transformer obj
tfidf_vect = TfidfVectorizer(max_features=20)
# %%
# instantiate linearsvc model
lin_svc = LinearSVC(C=1., max_iter=1000, tol=1e-3)
#%%
# define pipeline
clf_pipeline = Pipeline(
[('tfidf_vect', tfidf_vect),
('classifier', lin_svc)]
)
pipeline_model = clf_pipeline.fit(X_train, y_train)
# %%
# evaluate classifier
y_pred = pipeline_model.predict(X_test)
test_accuracy = accuracy_score(y_test, y_pred)
# %%
# store job objects & metadata
text_clf_pipeline_param = {}
text_clf_pipeline_param['pipeline'] = pipeline_model
text_clf_pipeline_param['class labels'] = {0:"negative", 1: "positive"}
text_clf_pipeline_param['sklearn version'] = sklearn_version
text_clf_pipeline_param['test accuracy'] = test_accuracy
# %%
# create models dir if not present
Path(os.path.join(os.getcwd(),"models")).mkdir(parents=True, exist_ok=True)
# dump pipeline w metadata
out_filename = "models/pipe_clf_checkpoint.joblib"
joblib.dump(text_clf_pipeline_param, out_filename)