-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
78 lines (59 loc) · 2.82 KB
/
model.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
76
77
78
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.feature_extraction.text import TfidfVectorizer
class Allisto:
def __init__(self):
self.df = pd.read_csv('cry.csv')
self.X = self.df.iloc[:, 0:19].values
self.y = self.df.iloc[:, 20]
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
self.X, self.y, test_size=0.2, random_state=0)
self.sc = StandardScaler()
self.X_train = self.sc.fit_transform(self.X_train)
self.X_test = self.sc.transform(self.X_test)
self.classifier = Sequential()
self.vectorizer = TfidfVectorizer()
def vectorizer_fit(self, X):
self.vectorizer.fit(X)
def vectorizer_transform(self, X):
X_transformed = self.vectorizer.transform(X)
return X_transformed
def build_model(self):
self.classifier.add(Dense(output_dim=6, init='uniform',
activation='relu', input_dim=19))
self.classifier.add(
Dense(output_dim=6, init='uniform', activation='relu'))
self.classifier.add(
Dense(output_dim=1, init='uniform', activation='sigmoid'))
self.classifier.compile(
optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
def train(self):
self.classifier.fit(self.X_train, self.y_train,
batch_size=10, nb_epoch=100)
def predict(self, input_params):
return self.classifier.predict(input_params) >= 0
def save(self):
self.classifier.save('model.h5')
def load(self):
self.classifier = load_model('model.h5')
class AllistoPredict:
def __init__(self):
self.model = load_model('model.h5')
def predict(self, input_params):
return self.model.predict(input_params) >= 0.5
"""
Testing of the Classes Created
model = Allisto()
model.build_model()
model.train()
model.save()
model.load()
obj = AllistoPredict()
obj.predict(np.array([[0.13201746241462,0.0846523059846263,0.130812215724496,0.0480311890838207,0.210136452241715,0.162105263157895,1.31334024389288,4.99252406943563,0.972450815429729,0.787036038235428,0.0282001299545159,0.13201746241462,0.178684467460517,0.015984015984016,0.275862068965517,0.751302083333333,0.0078125,6.1953125,6.1875]]))
"""
# import numpy as np
# value = clf.predict(np.array([[0.13201746241462,0.0846523059846263,0.130812215724496,0.0480311890838207,0.210136452241715,0.162105263157895,1.31334024389288,4.99252406943563,0.972450815429729,0.787036038235428,0.0282001299545159,0.13201746241462,0.178684467460517,0.015984015984016,0.275862068965517,0.751302083333333,0.0078125,6.1953125,6.1875]]))