<jupyter_start><jupyter_text>Build Model on new DataSet<jupyter_code>import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>I. Cleaning and Preprocessing Data<jupyter_code>df_songs = pd.read_csv('subset_every_feature.csv')
print(df_songs.shape)
df_songs.head()
df_songs.columns
local_output = ['ee', 'br',
       'my', 'cz', 'pe', 'gb', 'ie', 'se', 'sk', 'co', 'sg', 'ec', 'es', 'no',
       'dk', 'be', 'lu', 'do', 'nz', 'de', 'hu', 'cr', 'jp', 'hk', 'lt', 'pl',
       'it', 'hn', 'mx', 'ph', 'cl', 'is', 'ca', 'ar', 'at', 'ch', 'tr', 'py',
       'tw', 'gt', 'sv', 'us', 'fr', 'global', 'nl', 'bo', 'lv', 'gr', 'pa',
       'uy', 'au', 'pt', 'fi']
df_output = df_songs[local_output]
print('We have outputs in {} countries!'.format(len(local_output)))
spotify_features = ['acousticness', 'danceability', 'duration_ms', 'energy', 'instrumentalness',
       'key', 'liveness', 'loudness', 'mode', 'speechiness', 'tempo','valence']<jupyter_output><empty_output><jupyter_text>TODO: Check correlance between spotify features and popularity<jupyter_code># Create list of URI's for Ricky to scrape features
df_uri = df_songs['uri']
df_uri.head()
df_uri.to_csv('uri.csv')
plt.figure()
df_output.hist()
plt.show()
for i in range(df_output.shape[1]):
    print('Country: ' + local_output[i]);
    print(df_output.iloc[:,i].value_counts())<jupyter_output>Country: ee
False    1218
True      374
Name: ee, dtype: int64
Country: br
False    1191
True      401
Name: br, dtype: int64
Country: my
False    987
True     605
Name: my, dtype: int64
Country: cz
False    838
True     754
Name: cz, dtype: int64
Country: pe
False    1226
True      366
Name: pe, dtype: int64
Country: gb
False    865
True     727
Name: gb, dtype: int64
Country: ie
False    849
True     743
Name: ie, dtype: int64
Country: se
False    933
True     659
Name: se, dtype: int64
Country: sk
False    1144
True      448
Name: sk, dtype: int64
Country: co
False    1138
True      454
Name: co, dtype: int64
Country: sg
False    989
True     603
Name: sg, dtype: int64
Country: ec
False    1159
True      433
Name: ec, dtype: int64
Country: es
False    1065
True      527
Name: es, dtype: int64
Country: no
False    956
True     636
Name: no, dtype: int64
Country: dk
False    991
True     601
Name: dk, dtype: int64
Country: be
False    859
True     733
Name: be, dtype: int64
Country: l[...]<jupyter_text>Clean Timbre<jupyter_code>timbre_features = ['timbre_all_1', 'timbre_all_2',
       'timbre_all_3', 'timbre_all_4', 'timbre_all_5', 'timbre_all_6',
       'timbre_all_7', 'timbre_all_8', 'timbre_all_9', 'timbre_all_10',
       'timbre_all_11', 'timbre_all_12']
df_timbre = df_songs[timbre_features]
df_timbre.head()
# Check type of array:
type(df_timbre.iloc[0,0])
# Function for converting TimbreString into List
def timbreStringToList(timbreString):
    timbreString = timbreString[:-1]
    timbreString = timbreString[1:]
    timbreList = [float(s) for s in timbreString.split(',')]
    return timbreList
# Convert strings to lists
for i in range(df_timbre.shape[1]):
    timbre_df = df_timbre.iloc[:,i]
    for j in range(timbre_df.shape[0]):
        timbre_df[j] = timbreStringToList(timbre_df[j])
    df_timbre.iloc[:,i] = timbre_df
# Test if it's a list
type(df_timbre.iloc[2,1])
df_timbre.shape
from scipy.stats import kurtosis
from scipy.stats import skew
def f_mean(row):
    return(np.mean(row))
def f_median(row):
    return(np.median(row))
def f_std(row):
     return(np.std(row))
def f_min(row):
     return(np.min(row))
def f_max(row):
     return(np.max(row))
def f_80Percentile(row):
    return(np.percentile(row, 80))
def f_kurtosis(row):
    return(kurtosis(row))
def f_skew(row):
    return(skew(row))
# Create timbre features
for i in range(df_timbre.shape[1]):
    current_column  = df_timbre.iloc[:,i]
    df_timbre['mean_timbre{}'.format(i+1)] = current_column.apply(f_mean)
    df_timbre['median_timbre{}'.format(i+1)] = current_column.apply(f_median)
    df_timbre['std_timbre{}'.format(i+1)] = current_column.apply(f_std)
    df_timbre['min_timbre{}'.format(i+1)] = current_column.apply(f_min)
    df_timbre['max_timbre{}'.format(i+1)] = current_column.apply(f_max)
    df_timbre['range_timbre{}'.format(i+1)] = df_timbre['max_timbre{}'.format(i+1)]-df_timbre['min_timbre{}'.format(i+1)]
    df_timbre['80Percentile_timbre{}'.format(i+1)] = current_column.apply(f_80Percentile)
    df_timbre['kurtosis_timbre{}'.format(i+1)] = current_column.apply(f_kurtosis)
    df_timbre['skewness_timbre{}'.format(i+1)] = current_column.apply(f_skew)

    print('Finished Column {}'.format(i))
df_timbre.head()
print(list(df_timbre.columns))
# timbre features without vector values in the beginning
timbre_features = list(df_timbre.columns[12:])
print(timbre_features)<jupyter_output>['mean_timbre1', 'median_timbre1', 'std_timbre1', 'min_timbre1', 'max_timbre1', 'range_timbre1', '80Percentile_timbre1', 'kurtosis_timbre1', 'skewness_timbre1', 'mean_timbre2', 'median_timbre2', 'std_timbre2', 'min_timbre2', 'max_timbre2', 'range_timbre2', '80Percentile_timbre2', 'kurtosis_timbre2', 'skewness_timbre2', 'mean_timbre3', 'median_timbre3', 'std_timbre3', 'min_timbre3', 'max_timbre3', 'range_timbre3', '80Percentile_timbre3', 'kurtosis_timbre3', 'skewness_timbre3', 'mean_timbre4', 'median_timbre4', 'std_timbre4', 'min_timbre4', 'max_timbre4', 'range_timbre4', '80Percentile_timbre4', 'kurtosis_timbre4', 'skewness_timbre4', 'mean_timbre5', 'median_timbre5', 'std_timbre5', 'min_timbre5', 'max_timbre5', 'range_timbre5', '80Percentile_timbre5', 'kurtosis_timbre5', 'skewness_timbre5', 'mean_timbre6', 'median_timbre6', 'std_timbre6', 'min_timbre6', 'max_timbre6', 'range_timbre6', '80Percentile_timbre6', 'kurtosis_timbre6', 'skewness_timbre6', 'mean_timbre7', 'median_timbre7', 'std_[...]<jupyter_text>load popularity<jupyter_code>df_popularity = pd.read_csv('uri_with_popularity_subset.csv')
df_popularity = df_popularity['popularity']
df_popularity.head()<jupyter_output><empty_output><jupyter_text>Save CSV file of whole dataset:<jupyter_code>df = pd.concat([df_songs['uri'], df_songs[spotify_features], df_timbre[timbre_features], df_songs[local_output], df_popularity], axis = 1)
print(df.shape)
df.head()
df.to_csv('cleanedDataSet_final.csv')<jupyter_output><empty_output><jupyter_text>Explore Data Reload datafile, no need to clean the data again<jupyter_code>df = pd.read_csv('cleanedDataSet_final.csv')
columns = list(df.columns)
print(columns)
print(df.shape)
# Extract Features
index = columns.index('skewness_timbre12')
df_features = df.iloc[:,2:index+1]
features = list(df_features.columns)
print('We have {} features extracted'.format(df_features.shape[1]))
print(features)
# Extract output
df_output = df.iloc[:, index+1:]
output = list(df_output.columns)
print('We have rankings for {} countries, plus the overall popularity:'.format(df_output.shape[1]-1))
print(output)<jupyter_output>We have rankings for 53 countries, plus the overall popularity:
['ee', 'br', 'my', 'cz', 'pe', 'gb', 'ie', 'se', 'sk', 'co', 'sg', 'ec', 'es', 'no', 'dk', 'be', 'lu', 'do', 'nz', 'de', 'hu', 'cr', 'jp', 'hk', 'lt', 'pl', 'it', 'hn', 'mx', 'ph', 'cl', 'is', 'ca', 'ar', 'at', 'ch', 'tr', 'py', 'tw', 'gt', 'sv', 'us', 'fr', 'global', 'nl', 'bo', 'lv', 'gr', 'pa', 'uy', 'au', 'pt', 'fi', 'popularity']<jupyter_text>Define a few helper functions for ML<jupyter_code># import packages
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import Perceptron
import xgboost as xgb

from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve, auc

import operator

from sklearn.preprocessing import normalize

# Confusion Matrix
def printConfusionMatrix (y_true, y_pred):
    cf=pd.DataFrame(confusion_matrix(y_true, y_pred))
    print(cf)

# Plot ROC Curve
def plotROCCurve (y_true, y_pred, name):
    fpr, tpr, thresholds = roc_curve(y_true, y_pred)
    roc_auc = auc(fpr, tpr)
    
    plt.figure()
    lw = 2
    plt.plot(fpr, tpr, color='darkorange',
             lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic: '+ name)
    plt.legend(loc="lower right")
    plt.show()
# Feature importance
def featureImportance(x_train, y_train, features):
    random_forest = RandomForestClassifier(n_estimators=190)
    random_forest.fit(x_train, y_train)
    values = random_forest.feature_importances_
    dict_featureImportance = dict(zip(features,values))
    sorted_dict = sorted(dict_featureImportance.items(), key=operator.itemgetter(1), reverse = True)
    return sorted_dict
# trains a specific model and report accuracies
def runsModel(x_train, x_test, y_train, y_test, model, printing = True):

    # Train model
    model.fit(x_train, y_train)
    trainingAccuracy = (model.score(x_train, y_train))
    testAccuracy = (model.score(x_test, y_test))

    fpr, tpr, thresholds = roc_curve(y_test, model.predict(x_test))
    roc_auc = auc(fpr, tpr)
    aucScore = (roc_auc)
    
    if (printing):
        print('Test Accuracy: {}'.format(testAccuracy))
        print('Training Accuracy: {}'.format(trainingAccuracy))
        print('AUC Score: {}'.format(aucScore))
        print('Confusion Matrix')
        printConfusionMatrix(y_test, model.predict(x_test))
    return model
# Trains differnt models and compares the results
def performsMLModelComparison(x_train, x_test, y_train, y_test, printing=True, verbose = True):
    '''Tries standard ML models, reports average accuracy and best model (with its accuracy and confusion matrix)'''
    MLModels = [LogisticRegression(), SVC(), Perceptron(), KNeighborsClassifier(n_neighbors = 3), xgb.XGBClassifier(n_estimators = 140), RandomForestClassifier(n_estimators = 140)]
    MLModelsStrings = ["Logistic Regression", "Support Vector Machine", "Perceptron", "KN Neighbors", "Gradient Boosting", "Random Forest"]
    nModels = len(MLModels)
    trainingAccuracy = list()
    testAccuracy = list()
    aucScore = list()

    # Train models
    for i in range(nModels):
        MLModels[i].fit(x_train, y_train)
        trainingAccuracy.append (MLModels[i].score(x_train, y_train))
        testAccuracy.append(MLModels[i].score(x_test, y_test))
        
        fpr, tpr, thresholds = roc_curve(y_test, MLModels[i].predict(x_test))
        roc_auc = auc(fpr, tpr)
        aucScore.append(roc_auc)
    
    # Report average test accuracy
    if (printing and verbose):
        print('Used the following models:')
        print(MLModelsStrings)
        print('Test Scores')
        print(testAccuracy)
        print('AUC Scores')
        print(aucScore)
        print('Average test accuracies of all models is: {}'.format(np.mean(testAccuracy)))
    
    # Find best model
    max_testAccuracy = max(testAccuracy)
    max_index = testAccuracy.index(max_testAccuracy)
    bestModel = MLModels[max_index]
    if (printing):
        print('{} is the best model'.format(MLModelsStrings[max_index]))
        print('Test Accuracy: {}'.format(max_testAccuracy))
        print('AUC Score: {}'.format(aucScore[max_index]))
        print('Confusion Matrix')
        printConfusionMatrix(y_test, bestModel.predict(x_test))
    
    return bestModel<jupyter_output><empty_output><jupyter_text>Predict overall popularity  Approch 1: without gap<jupyter_code># Define In- and Output
X = df_features
X_norm = normalize(X)
print('Size of Input: {}'.format(X_norm.shape))
Y = df_output['popularity']
print('Size of Output: {}'.format(Y.shape))
# Create quantile bins for popularity
Y = pd.qcut(Y, 2, labels= False)
Y.head()
plt.figure()
Y.hist()
plt.title('Popularity Bins')
plt.show()
# Create train and test set
x_train, x_test, y_train, y_test = train_test_split(X_norm,Y, test_size=0.15, random_state=100)
print('Training Set: {}'.format(x_train.shape[0]))
print('Test Set: {}'.format(x_test.shape[0]))
performsMLModelComparison(x_train, x_test, y_train, y_test)<jupyter_output>C:\Users\mx190\Anaconda3\envs\data-x\lib\site-packages\sklearn\preprocessing\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.
  if diff:
C:\Users\mx190\Anaconda3\envs\data-x\lib\site-packages\sklearn\preprocessing\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.
  if diff:
C:\Users\mx190\Anaconda3\envs\data-x\lib\site-packages\sklearn\preprocessing\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.
  if diff:<jupyter_text>Approach 2: with Gap<jupyter_code># Define In- and Output
X = df_features
X_norm = normalize(X)
print('Size of Input: {}'.format(X_norm.shape))
Y = df_output['popularity']
print('Size of Output: {}'.format(Y.shape))
# Create quantile bins for popularity
gapSize = 0.3
binSize = (1-gapSize)/2
Y = pd.qcut(Y, [0, binSize, binSize+gapSize, 1], labels= False)
Y.head()
plt.figure()
Y.hist()
plt.title('Popularity Bins')
plt.show()
# Throw away the middle one
df_curr = pd.concat([X, Y], axis = 1)
n = df_curr.shape[0]
df_curr = df_curr[df_curr.popularity!=1]
print('Reduced data set to {} from {}:'.format(df_curr.shape[0], n))
df_curr.popularity = df_curr.popularity.replace(2,1)
# Build input/output:
X = df_curr.iloc[:, :-1]
X_norm = normalize(X)
Y = df_curr.popularity
print('Size of Input: {}'.format(X_norm.shape))
print('Size of Output: {}'.format(Y.shape))
# Create train and test set
x_train, x_test, y_train, y_test = train_test_split(X_norm,Y, test_size=0.15, random_state=100)
print('Training Set: {}'.format(x_train.shape[0]))
print('Test Set: {}'.format(x_test.shape[0]))
# Trains different models and compare results
model = performsMLModelComparison(x_train, x_test, y_train, y_test)
plotROCCurve(y_test, model.predict(x_test), 'Popularity Prediction')
# Analyse feature importance
list_features = featureImportance(x_train, y_train, list(df_features.columns))
dict_features = dict(list_features)
# Plot bar plot of feature importance
plt.figure()
plt.bar(range(len(dict_features)), dict_features.values(), align='center')
plt.xticks(range(len(dict_features)), dict_features.keys())
plt.show()
dict_topFeatures = dict(list_features[:10])
plt.figure()
plt.bar(range(len(dict_topFeatures)), dict_topFeatures.values(), align='center')
plt.xticks(range(len(dict_topFeatures)), dict_topFeatures.keys())
plt.show()
print('Top 10 Features: ')
print(list_features[:10])<jupyter_output><empty_output><jupyter_text>Approach 3: Group by Genres<jupyter_code>df_songs = pd.read_csv('subset_every_feature.csv')
df_genre = df_songs['genre']
df_genre.head()
df_genre.value_counts()
df_curr = pd.concat([df, df_genre], axis = 1)
df_curr = pd.concat([df_curr[df_curr.genre == 'dance pop'], df_curr[df_curr.genre == 'pop']])
print(df_curr.shape)
# Define In- and Output
X = df_curr[features]
X_norm = normalize(X)
print('Size of Input: {}'.format(X_norm.shape))
Y = df_curr['popularity']
print('Size of Output: {}'.format(Y.shape))
# Create quantile bins for popularity
Y = pd.qcut(Y, 2, labels= False)
Y.head()
plt.figure()
Y.hist()
plt.title('Popularity Bins')
plt.show()
# Create train and test set
x_train, x_test, y_train, y_test = train_test_split(X_norm,Y, test_size=0.15, random_state=100)
print('Training Set: {}'.format(x_train.shape[0]))
print('Test Set: {}'.format(x_test.shape[0]))
model = performsMLModelComparison(x_train, x_test, y_train, y_test)<jupyter_output>C:\Users\mx190\Anaconda3\envs\data-x\lib\site-packages\sklearn\preprocessing\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.
  if diff:
C:\Users\mx190\Anaconda3\envs\data-x\lib\site-packages\sklearn\preprocessing\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.
  if diff:
C:\Users\mx190\Anaconda3\envs\data-x\lib\site-packages\sklearn\preprocessing\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.
  if diff:<jupyter_text>Approach 4: Feature Reducing<jupyter_code># Define In- and Output
X = df_features
X_norm = normalize(X)
print('Size of Input: {}'.format(X_norm.shape))
Y = df_output['popularity']
print('Size of Output: {}'.format(Y.shape))
# Create quantile bins for popularity
gapSize = 0.3
binSize = (1-gapSize)/2
Y = pd.qcut(Y, [0, binSize, binSize+gapSize, 1], labels= False)
# Throw away the middle one
df_curr = pd.concat([X, Y], axis = 1)
n = df_curr.shape[0]
df_curr = df_curr[df_curr.popularity!=1]
print('Reduced data set to {} from {}:'.format(df_curr.shape[0], n))
df_curr.popularity = df_curr.popularity.replace(2,1)
# Get list of features
rankedFeatures = list(dict_features.keys())
n = len(rankedFeatures)
testAcc = list()
model = RandomForestClassifier(n_estimators=140)
vec = np.arange(5,n,2)
for i in vec:
    X = df_curr[rankedFeatures[:i+1]]
    X_norm = normalize(X)
    Y = df_curr.popularity
    print('Size of Input: {}'.format(X_norm.shape))
    print('Size of Output: {}'.format(Y.shape))
    x_train, x_test, y_train, y_test = train_test_split(X_norm,Y, test_size=0.15, random_state=100)
    
    model = runsModel(x_train, x_test, y_train, y_test, model, printing = False)
    testAcc.append(model.score(x_test, y_test))
    
plt.figure()
plt.plot(vec, testAcc)
plt.xlabel('number of features')
plt.ylabel('test accuracy')
plt.title('Feature selection')
plt.show()
maxTestAcc = max(testAcc)
optFeatures = vec[(testAcc.index(maxTestAcc))]+1

print('Max. test accuracy of {} at {} features'.format(maxTestAcc, optFeatures))<jupyter_output><empty_output><jupyter_text>Tune Hyperparameters<jupyter_code>n_features = 60
gapSize = 0.3
# Define In- and Output
X = df_features[rankedFeatures[:n_features]]
print('Size of Input: {}'.format(X.shape))
Y = df_output['popularity']
print('Size of Output: {}'.format(Y.shape))

# Create quantile bins for popularity
binSize = (1-gapSize)/2
Y = pd.qcut(Y, [0, binSize, binSize+gapSize, 1], labels= False)
# Throw away the middle one
df_curr = pd.concat([X, Y], axis = 1)
n = df_curr.shape[0]
df_curr = df_curr[df_curr.popularity!=1]
print('Reduced data set to {} from {}:'.format(df_curr.shape[0], n))
df_curr.popularity = df_curr.popularity.replace(2,1)

X = df_curr.iloc[:,:-1]
print(X.shape)
X_norm = normalize(X)
Y = df_curr.popularity
print(Y.shape)
x_train, x_test, y_train, y_test = train_test_split(X_norm,Y, test_size=0.15, random_state=100)
model = RandomForestClassifier(n_estimators=140)
model = runsModel(x_train, x_test, y_train, y_test, model)
# Tune Hyperparameter
n_estimators = np.arange(40, 300, 5)
testAcc = list()
for i in n_estimators:
    model = RandomForestClassifier(n_estimators=i)
    model = runsModel(x_train, x_test, y_train, y_test, model, printing=False)
    testAcc.append(model.score(x_test, y_test))
    if(i%50==0):
        print('Still going...')

plt.figure()
plt.plot(n_estimators, testAcc)
plt.xlabel('n_estimators')
plt.ylabel('test accuracy')
plt.title('Hyperparamter Tuning')
plt.show()
maxTestAcc = max(testAcc)
optEstimators = n_estimators[(testAcc.index(maxTestAcc))]

print('Max. test accuracy of {} at n_estimtors = {}'.format(maxTestAcc, optEstimators))<jupyter_output><empty_output><jupyter_text>Best run for popularity<jupyter_code># Define In- and Output
X = df_features[rankedFeatures[:n_features]]
print('Size of Input: {}'.format(X.shape))
Y = df_output['popularity']
print('Size of Output: {}'.format(Y.shape))

# Create quantile bins for popularity
binSize = (1-gapSize)/2
Y = pd.qcut(Y, [0, binSize, binSize+gapSize, 1], labels= False)
# Throw away the middle one
df_curr = pd.concat([X, Y], axis = 1)
n = df_curr.shape[0]
df_curr = df_curr[df_curr.popularity!=1]
print('Reduced data set to {} from {}:'.format(df_curr.shape[0], n))
df_curr.popularity = df_curr.popularity.replace(2,1)

X = df_curr.iloc[:,:-1]
print(X.shape)
X_norm = normalize(X)
Y = df_curr.popularity
print(Y.shape)
x_train, x_test, y_train, y_test = train_test_split(X_norm,Y, test_size=0.15, random_state=100)
model = RandomForestClassifier(n_estimators=100)
model = runsModel(x_train, x_test, y_train, y_test, model)<jupyter_output>Test Accuracy: 0.6445783132530121
Training Accuracy: 0.9968085106382979
AUC Score: 0.6438953488372093
Confusion Matrix
    0   1
0  57  29
1  30  50<jupyter_text>Predict regional charts<jupyter_code># Define In- and Output
X = df_features
X_norm = normalize(X)
print('Size of Input: {}'.format(X_norm.shape))
Y = df_output
Y = Y.drop('popularity', axis = 1)
print('Size of Output: {}'.format(Y.shape))
import warnings
warnings.filterwarnings('ignore')
testAccuracy = list()
aucScore = list()
for i in range(Y.shape[1]):
    print('Performs prediction at Country: {}'.format(Y.columns[i]))
    Y_curr = Y.iloc[:,i]
    x_train, x_test, y_train, y_test = train_test_split(X_norm,Y_curr, test_size=0.15, random_state=100)
    bestModel = performsMLModelComparison(x_train, x_test, y_train, y_test, verbose = False)
    testAccuracy.append(bestModel.score(x_test, y_test))
    fpr, tpr, thresholds = roc_curve(y_test, bestModel.predict(x_test))
    aucScore.append(auc(fpr, tpr))
countryNames = list(Y.columns)
plt.figure()
plt.bar(range(len(countryNames)), testAccuracy, align='center')
#plt.xticks(range(len(countryNames)), countryNames)
plt.title('Test Accuracy for different countries')
plt.show()
print('Average test accuracy: {}'.format(np.mean(testAcc)))
print('Best test accuracy: {}'.format(np.max(testAcc)))
print('Worst test accuracy: {}'.format(np.min(testAcc)))
countryNames = list(Y.columns)
plt.figure()
plt.bar(range(len(countryNames)), aucScore, align='center')
plt.xticks(range(len(countryNames)), countryNames)
plt.title('AUC Score for different countries')
plt.show()
print('Average auc Score: {}'.format(np.mean(aucScore)))
print('Best auc Score: {}'.format(np.max(aucScore)))
print('Worst auc Score: {}'.format(np.min(aucScore)))<jupyter_output><empty_output><jupyter_text>Try DNN on dataset<jupyter_code>X = df_timbre.iloc[:, :12]
X.head()
X['timbreAll'] = X['timbre_all_1']
for j in range(X.shape[0]):
    for i in range(X.shape[1]):
        currentList = X.iloc[j, i]
        for k in range(len(currentList)):
            X['timbreAll'] [j].append(currentList[k])
    if (j%50 ==0):
        print('Finished {} rows'.format(j))
print('Length of Timbre Vector: ')
print(len(X['timbreAll'][1]))
X = X['timbreAll']
X = pd.DataFrame(X)
Y = df_output
Y = Y.drop('popularity', axis = 1)
print(X.shape)
print(Y.shape)
def f_number(row, number):
    try:
        output = row[number]
    except IndexError:
        output = np.nan
    return(output)
functionList = list()
n = len(X.iloc[0, 0])
for i in range(n):
    double = lambda row: f_number(row, i)
    functionList.append(double)
len(functionList)
for i in range(77):
    X['timbreValue{}'.format(i+1)] = X['timbreAll'].apply(functionList[i])

print(X.isnull().sum())
print(X['timbreAll'][7])
n<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1485566278146802, 'fraction_numerical': 0.02297698124242804, 'mean_word_length': 5.4819929596533985, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '42162', 'license': 'MIT', 'n_tokens_mistral': 9217, 'n_tokens_neox': 8744, 'n_words': 2002, 'path': 'ML Models/Big Dataset/finalNotebook_predictRegionalSuccess.ipynb', 'repo_name': 'liams32/Spotify_Analysis', 'size': '23937'}