# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="crYdbMmW_3kn" colab_type="text"
# # Import and preprocessing

# + id="xx3AkgtVxr7h" colab_type="code" outputId="0648bc6f-fbbd-4654-a515-3c1694d591bc" colab={"base_uri": "https://localhost:8080/", "height": 35}
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from numpy import genfromtxt
from ast import literal_eval
from sklearn.feature_extraction.text import TfidfVectorizer
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv1D, GlobalMaxPooling1D
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
from keras import regularizers
import matplotlib.pyplot as plt

from google.colab import drive
drive.mount('/content/gdrive')

df = pd.read_csv('/content/gdrive/My Drive/datasets/dataset.csv', header=None, names=["artist", "song", "genre", "tokens"])

def to_list(x):
    return x[1:-1].split(',')

def unison_shuffled_copies(a, b):
    assert a.shape[0] == b.shape[0]
    p = np.random.permutation(a.shape[0])
    return a[p], b[p]

def dummy_fun(x):
    return x

df['tokens'] = df['tokens'].apply(to_list)

data = df.values

tfidf = TfidfVectorizer(
    analyzer='word',
    tokenizer=dummy_fun,
    preprocessor=dummy_fun,
    token_pattern=None)  

X = tfidf.fit_transform(data[:,3])
Y = data[:, 2]

encoder = LabelEncoder()
encoder.fit(Y)
Y = encoder.transform(Y)
Y = np_utils.to_categorical(Y)

# + id="VlZKjNbyo9lD" colab_type="code" colab={}
X_train = X[:5200]
# X_valid = X[4800:5500]
X_test = X[5200:]

Y_train = Y[:5200]
# Y_valid = Y[4800:5500]
Y_test = Y[5200:]

# + [markdown] id="EYx2rsAAAClh" colab_type="text"
# # Neural Network

# + id="IfyC1mEuz4KV" colab_type="code" outputId="b8d85b44-5990-4633-f5e0-cc0bbc42e96a" colab={"base_uri": "https://localhost:8080/", "height": 431}
seed = 7
np.random.seed(seed)

INPUT_SIZE = X.shape[1]

def baseline_model():
	model = Sequential()
	model.add(Dense(100, input_dim=INPUT_SIZE, activation='relu'))
	model.add(Dropout(.5))
	model.add(Dense(100, activation='relu'))
	model.add(Dropout(.5))
	model.add(Dense(7, activation='softmax'))
	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
	return model

model = baseline_model()
history = model.fit(X_train, Y_train,
                    epochs=10,
                    verbose=1,
                    validation_split=0.2,
                    shuffle=True,
                    batch_size=10)

loss, accuracy = model.evaluate(X_train, Y_train, verbose=False)
print("Training Accuracy: {:.4f}".format(accuracy))

loss, accuracy = model.evaluate(X_test, Y_test, verbose=False)
print("Testing Accuracy:  {:.4f}".format(accuracy))

# estimator = KerasClassifier(build_fn=baseline_model, epochs=200, batch_size=20, verbose=0)
# kfold = KFold(n_splits=10, shuffle=True, random_state=seed)

# results = cross_val_score(estimator, X, dummy_y, cv=kfold)
# print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))

# + id="oK0tVf94jkwj" colab_type="code" outputId="60a8cf02-039f-4d7c-fe5a-ae447a418a68" colab={"base_uri": "https://localhost:8080/", "height": 354}

plt.style.use('ggplot')

def plot_history(history):
    acc = history.history['acc']
    val_acc = history.history['val_acc']
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    x = range(1, len(acc) + 1)

    plt.figure(figsize=(12, 5))
    plt.subplot(1, 2, 1)
    plt.plot(x, acc, 'b', label='Training acc')
    plt.plot(x, val_acc, 'r', label='Validation acc')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.title('ANN Training and Validation accuracy')
    plt.legend()
    plt.subplot(1, 2, 2)
    plt.plot(x, loss, 'b', label='Training loss')
    plt.plot(x, val_loss, 'r', label='Validation loss')
    plt.ylabel('Loss')
    plt.xlabel('epoch')
    plt.title('Training and validation loss')
    plt.legend()
    
plot_history(history)


# + [markdown] id="8NTcz4Ck_lPM" colab_type="text"
# # Convolutional Neural Network

# + id="uXHzAqTy9QqE" colab_type="code" outputId="4c8f3423-df8f-4aef-f73d-b26dc22ad11b" colab={"base_uri": "https://localhost:8080/", "height": 431}
def cnn_model():
	model = Sequential()
	model.add(Conv1D(512, 5,input_shape=(1, INPUT_SIZE)))
	model.add(Dense(36, activation='relu'))
	model.add(Dropout(.5))
	model.add(Dense(36, activation='relu'))
	model.add(Dropout(.5))
	model.add(Dense(7, activation='softmax'))
	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
	return model

model = baseline_model()
history = model.fit(X_train, Y_train,
                    epochs=10,
                    verbose=1,
                    validation_split=0.2,
                    shuffle=True,
                    batch_size=10)

loss, accuracy = model.evaluate(X_train, Y_train, verbose=False)
print("Training Accuracy: {:.4f}".format(accuracy))

loss, accuracy = model.evaluate(X_test, Y_test, verbose=False)
print("Testing Accuracy:  {:.4f}".format(accuracy))

# + id="vFrEBNDfp-rN" colab_type="code" outputId="3e4b59db-6144-484f-facf-52ce667736b9" colab={"base_uri": "https://localhost:8080/", "height": 354}
plt.style.use('ggplot')

def plot_history(history):
    acc = history.history['acc']
    val_acc = history.history['val_acc']
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    x = range(1, len(acc) + 1)

    plt.figure(figsize=(12, 5))
    plt.subplot(1, 2, 1)
    plt.plot(x, acc, 'b', label='Training acc')
    plt.plot(x, val_acc, 'r', label='Validation acc')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.title('CNN Training and validation accuracy')
    plt.legend()
    plt.subplot(1, 2, 2)
    plt.plot(x, loss, 'b', label='Training loss')
    plt.plot(x, val_loss, 'r', label='Validation loss')
    plt.ylabel('Loss')
    plt.xlabel('epoch')
    plt.title('Training and validation loss')
    plt.legend()
    
plot_history(history)

# + id="hoonbHhffc2d" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered