def test_repe_test(times, features, classes): every_f1_test = {} array_means_test = np.array([]) array_vars_test = np.array([]) iii = 0 while iii < times: x_train, y_train, x_cv, y_cv, x_test, y_test = TrainCvTest. \ separateDatabases(features, classes) clf = svm.trainSVM(x_train, y_train, call=True) F1Test = FScore.F1Score(x_test, y_test, clf) mTest = np.array([]) for key in F1Test.keys(): if key not in every_f1_test: every_f1_test[key] = F1Test[key] else: every_f1_test[key] = np.append(every_f1_test[key], F1Test[key]) mTest = np.append(mTest, F1Test[key]) array_means_test = np.append(array_means_test, mTest.mean()) array_vars_test = np.append(array_vars_test, mTest.var()) iii += 1 return every_f1_test, array_means_test, array_vars_test
def classify_new_sounds(folder_of_test_sounds, target_class): new_descriptors = {target_class: load_description(folder_of_test_sounds)} new_normalized_features, new_y_class, new_features_names = preprocessDescriptors(new_descriptors) new_y_class = np.array([target_class]*len(new_y_class)) descriptors = loadDescriptors(maximum='Inf', reverbs=True) normalized_features, yClass, features_names = preprocessDescriptors(descriptors) clf = trainSVM(normalized_features, yClass, call=True) F1 = F1Score(new_normalized_features, new_y_class, clf) return F1
def classify_new_sounds(folder_of_test_sounds, target_class): new_descriptors = {target_class: load_description(folder_of_test_sounds)} new_normalized_features, new_y_class, new_features_names = preprocessDescriptors( new_descriptors) new_y_class = np.array([target_class] * len(new_y_class)) descriptors = loadDescriptors(maximum='Inf', reverbs=True) normalized_features, yClass, features_names = preprocessDescriptors( descriptors) clf = trainSVM(normalized_features, yClass, call=True) F1 = F1Score(new_normalized_features, new_y_class, clf) return F1
def recall(XData, YData, clf=1): if clf == 1: clf = svm.trainSVM(XData, YData, True) predictions = [] for iii in np.arange(YData.size): predictions = np.append(predictions, clf.predict(XData[iii, :])[0]) tp = true_pos(predictions, YData) fn = false_neg(predictions, YData) recall = {} for key in tp.keys(): recall[key] = tp[key] / float(tp[key] + fn[key]) return recall
def test_repe_train_and_cv(times, features, classes): every_f1_train = {} every_f1_cv = {} array_means_train = np.array([]) array_vars_train = np.array([]) array_means_cv = np.array([]) array_vars_cv = np.array([]) iii = 0 while iii < times: x_train, y_train, x_cv, y_cv, x_test, y_test = TrainCvTest. \ separateDatabases(features, classes) clf = svm.trainSVM(x_train, y_train, call=True) F1Train = FScore.F1Score(x_train, y_train, clf) mTest = np.array([]) for key in F1Train.keys(): if key not in every_f1_cv: every_f1_train[key] = F1Train[key] else: every_f1_train[key] = np.append(every_f1_train[key], F1Train[key]) mTest = np.append(mTest, F1Train[key]) array_means_train = np.append(array_means_train, mTest.mean()) array_vars_train = np.append(array_vars_train, mTest.var()) F1CV = FScore.F1Score(x_cv, y_cv, clf) mCV = np.array([]) for key in F1CV.keys(): if key not in every_f1_cv: every_f1_cv[key] = F1CV[key] else: every_f1_cv[key] = np.append(every_f1_cv[key], F1CV[key]) mCV = np.append(mCV, F1CV[key]) array_means_cv = np.append(array_means_cv, mCV.mean()) array_vars_cv = np.append(array_vars_cv, mCV.var()) iii += 1 return every_f1_train, every_f1_cv, array_means_train, \ array_vars_train, array_means_cv, array_vars_cv
def precision(XData, YData, clf=1): if clf == 1: clf = svm.trainSVM(XData, YData, True) predictions = [] for iii in np.arange(YData.size): predictions = np.append(predictions, clf.predict(XData[iii, :])[0]) tp = true_pos(predictions, YData) fp = false_pos(predictions, YData) precision_ = {} for key in tp.keys(): try: precision_[key] = tp[key] / float(tp[key] + fp[key]) except: precision_[key] = -1 return precision_