text_plain_output_1.png image_output_1.png
from sklearn import svm

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import scale

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

table = pd.read_csv('../input/genre-dataset/genre_dataset.txt')
table = table[table.genre.str.contains('jazz and blues') | table.genre.str.contains('soul and reggae')]
for i in range(4, len(table.columns)):
    col = table.iloc[:, [i]].values
    table.iloc[:, [i]] = scale(col)

le = LabelEncoder()
table['genre'] = le.fit_transform(table[['genre']])

X_train, X_test = train_test_split(table, test_size=0.2)
x_train = X_train.iloc[:, 4:].values
y_train = X_train.iloc[:, 0]
x_test = X_test.iloc[:, 4:].values
y_test = X_test.iloc[:, 0]

cls = svm.SVC(kernel='poly')

# Ajuste del grado del polinomio
smallgrid = {'degree': [2,3,4,5,6,7,8,9,10]}
grid1 = GridSearchCV(cls,smallgrid)
grid1.fit(x_train,y_train)

# Graficar los resultados
grid_result1 = pd.DataFrame(grid1.cv_results_)
plt = grid_result1.plot(x ='param_degree', y='mean_test_score')
plt = plt.set_ylabel("Precisión")

param_grid = {'C': [0.1, 1, 10, 100], 'degree': [2, 3, 4, 5]}
grid = GridSearchCV(cls, param_grid)
grid.fit(x_train, y_train)

pvt = pd.pivot_table(pd.DataFrame(grid.cv_results_), values='mean_test_score', index='param_degree', columns='param_C')

smallgrid2 = {'C': [2, 4, 6, 8, 10, 12, 14, 16, 18, 20], 'degree': [3]}
grid2 = GridSearchCV(cls, smallgrid2)
grid2.fit(x_train, y_train)

# Graficar los resultados
grid_result2 = pd.DataFrame(grid2.cv_results_)
plt = grid_result2.plot(x ='param_C', y='mean_test_score')
plt = plt.set_ylabel("Precisión")

smallgrid3 = {'gamma': [0.001, 0.01, 0.1], 'C': [4], 'degree': [3]}
grid3 = GridSearchCV(cls, smallgrid3)
grid3.fit(x_train, y_train)
grid_result3 = pd.DataFrame(grid3.cv_results_)
plt3 = grid_result3.plot(x='param_gamma', y='mean_test_score')