Using pickle and trying to generate classifier result in python gives me a TypeError. I understand the general concept as to why this occurs but I am not sure what is wrong with my syntax. Everything runs until I get to this part.
Thank you in advance for your help on the syntax and any details you can give on what I am missing:
in jupyter notebook cells: .
# Preparing a trainig data set from our data
def prepare_training(models):
dataset = open_dataset()
validation_size = 0.50
seed = 7
X, Y = split_data(dataset)
# --> split the data into two sets - one training, one test
X_train, X_test, Y_train, Y_test = model_selection.train_test_split(
X,
Y,
test_size=validation_size,
random_state=seed
)
train_models(models, X_train, Y_train, X_test, Y_test)
# Using kfold to iterate through some models
def train_models(models, X_train, Y_train, X_test, Y_test):
classifiers = []
# iterates through the models
for name, model in models:
# chooses the index's for test and training set
kfold = model_selection.KFold(n_splits=4)
for traincv, testcv in kfold.split(X_train):
# trains the models
model.fit(X_train[traincv], Y_train[traincv])
# tests the models, doesn't output the result
model.predict(X_train[testcv])
# final test on the original test set
prediction = model.predict(X_test)
print(name, accuracy_score(prediction, Y_test) * 100)
with open(abspath,
'pickle/' + name + '_classifier.pickle',
'wb'
) as ph:
pickle.dump(model, ph)
classifiers.append((name, model))
return classifiers
# Dictionary for Glass Types
glass_types = {
'1.0': 'Building Windows Float Processed',
'2.0': 'Building Windows Non Float Processed',
'3.0': 'Vehicle Windows Float Processed',
'4.0': 'Vehicle Windows Non Float Processed',
'5.0': 'Containers',
'6.0': 'Tableware',
'7.0': 'Headlamps'
}
# Generating a short catalogue of classifers
classifiers = [
'DecisionTreeClassifier',
'SVM'
]
all cells above run fine, but this cell outputs the TypeError
# Checking for classifers -- if not in there, then it loops to create them
models = []
if os.path.isfile(os.path.join(abspath, 'pickle/SVM_classifier.pickle')):
#if os.path.isfile(os.path.abspath('pickle/SVM_classifier.pickle')):#
for name in classifiers:
with open(abspath, 'pickle/SVM_classifier.pickle', 'rb') as ph:
models.append((name, pickle.load(ph)))
else:
models.append(('DecisionTreeClassifier', DecisionTreeClassifier()))
models.append(('SVM', SVC()))
models = prepare_training(models)
---------------------------------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-56-eceaf68f7f00> in <module>
12 models.append(('DecisionTreeClassifier',
DecisionTreeClassifier()))
13 models.append(('SVM', SVC()))
---> 14 models = prepare_training(models)
<ipython-input-49-fead6c6a1c89> in prepare_training(models)
13 random_state=seed
14 )
---> 15 train_models(models, X_train, Y_train, X_test, Y_test)
<ipython-input-50-a76ec7e93740> in train_models(models, X_train,
Y_train, X_test, Y_test)
17 with open(abspath,
18 'pickle/' + name + '_classifier.pickle',
---> 19 'wb'
20 ) as ph:
21 pickle.dump(model, ph)
TypeError: an integer is required (got type str)