示例#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")
示例#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)