本文整理汇总了Python中imblearn.over_sampling.SMOTE.reshape方法的典型用法代码示例。如果您正苦于以下问题:Python SMOTE.reshape方法的具体用法?Python SMOTE.reshape怎么用?Python SMOTE.reshape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类imblearn.over_sampling.SMOTE
的用法示例。
在下文中一共展示了SMOTE.reshape方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_save_model
# 需要导入模块: from imblearn.over_sampling import SMOTE [as 别名]
# 或者: from imblearn.over_sampling.SMOTE import reshape [as 别名]
def run_save_model(save_folder, spec, model_no, X_train, y_train, model_fn):
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=2)
cvscores = []
f1scores = []
for train, val in kfold.split(X_train, y_train):
# create model using the model_fn parameter
model = model_fn(spec, X_train)
if model == None:
return # returns if there was a mistake in specifications
# fit model to k-split of training data
num_examples, dx, dy = X_train[train].shape
X_resampled, y_resampled = SMOTE(kind='borderline1', random_state=1).fit_sample(
X_train[train].reshape((num_examples, dx * dy)), y_train[train])
num_total_examples, _ = X_resampled.shape
X_resampled_reshaped = X_resampled.reshape(num_total_examples, dx, dy)
model.fit(x=X_resampled_reshaped, y=y_resampled, epochs=10, batch_size=16, verbose=0)
# evaluate model
scores = model.evaluate(X_train[val], y_train[val], verbose=0)
print('Accuracy: {}%'.format(scores[1] * 100))
cvscores.append(scores[1])
# get f1
f1 = f1_score(y_train[val], model.predict(X_train[val]) > 0.5)
print('F1 score: {}'.format(f1))
f1scores.append(f1)
mean_acc = 'Mean Accuracy: {}% +/- {}%'.format(np.mean(cvscores) * 100, np.std(cvscores) * 100)
mean_f1 = 'Mean F1 score: {} +/- {}'.format(np.mean(f1scores), np.std(f1scores))
print(mean_acc)
print(mean_f1)
# modelfile = save_folder + 'model' + str(model_no) + '.h5'
# save_model(model, modelfile)
# print('model saved')
txtfile = save_folder + 'model' + str(model_no) + '.txt'
with open(txtfile, 'w') as f:
f.write(mean_acc)
f.write(mean_f1)
f.write('\n')
f.write('\n')
f.writelines(spec)
print('specs saved')