{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Music Genre Classification Project Using Machine Learning Techniques**","metadata":{}},{"cell_type":"markdown","source":"# Importing required packages\n","metadata":{}},{"cell_type":"code","source":"#import the packeges \nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\nimport warnings\n\n\n\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.datasets import fetch_openml\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder\nfrom sklearn.model_selection import train_test_split, GridSearchCV\nfrom sklearn.preprocessing import OrdinalEncoder\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.linear_model import SGDClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom xgboost import XGBClassifier\nfrom sklearn.svm import SVC\nfrom imblearn.over_sampling import SMOTE\nfrom imblearn.pipeline import Pipeline \nfrom sklearn.model_selection import cross_val_score\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:35.371594Z","iopub.execute_input":"2022-07-07T21:54:35.371958Z","iopub.status.idle":"2022-07-07T21:54:36.584275Z","shell.execute_reply.started":"2022-07-07T21:54:35.371929Z","shell.execute_reply":"2022-07-07T21:54:36.583348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv('../input/shai-music-genre-classification/train (1).csv')\ndata_test=pd.read_csv('../input/shai-music-genre-classification/test (2).csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:56:30.097743Z","iopub.execute_input":"2022-07-07T21:56:30.098095Z","iopub.status.idle":"2022-07-07T21:56:30.174483Z","shell.execute_reply.started":"2022-07-07T21:56:30.098067Z","shell.execute_reply":"2022-07-07T21:56:30.173260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:37.754588Z","iopub.execute_input":"2022-07-07T21:54:37.754930Z","iopub.status.idle":"2022-07-07T21:54:37.785945Z","shell.execute_reply.started":"2022-07-07T21:54:37.754906Z","shell.execute_reply":"2022-07-07T21:54:37.785019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:38.381148Z","iopub.execute_input":"2022-07-07T21:54:38.382211Z","iopub.status.idle":"2022-07-07T21:54:38.408700Z","shell.execute_reply.started":"2022-07-07T21:54:38.382166Z","shell.execute_reply":"2022-07-07T21:54:38.408144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:39.152677Z","iopub.execute_input":"2022-07-07T21:54:39.153097Z","iopub.status.idle":"2022-07-07T21:54:39.225210Z","shell.execute_reply.started":"2022-07-07T21:54:39.153072Z","shell.execute_reply":"2022-07-07T21:54:39.224257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.columns\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:40.202600Z","iopub.execute_input":"2022-07-07T21:54:40.203026Z","iopub.status.idle":"2022-07-07T21:54:40.210012Z","shell.execute_reply.started":"2022-07-07T21:54:40.202996Z","shell.execute_reply":"2022-07-07T21:54:40.209098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:41.312129Z","iopub.execute_input":"2022-07-07T21:54:41.312898Z","iopub.status.idle":"2022-07-07T21:54:41.319818Z","shell.execute_reply.started":"2022-07-07T21:54:41.312862Z","shell.execute_reply":"2022-07-07T21:54:41.319158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking the missing value for the training data\ndata.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:42.407702Z","iopub.execute_input":"2022-07-07T21:54:42.408077Z","iopub.status.idle":"2022-07-07T21:54:42.422151Z","shell.execute_reply.started":"2022-07-07T21:54:42.408041Z","shell.execute_reply":"2022-07-07T21:54:42.421011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_value=round((data.isna().sum()/len(data))*100 ,2)\nmissing_value","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:43.467984Z","iopub.execute_input":"2022-07-07T21:54:43.468863Z","iopub.status.idle":"2022-07-07T21:54:43.482206Z","shell.execute_reply.started":"2022-07-07T21:54:43.468828Z","shell.execute_reply":"2022-07-07T21:54:43.480926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Analysis","metadata":{}},{"cell_type":"code","source":"data=data.drop(['Id'], axis=1)\ndata_test=data_test.drop(['Id'], axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:56:35.647838Z","iopub.execute_input":"2022-07-07T21:56:35.648243Z","iopub.status.idle":"2022-07-07T21:56:35.656038Z","shell.execute_reply.started":"2022-07-07T21:56:35.648192Z","shell.execute_reply":"2022-07-07T21:56:35.654954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['Class'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:53.325955Z","iopub.execute_input":"2022-07-07T21:54:53.327444Z","iopub.status.idle":"2022-07-07T21:54:53.335452Z","shell.execute_reply.started":"2022-07-07T21:54:53.327414Z","shell.execute_reply":"2022-07-07T21:54:53.334313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.unique(data['Class'])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:55.265275Z","iopub.execute_input":"2022-07-07T21:54:55.265676Z","iopub.status.idle":"2022-07-07T21:54:55.273005Z","shell.execute_reply.started":"2022-07-07T21:54:55.265646Z","shell.execute_reply":"2022-07-07T21:54:55.272117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#this indicate that the data is implance \nsns.countplot(x='Class', data=data)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:57.820446Z","iopub.execute_input":"2022-07-07T21:54:57.821166Z","iopub.status.idle":"2022-07-07T21:54:58.040556Z","shell.execute_reply.started":"2022-07-07T21:54:57.821138Z","shell.execute_reply":"2022-07-07T21:54:58.039781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(16, 9))\n\nsns.scatterplot(x=data['Popularity'],y=data['Class'])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:54:59.225420Z","iopub.execute_input":"2022-07-07T21:54:59.225833Z","iopub.status.idle":"2022-07-07T21:54:59.468431Z","shell.execute_reply.started":"2022-07-07T21:54:59.225802Z","shell.execute_reply":"2022-07-07T21:54:59.467607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features = data.select_dtypes(include=[np.number]).columns\ncategorical_features = data.select_dtypes(exclude=[np.number]).columns","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:00.968403Z","iopub.execute_input":"2022-07-07T21:55:00.969004Z","iopub.status.idle":"2022-07-07T21:55:00.977748Z","shell.execute_reply.started":"2022-07-07T21:55:00.968978Z","shell.execute_reply":"2022-07-07T21:55:00.976397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in numeric_features:\n    print(i)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:02.276696Z","iopub.execute_input":"2022-07-07T21:55:02.277083Z","iopub.status.idle":"2022-07-07T21:55:02.282231Z","shell.execute_reply.started":"2022-07-07T21:55:02.277054Z","shell.execute_reply":"2022-07-07T21:55:02.280880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for variable in numeric_features:\n    fig = plt.figure(figsize=(12, 8))\n    sns.scatterplot(x=data['Class'],y=data[variable])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:03.374315Z","iopub.execute_input":"2022-07-07T21:55:03.375440Z","iopub.status.idle":"2022-07-07T21:55:06.254662Z","shell.execute_reply.started":"2022-07-07T21:55:03.375377Z","shell.execute_reply":"2022-07-07T21:55:06.254124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.hist(figsize = (20,15),bins = 50)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:06.256234Z","iopub.execute_input":"2022-07-07T21:55:06.256482Z","iopub.status.idle":"2022-07-07T21:55:08.902113Z","shell.execute_reply.started":"2022-07-07T21:55:06.256456Z","shell.execute_reply":"2022-07-07T21:55:08.901378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"#plot box plot to indicate the outliers \nfor variable in numeric_features:\n    fig = plt.figure(figsize=(12, 8))\n    sns.boxplot(x=data[variable])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:08.903470Z","iopub.execute_input":"2022-07-07T21:55:08.904152Z","iopub.status.idle":"2022-07-07T21:55:10.636815Z","shell.execute_reply.started":"2022-07-07T21:55:08.904121Z","shell.execute_reply":"2022-07-07T21:55:10.636176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Processing**","metadata":{}},{"cell_type":"markdown","source":"# remove outliers","metadata":{}},{"cell_type":"code","source":"\nprint(numeric_features)\nprint(categorical_features)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:13.504905Z","iopub.execute_input":"2022-07-07T21:55:13.505450Z","iopub.status.idle":"2022-07-07T21:55:13.513803Z","shell.execute_reply.started":"2022-07-07T21:55:13.505424Z","shell.execute_reply":"2022-07-07T21:55:13.512872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#define function to remove outliers\ndef  outliers(df , ft ):\n    \n    Q1=df[ft].quantile(0.25)\n    Q3=df[ft].quantile(0.75)\n    IQR=Q3 - Q1\n    # Upper bound\n    upper = Q3+1.5*IQR\n    # Lower bound\n    lower = Q1-1.5*IQR\n\n    ls=df.index[(df[ft] < lower) | (df[ft] > upper)]\n    return ls ","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:19.979940Z","iopub.execute_input":"2022-07-07T21:57:19.980304Z","iopub.status.idle":"2022-07-07T21:57:19.987420Z","shell.execute_reply.started":"2022-07-07T21:57:19.980275Z","shell.execute_reply":"2022-07-07T21:57:19.986282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create empty list to store the list index\n\nindex_list=[]\nfor feature in numeric_features :\n    index_list.extend(outliers(data , feature ))","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:20.881348Z","iopub.execute_input":"2022-07-07T21:57:20.881705Z","iopub.status.idle":"2022-07-07T21:57:20.915407Z","shell.execute_reply.started":"2022-07-07T21:57:20.881679Z","shell.execute_reply":"2022-07-07T21:57:20.914549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(index_list)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:21.736999Z","iopub.execute_input":"2022-07-07T21:57:21.737406Z","iopub.status.idle":"2022-07-07T21:57:21.743653Z","shell.execute_reply.started":"2022-07-07T21:57:21.737374Z","shell.execute_reply":"2022-07-07T21:57:21.742587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def remove(df, ls):\n    ls=sorted(set(ls))\n    df=df.drop(ls)\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:22.730461Z","iopub.execute_input":"2022-07-07T21:57:22.731296Z","iopub.status.idle":"2022-07-07T21:57:22.735640Z","shell.execute_reply.started":"2022-07-07T21:57:22.731265Z","shell.execute_reply":"2022-07-07T21:57:22.734761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data= remove(data , index_list)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:23.917128Z","iopub.execute_input":"2022-07-07T21:57:23.917483Z","iopub.status.idle":"2022-07-07T21:57:23.925842Z","shell.execute_reply.started":"2022-07-07T21:57:23.917456Z","shell.execute_reply":"2022-07-07T21:57:23.925010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot box plot to indicate the outliers \nfor variable in numeric_features:\n    fig = plt.figure(figsize=(12, 8))\n    sns.boxplot(x=data[variable])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:19.651612Z","iopub.execute_input":"2022-07-07T21:55:19.652823Z","iopub.status.idle":"2022-07-07T21:55:21.729626Z","shell.execute_reply.started":"2022-07-07T21:55:19.652767Z","shell.execute_reply":"2022-07-07T21:55:21.728913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.corr()['Class'].sort_values(ascending = False)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:21.731115Z","iopub.execute_input":"2022-07-07T21:55:21.732111Z","iopub.status.idle":"2022-07-07T21:55:21.748960Z","shell.execute_reply.started":"2022-07-07T21:55:21.732074Z","shell.execute_reply":"2022-07-07T21:55:21.747775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrmat= data.corr()\nf, ax = plt.subplots(figsize=(16,12))\nsns.heatmap(corrmat,annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:21.750609Z","iopub.execute_input":"2022-07-07T21:55:21.751001Z","iopub.status.idle":"2022-07-07T21:55:22.811390Z","shell.execute_reply.started":"2022-07-07T21:55:21.750967Z","shell.execute_reply":"2022-07-07T21:55:22.810215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Selcetion and Data Spliting ","metadata":{}},{"cell_type":"code","source":"data['Class'] ","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:24.800693Z","iopub.execute_input":"2022-07-07T21:55:24.801970Z","iopub.status.idle":"2022-07-07T21:55:24.810622Z","shell.execute_reply.started":"2022-07-07T21:55:24.801938Z","shell.execute_reply":"2022-07-07T21:55:24.809816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:55:26.728378Z","iopub.execute_input":"2022-07-07T21:55:26.728690Z","iopub.status.idle":"2022-07-07T21:55:26.742353Z","shell.execute_reply.started":"2022-07-07T21:55:26.728666Z","shell.execute_reply":"2022-07-07T21:55:26.741514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=data.drop(['Track Name', 'Artist Name'], axis=1)\ndata_test=data_test.drop(['Track Name', 'Artist Name'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:56:45.700975Z","iopub.execute_input":"2022-07-07T21:56:45.701375Z","iopub.status.idle":"2022-07-07T21:56:45.711180Z","shell.execute_reply.started":"2022-07-07T21:56:45.701346Z","shell.execute_reply":"2022-07-07T21:56:45.710091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:56:48.320584Z","iopub.execute_input":"2022-07-07T21:56:48.321624Z","iopub.status.idle":"2022-07-07T21:56:48.349946Z","shell.execute_reply.started":"2022-07-07T21:56:48.321583Z","shell.execute_reply":"2022-07-07T21:56:48.348307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['Class']=data['Class'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:56:50.270459Z","iopub.execute_input":"2022-07-07T21:56:50.271335Z","iopub.status.idle":"2022-07-07T21:56:50.291120Z","shell.execute_reply.started":"2022-07-07T21:56:50.271294Z","shell.execute_reply":"2022-07-07T21:56:50.290312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:56:51.979745Z","iopub.execute_input":"2022-07-07T21:56:51.980335Z","iopub.status.idle":"2022-07-07T21:56:51.996295Z","shell.execute_reply.started":"2022-07-07T21:56:51.980303Z","shell.execute_reply":"2022-07-07T21:56:51.995540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:56:53.096447Z","iopub.execute_input":"2022-07-07T21:56:53.097135Z","iopub.status.idle":"2022-07-07T21:56:53.102625Z","shell.execute_reply.started":"2022-07-07T21:56:53.097098Z","shell.execute_reply":"2022-07-07T21:56:53.101893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.unique(data['Class'])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:56:54.501638Z","iopub.execute_input":"2022-07-07T21:56:54.503113Z","iopub.status.idle":"2022-07-07T21:56:54.511999Z","shell.execute_reply.started":"2022-07-07T21:56:54.503075Z","shell.execute_reply":"2022-07-07T21:56:54.510934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder=LabelEncoder()\ndata['Class'] = label_encoder.fit_transform(data['Class'])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:56:56.192065Z","iopub.execute_input":"2022-07-07T21:56:56.192447Z","iopub.status.idle":"2022-07-07T21:56:56.207386Z","shell.execute_reply.started":"2022-07-07T21:56:56.192417Z","shell.execute_reply":"2022-07-07T21:56:56.206287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.unique(data['Class'])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:56:57.851460Z","iopub.execute_input":"2022-07-07T21:56:57.851877Z","iopub.status.idle":"2022-07-07T21:56:57.860278Z","shell.execute_reply.started":"2022-07-07T21:56:57.851847Z","shell.execute_reply":"2022-07-07T21:56:57.859141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(label_encoder.classes_)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:00.822907Z","iopub.execute_input":"2022-07-07T21:57:00.823897Z","iopub.status.idle":"2022-07-07T21:57:00.829846Z","shell.execute_reply.started":"2022-07-07T21:57:00.823864Z","shell.execute_reply":"2022-07-07T21:57:00.829028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=data.drop(['Class'], axis=1)\ny=data['Class']\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:04.423964Z","iopub.execute_input":"2022-07-07T21:57:04.424368Z","iopub.status.idle":"2022-07-07T21:57:04.432093Z","shell.execute_reply.started":"2022-07-07T21:57:04.424337Z","shell.execute_reply":"2022-07-07T21:57:04.430936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:05.426258Z","iopub.execute_input":"2022-07-07T21:57:05.426911Z","iopub.status.idle":"2022-07-07T21:57:05.440161Z","shell.execute_reply.started":"2022-07-07T21:57:05.426872Z","shell.execute_reply":"2022-07-07T21:57:05.439109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features= data.select_dtypes(include=[np.number]).drop(['Class'], axis=1).columns\ncategorical_features=data.select_dtypes(exclude=[np.number]).columns\nprint(numeric_features)\nprint(categorical_features)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:36.303283Z","iopub.execute_input":"2022-07-07T21:57:36.303678Z","iopub.status.idle":"2022-07-07T21:57:36.318265Z","shell.execute_reply.started":"2022-07-07T21:57:36.303647Z","shell.execute_reply":"2022-07-07T21:57:36.316902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33,random_state = 42)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:39.586741Z","iopub.execute_input":"2022-07-07T21:57:39.587464Z","iopub.status.idle":"2022-07-07T21:57:39.596884Z","shell.execute_reply.started":"2022-07-07T21:57:39.587424Z","shell.execute_reply":"2022-07-07T21:57:39.595935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bulid Pipline","metadata":{}},{"cell_type":"code","source":"\nnumeric_transformer = Pipeline(\n    steps=[(\"imputer\", SimpleImputer(strategy=\"median\")),\n           (\"scaler\", StandardScaler())]\n)\n\ncategorical_transformer =  Pipeline( \n               steps=[(\"encoder\", OrdinalEncoder())]\n)\n\n\n\npreprocessor = ColumnTransformer(\n    transformers=[\n        (\"num\", numeric_transformer, numeric_features),\n         (\"cat\", categorical_transformer, categorical_features),\n        \n\n    ],\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:42.030209Z","iopub.execute_input":"2022-07-07T21:57:42.030587Z","iopub.status.idle":"2022-07-07T21:57:42.036669Z","shell.execute_reply.started":"2022-07-07T21:57:42.030558Z","shell.execute_reply":"2022-07-07T21:57:42.036036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bulid Model","metadata":{}},{"cell_type":"code","source":"\n# Building pipelins of  processor and model for varios Classifier.\n\n#pipline for linear Classifier model\npipeline_lr=Pipeline([(\"preprocessor\",preprocessor),\n                      ('smote', SMOTE(random_state=42)),\n                     (\"lr_clas\",LogisticRegression())])\n\n#pipline for Decision Tree Classifier\npipeline_dt=Pipeline([(\"preprocessor\",preprocessor),\n                      ('smote', SMOTE(random_state=42)),\n                     (\"dt_clas\",DecisionTreeClassifier())])\n\n#pipline for Random Forest Classifier/\npipeline_rf=Pipeline([(\"preprocessor\",preprocessor),\n                      ('smote', SMOTE(random_state=42)),\n                     (\"rf_clas\",RandomForestClassifier())])\n\n\n#pipline for KNeighbors Classifier\npipeline_kn=Pipeline([(\"preprocessor\",preprocessor),\n                      ('smote', SMOTE(random_state=42)),\n                     (\"kn_clas\",KNeighborsClassifier())])\n\n#pipline for Suport Vector  Regressor\npipeline_svm=Pipeline([(\"preprocessor\",preprocessor),\n                       ('smote', SMOTE(random_state=42)),\n                     (\"svm_clas\",SVC())])\n\n#pipline for  XGBClassifier  Classifier\npipeline_xgb=Pipeline([(\"preprocessor\",preprocessor),\n                       ('smote', SMOTE(random_state=42)),\n                       (\"xgb_clas\",XGBClassifier())])\n\n#pipline for  SGDClassifier  Classifier\n\npipeline_sgd=Pipeline([(\"preprocessor\",preprocessor),\n                       ('smote', SMOTE(random_state=42)),\n                     (\"sgd_clas\",SGDClassifier())])\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:43.999773Z","iopub.execute_input":"2022-07-07T21:57:44.000583Z","iopub.status.idle":"2022-07-07T21:57:44.009442Z","shell.execute_reply.started":"2022-07-07T21:57:44.000495Z","shell.execute_reply":"2022-07-07T21:57:44.008691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipelines = [pipeline_lr, pipeline_dt, pipeline_rf, pipeline_kn, pipeline_svm,pipeline_xgb,pipeline_sgd]\n\n# Dictionary of pipelines and model types for ease of reference\npipe_dict = {0: \"LogicticRegression\", 1: \"DecisionTree\", 2: \"RandomForest\",3: \"KNeighbors\", 4: \"Support Vector\",5:\"XGBClassifier\",6:\"SGDClassifier\"}","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:45.252694Z","iopub.execute_input":"2022-07-07T21:57:45.253134Z","iopub.status.idle":"2022-07-07T21:57:45.258591Z","shell.execute_reply.started":"2022-07-07T21:57:45.253103Z","shell.execute_reply":"2022-07-07T21:57:45.257501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.filterwarnings(\"ignore\")\n# Fit  all  pipelines\nfor pipe in pipelines:\n    print(pipe.fit(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:57:46.148372Z","iopub.execute_input":"2022-07-07T21:57:46.148972Z","iopub.status.idle":"2022-07-07T21:58:52.061977Z","shell.execute_reply.started":"2022-07-07T21:57:46.148944Z","shell.execute_reply":"2022-07-07T21:58:52.061099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_results_rms = []\n#scoring = ['','precision', 'recall', 'f1']\nfor i, model in enumerate(pipelines):\n    cv_score = cross_val_score(model, X_train,y_train,scoring='accuracy', cv=5)\n    cv_results_rms.append(cv_score)\n    print(\"%s : %f \" % (pipe_dict[i], cv_score.mean()))","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:58:52.063643Z","iopub.execute_input":"2022-07-07T21:58:52.063975Z","iopub.status.idle":"2022-07-07T22:03:40.194170Z","shell.execute_reply.started":"2022-07-07T21:58:52.063946Z","shell.execute_reply":"2022-07-07T22:03:40.193454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name=[]\nfor key, value in pipe_dict.items():\n    print(key, ' : ', value)\n    name.append(value)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T22:03:40.195436Z","iopub.execute_input":"2022-07-07T22:03:40.196527Z","iopub.status.idle":"2022-07-07T22:03:40.202393Z","shell.execute_reply.started":"2022-07-07T22:03:40.196490Z","shell.execute_reply":"2022-07-07T22:03:40.201589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name","metadata":{"execution":{"iopub.status.busy":"2022-07-07T22:03:40.204714Z","iopub.execute_input":"2022-07-07T22:03:40.205051Z","iopub.status.idle":"2022-07-07T22:03:40.218902Z","shell.execute_reply.started":"2022-07-07T22:03:40.205012Z","shell.execute_reply":"2022-07-07T22:03:40.217639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(12, 8))\nfig.suptitle('Algorithm Comparison')\nax = fig.add_subplot(111)\nplt.boxplot(cv_results_rms)\nax.set_xticklabels(name)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T22:03:40.220169Z","iopub.execute_input":"2022-07-07T22:03:40.220546Z","iopub.status.idle":"2022-07-07T22:03:40.489938Z","shell.execute_reply.started":"2022-07-07T22:03:40.220503Z","shell.execute_reply":"2022-07-07T22:03:40.488819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# modul Tuning","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T22:03:40.491016Z","iopub.execute_input":"2022-07-07T22:03:40.491326Z","iopub.status.idle":"2022-07-07T22:03:40.496261Z","shell.execute_reply.started":"2022-07-07T22:03:40.491295Z","shell.execute_reply":"2022-07-07T22:03:40.495048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {#\"xgb_clas__max_depth\":[2, 6, 12],\n              \"xgb_clas__learning_rate\":[0.3, 0.1, 0.03],\n              \"xgb_clas__n_estimators\":[100]}\ngrid_search = GridSearchCV(pipeline_xgb,param_grid,cv = 10, scoring = 'accuracy',return_train_score = True)\ngrid_search.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T22:03:40.497393Z","iopub.execute_input":"2022-07-07T22:03:40.497670Z","iopub.status.idle":"2022-07-07T22:17:36.729820Z","shell.execute_reply.started":"2022-07-07T22:03:40.497638Z","shell.execute_reply":"2022-07-07T22:17:36.728964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"search_score = cross_val_score(grid_search,\n                               X_train,\n                               y_train,\n                               scoring=\"accuracy\",\n                               cv=5)\nsearch_rmse_score=np.sqrt(search_score)\nprint(\"Scores: \", search_rmse_score)\nprint(\"Mean: \", search_rmse_score.mean())\nprint(\"Standard Deviation: \", search_rmse_score.std())","metadata":{"execution":{"iopub.status.busy":"2022-07-07T22:20:01.006869Z","iopub.execute_input":"2022-07-07T22:20:01.007246Z","iopub.status.idle":"2022-07-07T23:16:35.685868Z","shell.execute_reply.started":"2022-07-07T22:20:01.007217Z","shell.execute_reply":"2022-07-07T23:16:35.685041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analyze the Best Models and Their Errors","metadata":{}},{"cell_type":"code","source":"grid_search.best_params_\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T23:16:35.689142Z","iopub.execute_input":"2022-07-07T23:16:35.691090Z","iopub.status.idle":"2022-07-07T23:16:35.697431Z","shell.execute_reply.started":"2022-07-07T23:16:35.691024Z","shell.execute_reply":"2022-07-07T23:16:35.696419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cvres = grid_search.cv_results_\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T23:17:24.058055Z","iopub.execute_input":"2022-07-07T23:17:24.058462Z","iopub.status.idle":"2022-07-07T23:17:24.063889Z","shell.execute_reply.started":"2022-07-07T23:17:24.058431Z","shell.execute_reply":"2022-07-07T23:17:24.062698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor accuracy, params in zip(cvres[\"mean_test_score\"],cvres[\"params\"]):\n  print(accuracy, params)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T23:19:07.415077Z","iopub.execute_input":"2022-07-07T23:19:07.415415Z","iopub.status.idle":"2022-07-07T23:19:07.420182Z","shell.execute_reply.started":"2022-07-07T23:19:07.415390Z","shell.execute_reply":"2022-07-07T23:19:07.419481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate Your Model","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error","metadata":{"execution":{"iopub.status.busy":"2022-07-07T23:21:50.428271Z","iopub.execute_input":"2022-07-07T23:21:50.428663Z","iopub.status.idle":"2022-07-07T23:21:50.434048Z","shell.execute_reply.started":"2022-07-07T23:21:50.428633Z","shell.execute_reply":"2022-07-07T23:21:50.432681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model = grid_search.best_estimator_\nfinal_predictions = final_model.predict(X_test)\nfinal_mse = mean_squared_error(y_test,final_predictions)\nfinal_rmse = np.sqrt(final_mse)\nfinal_rmse","metadata":{"execution":{"iopub.status.busy":"2022-07-07T23:21:51.431487Z","iopub.execute_input":"2022-07-07T23:21:51.431885Z","iopub.status.idle":"2022-07-07T23:21:51.508971Z","shell.execute_reply.started":"2022-07-07T23:21:51.431853Z","shell.execute_reply":"2022-07-07T23:21:51.508054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = final_model.predict(data_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T23:21:53.768844Z","iopub.execute_input":"2022-07-07T23:21:53.769316Z","iopub.status.idle":"2022-07-07T23:21:53.821525Z","shell.execute_reply.started":"2022-07-07T23:21:53.769287Z","shell.execute_reply":"2022-07-07T23:21:53.820842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Submission File","metadata":{}},{"cell_type":"code","source":"submission_file = pd.read_csv(\"../input/shai-music-genre-classification/sample_submission (2).csv\")\nsubmission_file['Class'] = prediction.round().astype(int)\nsubmission_file.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T23:21:56.038291Z","iopub.execute_input":"2022-07-07T23:21:56.038901Z","iopub.status.idle":"2022-07-07T23:21:56.070957Z","shell.execute_reply.started":"2022-07-07T23:21:56.038861Z","shell.execute_reply":"2022-07-07T23:21:56.069434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file","metadata":{"execution":{"iopub.status.busy":"2022-07-07T23:21:57.046823Z","iopub.execute_input":"2022-07-07T23:21:57.047401Z","iopub.status.idle":"2022-07-07T23:21:57.059472Z","shell.execute_reply.started":"2022-07-07T23:21:57.047369Z","shell.execute_reply":"2022-07-07T23:21:57.058068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}