Beispiel #1
0
def svm_classification(genres, features_type):
	training_set_features = tf.read_features_from_files("../../music/training", genres, features_type)
	testing_set_features = tf.read_features_from_files("../../music/testing", genres, features_type)

	X = []
	y = []
	for feature in training_set_features:
		(mean, cov_mat, genre_name) = feature
		X.append(mean.tolist())
		y.append(tf.get_genre_ID(genre_name))

	training_data = np.array(X)
	training_class = np.array(y)

	X = []
	y = []
	for feature in testing_set_features:
		(mean, cov_mat, genre_name) = feature
		X.append(mean.tolist())
		y.append(tf.get_genre_ID(genre_name))

	testing_data = np.array(X)
	testing_class = np.array(y)


	clf = OneVsOneClassifier(SVC(kernel='linear'))
	result_class = np.array(clf.fit(training_data, training_class).predict(testing_data))

	rt.print_accuracy(list(testing_class), list(result_class), genres, features_type, "svm")
	rt.write_accuracy_to_file("../../music/", list(testing_class), list(result_class), genres, features_type, "svm")
Beispiel #2
0
def knn_classification(genres, features_type='mfcc', weight='distance', n_neighbors_min=3, n_neighbors_max=5):
	training_array = []
	training_classes = []

	training_set_features = tf.read_features_from_files("../../music/training", genres, features_type)

	n_features = len(training_set_features[0][0])

	# convert each cortege to array
	for track_features in training_set_features:
		training_array[len(training_array):] = [tf.cortege_to_list(track_features)]
		training_classes[len(training_classes):] = [tf.get_genre_ID(str(track_features[2]))]

	testing_set_features = tf.read_features_from_files("../../music/testing", genres, features_type)
	testing_array = []
	expected_genres = []

	# convert each cortege to array
	for track_features in testing_set_features:
		testing_array[len(testing_array):] = [tf.cortege_to_list(track_features)]
		expected_genres[len(expected_genres):] = [tf.get_genre_ID(str(track_features[2]))]

	for n_neighbors in range(n_neighbors_min, n_neighbors_max+1):
		knn_classifier = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, weights=weight, algorithm='ball_tree',
											metric=kl.calculate_KL_divergence, metric_params={"n_features": n_features})
		knn_classifier.fit(training_array,training_classes)
		result_of_classification = knn_classifier.predict(testing_array)
		result = []

		params_string = "weight: " + str(weight) + " n_neighbors: " + str(n_neighbors)
		rt.print_accuracy(expected_genres, result_of_classification, genres, features_type, "knn", params_string)
		rt.write_accuracy_to_file("../../music/", expected_genres, result_of_classification, genres, features_type, "knn", params_string)
def knn_classification(genres,
                       features_type='mfcc',
                       weight='distance',
                       n_neighbors_min=3,
                       n_neighbors_max=5):
    training_array = []
    training_classes = []

    training_set_features = tf.read_features_from_files(
        "../../music/training", genres, features_type)

    n_features = len(training_set_features[0][0])

    # convert each cortege to array
    for track_features in training_set_features:
        training_array[len(training_array):] = [
            tf.cortege_to_list(track_features)
        ]
        training_classes[len(training_classes):] = [
            tf.get_genre_ID(str(track_features[2]))
        ]

    testing_set_features = tf.read_features_from_files("../../music/testing",
                                                       genres, features_type)
    testing_array = []
    expected_genres = []

    # convert each cortege to array
    for track_features in testing_set_features:
        testing_array[len(testing_array):] = [
            tf.cortege_to_list(track_features)
        ]
        expected_genres[len(expected_genres):] = [
            tf.get_genre_ID(str(track_features[2]))
        ]

    for n_neighbors in range(n_neighbors_min, n_neighbors_max + 1):
        knn_classifier = neighbors.KNeighborsClassifier(
            n_neighbors=n_neighbors,
            weights=weight,
            algorithm='ball_tree',
            metric=kl.calculate_KL_divergence,
            metric_params={"n_features": n_features})
        knn_classifier.fit(training_array, training_classes)
        result_of_classification = knn_classifier.predict(testing_array)
        result = []

        params_string = "weight: " + str(weight) + " n_neighbors: " + str(
            n_neighbors)
        rt.print_accuracy(expected_genres, result_of_classification, genres,
                          features_type, "knn", params_string)
        rt.write_accuracy_to_file("../../music/", expected_genres,
                                  result_of_classification, genres,
                                  features_type, "knn", params_string)
Beispiel #4
0
def get_accuracy(expected_class, actual_class, genres, features_type,
                 *extra_strings):
    genres_correct = [0 for i in range(len(genres))]
    genres_total = [
        expected_class.count(tf.get_genre_ID(genres[i]))
        for i in range(len(genres))
    ]

    genres_correct_total = 0
    for i in range(len(expected_class)):
        if (expected_class[i] == actual_class[i]):
            pos = genres.index(tf.get_genre_name(expected_class[i]))
            genres_correct[pos] += 1
            genres_correct_total += 1
    extra_string = ""
    for string in extra_strings:
        if len(string) != 0:
            extra_string += str(string) + "\n"
    result = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \n" + \
    str(features_type) + "\n" + \
    str(genres) + "\n" + \
    str(extra_string) + \
    "Accuracy: \n"
    for i in range(len(genres)):
        result += str(genres[i]) + ": " + str(genres_correct[i]) + " " + str(
            float(genres_correct[i]) / float(genres_total[i])) + "\n"
    result += "average: " + str(float(genres_correct_total) / float(len(actual_class))) + "\n" + \
    "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< \n"
    return result