forked from 100rabh1401/VenmoPlugDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
115 lines (94 loc) · 3.2 KB
/
analysis.py
File metadata and controls
115 lines (94 loc) · 3.2 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
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
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import GaussianNB
import _pickle as cPickle
from emoji_handling import emoji_ratio, emoji_ohe
bag_of_words = ["alc", "alcohol", "bubbly", "champagne", "drinks", "beer", "bud", "drank", "weed", "pills", "ecstasy", "broccoli", "plug", "codeine", "high", "buzzed", "stoned", "420", "smoke", "popper", "pods", "pod", "juul", "suorin", "vape", "vaping", "vape"]
bad_fp = "data/illicit.txt"
good_fp = "data/clean.txt"
MODEL_FP = "bayesianNB.pkl"
'''
[INPUT] list of unlabeled observations
[OUTPUT] list of data to render on website
'''
def unlabeled_runner(observations):
features = bow_featurizer(observations)
model = retrieve_model(MODEL_FP)
predictions = model.predict(features)
return predictions
'''
[INPUT] filepath to where model should be pickled
[OUTPUT] none, writes a pickled logistic regression model
'''
def model_runner(filepath):
df = read_training_data(bad_fp, good_fp)
X, Y = training_pipeline(df)
model = generate_model(X, Y)
write_model(model, filepath)
'''
[INPUT] filepaths of illicit and clean newline-delimited training data
[OUTPUT] dataframe containing payment texts and labels
'''
def read_training_data(bad_filepath, good_filepath):
bad_data = pd.read_csv(bad_filepath, sep="\n", header=None)
good_data = pd.read_csv(good_filepath, sep="\n", header=None)
bad_data["label"] = pd.Series([1] * len(bad_data))
good_data["label"] = pd.Series([0] * len(good_data))
df = pd.concat([bad_data, good_data])
df = df.rename({0: "payment_text"}, axis=1)
return df
'''
[INPUT] labeled, textual training data
[OUTPUT] train_test split data ready to be put into a model
'''
def training_pipeline(labeled_data):
X = bow_featurizer(labeled_data["payment_text"])
Y = labeled_data["label"]
return X, Y
'''
[INPUT] list of payment texts
[OUTPUT] bag of words, emoji ratio, emoji one-hot-encoding transposed and returned as a df
'''
def bow_featurizer(payment_text_array):
vectorizer = CountVectorizer()
vectorizer.fit(bag_of_words)
bow_ohe = vectorizer.transform(payment_text_array).toarray()
features = pd.DataFrame(bow_ohe)
payment_text_array = pd.Series(payment_text_array)
er = payment_text_array.apply(emoji_ratio)
eohe = payment_text_array.apply(emoji_ohe)
df=pd.concat([features, er, eohe], axis=0)
print(df.head())
return df
'''
[INPUT] list of payment box texts from scraping.py
[OUTPUT] feature matrix to run model.predict() on
'''
def testing_pipeline(unlabeled_data):
feature_matrix = bow_featurizer(unlabeled_data)
return feature_matrix
'''
[INPUT] feature matrix with labels
[OUTPUT] sklearn Model object to be serialized
'''
def generate_model(X_train, Y_train):
model = GaussianNB()
model.fit(X_train, Y_train)
return model
'''
[INPUT] filepath to a serialized model
[OUTPUT] a model to use for predicting on unlabeled data
'''
def retrieve_model(filepath):
with open(filepath, 'rb') as fid:
model_loaded = cPickle.load(fid)
return model_loaded
'''
[INPUT] sklearn Model object to be pickled, filepath to pickle
[OUTPUT] none
[EFFECT] writes a serialized model to specified filepath
'''
def write_model(model, fp):
with open(fp, 'wb') as fid:
cPickle.dump(model, fid)
model_runner(MODEL_FP)