def lr_predict(train, test, preprocessing_type): # separate class label (last column) train, labels = preprocessing.split_labels(train) if preprocessing_type == 'smote': train, labels = preprocessing.apply_smote(train, labels) classifier = linear_model.LogisticRegression() validation.cross_validate(classifier, train, labels) classifier.fit(train, labels) # test test, test_labels = preprocessing.split_labels(test) validation.test(classifier, test, test_labels)
def multiple_balanced_sets(): train, test = preprocessing.prepare_data() train_list = preprocessing.multiple_balanced_samples(train, 5) # separate class label (last column) for i in range(5): train, labels = preprocessing.split_labels(train_list[i]) classifier = linear_model.LogisticRegression() validation.cross_validate_set(classifier, train, labels) validation.cross_validate(classifier, train, labels) classifier.fit(train, labels) # test test, test_labels = preprocessing.split_labels(test) validation.test(classifier, test, test_labels)
def nn_predict(train, labels, test, writer, params): clf = MLPClassifier(solver='lbfgs', alpha=1.e-3, hidden_layer_sizes=(20,params['layer_2']), random_state=1) vresult = validation.cross_validate(clf, train, labels) clf.fit(train, labels) # test test_labels = test[['Class']] test_labels = test_labels.values.ravel() test = test.drop('Class', 1) tresult = validation.test(clf, test, test_labels) results = list(params.values()) results.append(vresult['roc_auc']) results.append(vresult['precision']) results.append(vresult['recall']) results.append(vresult['f1']) results.append(vresult['fp']) results.append(vresult['fn']) results.append(tresult['roc_auc']) results.append(tresult['precision']) results.append(tresult['recall']) results.append(tresult['f1']) results.append(tresult['fp']) results.append(tresult['fn']) writer.writerow(results)
def svm_predict(train, test, preprocessing_type): # separate class label (last column) train, labels = preprocessing.split_labels(train) if preprocessing_type == 'smote': train, labels = preprocessing.apply_smote(train, labels) # Classifier # Class weight parameter: weights positive class more strongly than negative class. # class_weight={1: 2.61, 0: 0.383} classifier = svm.SVC(kernel='rbf') validation.cross_validate(classifier, train, labels) classifier.fit(train, labels) # test test, test_labels = preprocessing.split_labels(test) validation.test(classifier, test, test_labels)
def rf_predict(train, test, preprocessing_type, results_file): # separate class label train, labels = preprocessing.split_labels(train) if preprocessing_type == 'smote': train, labels = preprocessing.apply_smote(train, labels) classifier = ensemble.RandomForestClassifier(class_weight={ 0: 0.75, 1: 1.5 }, min_samples_split=40, n_estimators=15) classifier.fit(train, labels) vresult = validation.cross_validate(classifier, train, labels) # test test, test_labels = preprocessing.split_labels(test) tresult = validation.test(classifier, test, test_labels) # save results results = [] results.append("low_skew (0=0.75, 1=1.5)") results.append(40) results.append(15) results.append(vresult['roc_auc']) results.append(vresult['precision']) results.append(vresult['recall']) results.append(vresult['f1']) results.append(vresult['fp']) results.append(vresult['fn']) results.append(tresult['roc_auc']) results.append(tresult['precision']) results.append(tresult['recall']) results.append(tresult['f1']) results.append(tresult['fp']) results.append(tresult['fn']) results_file.writerow(results)
print(xTrain.shape, yTrain.shape) print("Data loaded...") print("Training data") print(xTrain) print(yTrain) print("Verification data") print(xTest) print(yTest) print("length of x" + str(len(xTrain))) print("length of y" + str(len(yTrain))) print("Creating classifiers...") clf = KNeighborsClassifier() # Validation validation.cross_validate(clf, xTrain, yTrain) clf.fit(xTrain, yTrain) print("KNeighborsClassifier") score = clf.score(xTest, yTest) print(str(score)) # Test validation.test(clf, xTest, yTest)