{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"This is my my first kernel in kaggle to solve Titanic dataset.\nWould appreciate your feedback and comments. And don't forget to Like if you like it!"},{"metadata":{"_uuid":"4e12ab9f3d7ade9e6ff2a1173acb30a5d7791328"},"cell_type":"markdown","source":"**Solution Approach that I have followed**\n1. Describe and visualize the data\n2. Impute any missing values\n3. Convert any Categorical features into numeric, one-hot encode.\n4. Find Co-relation among all the features and the solution goal\n5. Feature engineer and create/ change/ convert the features  \n6. Drop/ discard any feature that is not contributing to the analysis.\n7. Check for any incorrect data, outliers\n"},{"metadata":{"_uuid":"40aa1720953cf024ceb03af26b1b27db8d2d44c9","trusted":true,"scrolled":false},"cell_type":"code","source":"#data analysis\nimport numpy as np\nimport pandas as pd\nimport re\n\n#visualization\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nsns.set(style=\"ticks\", color_codes=True)\n\n#machine learning\nfrom sklearn.compose import ColumnTransformer, make_column_transformer\nfrom sklearn.preprocessing import LabelEncoder, OneHotEncoder, Imputer, StandardScaler, Normalizer\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.linear_model import LogisticRegression","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0866fc84d1f1816c163dbd018a159a5ee0f752a5"},"cell_type":"code","source":"train_data = pd.read_csv('../input/train.csv')\ntest_data = pd.read_csv('../input/test.csv')\ncombine_data = [train_data, test_data]\nprint(train_data.info())\nprint(test_data.info())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3061249c8309b1eb38adc6ae9997312715705547"},"cell_type":"markdown","source":"Categorical features - Sex, Embarked, Survived\nOrdinal - Pclass\nNumerical features -\n    Discrete - SibSp, Parch\n    Continous - Age, Fare"},{"metadata":{"_uuid":"420bb04dcf30a7045e60fba9e2c789da852af5dd"},"cell_type":"markdown","source":"First look at the data shows that - Training has 891 records and Test has 418.\nThere are missing values in  Age, Cabin and Embarked.\nLets impute the missing values\n\nFor Age, let us substitute the mean of the Age grouped by sex, pclass, embarked"},{"metadata":{"trusted":true,"_uuid":"432cb10e9d4f864116e88d2bb8e65a7320650412"},"cell_type":"code","source":"#numeric data\ntrain_data.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"d3260569338ce9f75a5dd183ba984de26cd2df19"},"cell_type":"code","source":"#categorical data\ntrain_data.describe(include=['O'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0d1e2b04687b04d069acb950c5f81e8aa198fb1c"},"cell_type":"code","source":"#view first 5 rows\nprint(train_data.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e164a20cd7cfdce21eb583152b5936b5e15d4bb1"},"cell_type":"code","source":"#view Name feature, top 5 rows\nprint(train_data.Name.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bea1aeedbf33d18172ef867c2c5358beb7007b02"},"cell_type":"code","source":"#Survival rate per Class. \"1\" class passengers have highest survival rate.\ntrain_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index = False).mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc92a24a860a62060e2d5f3e52d9f7818fda84f5"},"cell_type":"code","source":"#Survival rate Gender-wise. \n#Females-  74.2% have survived. \n#Males- 18.89% have survived.\ntrain_data[['Sex', 'Survived']].groupby(['Sex'], as_index = False).mean().sort_values(by = 'Survived', ascending = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a0c63baea49c3940dfb89a7a4ea2a925f72ba0c"},"cell_type":"code","source":"#compared to singles (no siblings and spouse), small families have higher survival rate\n#Lagre families didnt survive\ntrain_data[['SibSp', 'Survived']].groupby(['SibSp'], as_index = False).mean().sort_values(by = 'Survived', ascending = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe02e342bec728b7a7cb7a81ba0bdfc8fe48738e"},"cell_type":"code","source":"#Same observation as \"SibSp\"\n#Small families have higher survival rate, compared to passengers travelling alone and larger families\ntrain_data[['Parch', 'Survived']].groupby(['Parch'], as_index=False).mean().sort_values(by='Survived', ascending = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"feb346998372668615fea42fd8445910d01ca9ff"},"cell_type":"code","source":"#\"SibSp\" and \"Parch\" both indicate the family size. So let us have single feature \"Familysize\"  \n#and drop these 2 features\ntrain_data['Familysize'] = train_data['Parch'] + train_data['SibSp'] + 1\ntest_data['Familysize'] = test_data['Parch'] + test_data['SibSp'] + 1\n\ntrain_data = train_data.drop(['Parch', 'SibSp'], axis = 1)\ntest_data = test_data.drop(['Parch', 'SibSp'], axis = 1)\ncombine_data = [train_data, test_data]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"399054051773ce53297f0721760cec9786212ba9"},"cell_type":"code","source":"#\"Cabin\" has lot of missing values. 204/891 are available. It doesnt help in analysis. So, drop it.\n#\"Ticket\" value is unique for 681/891 records. Alpha numeric ticket number, shared by 210 records. \n#No visible pattern. So drop these two columns it doesnt help in analysis.\n\ntrain_data = train_data.drop(['Cabin', 'Ticket'], axis = 1)\ntest_data = test_data.drop(['Cabin', 'Ticket'], axis = 1)\n\ncombine_data = [train_data, test_data]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6aac0bf277792e3738da3707b660d36d4920353d"},"cell_type":"code","source":"# Define function to extract titles from passenger names\ndef extract_title(name):\n    title_search = re.search(' ([A-Za-z]+)\\.', name)\n    # If the title exists, extract and return it.\n    if title_search:\n        return title_search.group(1)\n    return \"\"\n\n# Create a new feature Title, containing the titles of passenger names\nfor dataset in combine_data:\n    dataset['Title'] = dataset['Name'].apply(extract_title)\n\n#Check if the Titles are properly aligned as per the Sex of the passenger\npd.crosstab(train_data['Title'], train_data['Sex'])\n\n#Title feature will help in data analysis. So, retain it.\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e00951c75f20a743e4860b85398b426ab42f3611"},"cell_type":"code","source":"#Some rare titles can be grouped together, which have very few ( 2-3) passengers mapped.\nfor dataset in combine_data:\n    dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')\n\n    dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')\n    dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')\n    dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ab575e94819ae22b0e2b8633fddf9a1e2dd83fff"},"cell_type":"raw","source":""},{"metadata":{"trusted":true,"_uuid":"16b34f8689b771b5183a6ba7453a8b2bd2f89612"},"cell_type":"code","source":"#Survival rate based on Title\ntrain_data[['Title', 'Survived']].groupby(['Title'], as_index = False).mean()\n\n#Name feature can be dropped, as we have extracted the Title and this is not used further for analysis.\n#PassengerId is just a sequential numbering for the data records. Can be droppped\ntrain_data = train_data.drop(['Name', 'PassengerId'], axis = 1)\ntest_data = test_data.drop(['Name', 'PassengerId'], axis = 1)\ncombine_data = [train_data, test_data]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d067af1b10f8a74371c15a7f2758241e5ca03859"},"cell_type":"code","source":"title_mapping = {'Mr' : 1, 'Miss' : 2, 'Master' : 3, 'Mrs' : 4, 'Rare' : 5}\nfor dataset in combine_data:\n    dataset['Titlecode'] = dataset['Title'].map(title_mapping)\n    dataset['Titlecode'] = dataset['Title'].fillna(0)    \ntrain_data['Titlecode'] = train_data['Title'].map(title_mapping)\ntest_data['Titlecode'] = test_data['Title'].map(title_mapping)\ncombine_data = [train_data, test_data]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83cdc206724749e28030e3962d946f0ea6c08b19"},"cell_type":"code","source":"#Most of the passengers who have paid extremely higher price (Fare>80) have survived\n#Most of the Passengers who have paid extremely lower price (Fare<20) have nor survived\n# ore Females have paid higher Fare\ngrid = sns.FacetGrid(train_data, col='Survived', hue = \"Sex\")\ngrid.map(plt.hist, 'Fare', alpha=.5, bins = 6)\ngrid.add_legend();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"727ac23b0689593e3b940fabf347ba391648bcf1"},"cell_type":"code","source":"# Comparitively more Female pssengers have paid higher Fare\n# Age >65 senior citizens are Male passengers\n# oldest Male passenger has survived.\n# High Fare paying passengers have survived.\ngrid = sns.FacetGrid(train_data, col = 'Survived', hue='Sex')\ngrid.map(plt.scatter, 'Fare','Age')\ngrid.add_legend();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"81b11c70ec7b306e2809921b336587557be90f50"},"cell_type":"code","source":"#Third class (Pclass = 3) embarked in either \"S\" or \"Q\"\n#First class (Pclass = 1) embarked mostly in \"C\".\ngrid = sns.FacetGrid(train_data, hue='Embarked')\ngrid.map(plt.hist, 'Pclass',alpha = 0.9, bins=5)\ngrid.add_legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"061091b5af86956c78ec60f8a1e7829af5aeea1a"},"cell_type":"code","source":"#First class passengers (Pclass = 1) have mostly embarked in \"C\". Had good survival rate.\n#Third class passengers(PClass = 3) embarked in \"S\"have suffered the most.\ngrid = sns.FacetGrid(train_data, col = 'Survived', hue='Embarked')\ngrid.map(plt.hist, 'Pclass',alpha = 0.9, bins=5)\ngrid.add_legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d2bdb5f0f4ddab0144475ed0cc621cf08261e75"},"cell_type":"code","source":"#Singles and larger families (Familysize>4) have least survival rate\ngrid = sns.FacetGrid(train_data, col = 'Survived', hue='Sex')\ngrid.map(plt.hist, 'Familysize',alpha = 0.6, bins=11)\ngrid.add_legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c937054e8a3d02d588ac86084bc9a6f6cad46a22"},"cell_type":"code","source":"#Singles and larger families travelled in third class (Pclass = 3)\n#Smaller families and fewer singles travelled in first or second class\nsns.catplot(y=\"Familysize\", hue=\"Pclass\", kind=\"count\", edgecolor=\".6\",data=train_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6344ef2f40dd57bfc19a1375cbde6130e6e95687"},"cell_type":"code","source":"#Survival rate Familysize-wise. \n#Smaller size families have higher survival rate, together. Large families have perished together.\ntrain_data[['Familysize', 'Survived']].groupby(['Familysize'], as_index = False).mean().sort_values(by = 'Survived', ascending = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"824ce0485e23fe4a860b19c5061fcfd6bae44948"},"cell_type":"code","source":"#Males- 18.89% have survived.\ntrain_data[['Sex', 'Survived']].groupby(['Sex'], as_index = False).mean().sort_values(by = 'Survived', ascending = False)\n\npd.crosstab(train_data['Embarked'], train_data['Sex'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"436e213f45b231f3596832cf63e1bd71a8406524"},"cell_type":"code","source":"#Larger families (Familysize > 4) have embarked in \"S\". Have least survival rate\npd.crosstab(train_data['Familysize'], [train_data['Survived'], train_data['Embarked']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f2fbc0fe33095818a9b1c79f0b51a312c566d868"},"cell_type":"code","source":"#Females, minor Passengers embarked in \"C\" and \"Q\" have mostly survived.\npd.crosstab(train_data['Title'], [train_data['Survived'], train_data['Embarked']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4dc85298ac68fe3ffa94b4f511deb7869139c82f"},"cell_type":"code","source":"#Almost all the children, Females travelling in first and second class have Survived. \n#However, the children travelling in third class (embarked in \"S\") have been penalised along with their parents.\n#So families sink or survive together.\npd.crosstab(train_data['Title'], [train_data['Survived'], train_data['Pclass']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9390936e4bc180cf75577485da9233cdc13106b"},"cell_type":"code","source":"grid = sns.FacetGrid(train_data, col = 'Survived', row = 'Pclass', height = 2.2, aspect = 2 )\ngrid.map(plt.hist, 'Age', bins = 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01a5c4fee7d013a5e1d161bb7a2007b008d8d005"},"cell_type":"code","source":"grid = sns.FacetGrid(train_data, row = 'Embarked')\ngrid.map(sns.pointplot, 'Pclass', 'Survived', 'Sex',palette = 'deep')\ngrid.add_legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"caed6602f077281eac20db012f266e9fbdba36de"},"cell_type":"code","source":"grid = sns.FacetGrid(train_data, row = 'Embarked', col = 'Survived')\ngrid.map(sns.barplot, 'Sex', 'Fare')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"043d0088c998bbbec955a0d96753938cb82c7c28"},"cell_type":"code","source":"guessed_ages = np.zeros(5)\nguessed_ages","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9e86b93f465a6432f2305b07925419a66a2b53b","_kg_hide-output":true},"cell_type":"code","source":"#Impute the missing \"Age\" data. \n#Find the Median age per Title and use that to impute missing data\nfor dataset in combine_data:   \n    for i in range(0,5):        \n        guess_age = dataset[(dataset['Titlecode'] == i+1)]['Age'].dropna()\n        guess_age = guess_age.median()\n        guess_age = int( guess_age/0.5 + 0.5 ) * 0.5 \n        guessed_ages[i] = guess_age\n      \n    for i in range(0,5):          \n        dataset.loc[ (dataset.Age.isnull()) & (dataset.Titlecode == i+1), 'Age'] = guessed_ages[i]\n    \n    dataset['Age'] = dataset['Age'].astype(int)   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"249e3dd2d3175e67a2955caa793fab7765ed0258"},"cell_type":"code","source":"#Impute the missing \"Embarked\" data\n#889 records have not null values. two records have missing values. \n#These can be the Mode of the dataset.\ntrain_data['Embarked'].count() \nfreq_embarked = train_data['Embarked'].mode()[0]\n#print(freq_embarked)\ntrain_data.loc[(train_data.Embarked.isnull()), 'Embarked'] = freq_embarked\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5701b4730b40c8b8648217075013604658159ad6"},"cell_type":"code","source":"test_data['Fare'].fillna(test_data['Fare'].dropna().median(), inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f7d5ba38003df7acf7f4692616fd6382da2cd2a"},"cell_type":"code","source":"#place the passengers in the bins based on their Age. \n#\"pd.cut\" divides the data based on the Age, into 5 bins\n#train_data['AgeBins'] --> holds array-like object representing respective Age bin for each passenger\n\n\n#Also, place the passengers in the bins based on the Fare.\n#\"pd.qcut\" divides the data based on ticker fare, into ordered 5 bins.\n#train_data['FareBin'] --> holds array-like object representing respective Fare bin for each passenger\nfor dataset in combine_data:\n    dataset['AgeBin'] = pd.cut(dataset['Age'], 5)\n    dataset['FareBin'] = pd.qcut(dataset['Fare'], 4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83200d6955aec24171818bead9a0ce72bafbb06b"},"cell_type":"code","source":"# Mapping Fare\nfor dataset in combine_data:\n    dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0\n    dataset.loc[(dataset['Fare'] > 7.91) & (dataset['Fare'] <= 14.454), 'Fare'] = 1\n    dataset.loc[(dataset['Fare'] > 14.454) & (dataset['Fare'] <= 31), 'Fare']   = 2\n    dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3\n    dataset['Fare'] = dataset['Fare'].astype(int)\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58deb1763ad4bf4e4c157e4d021b70317545b9e3"},"cell_type":"code","source":"# Mapping Age\nfor dataset in combine_data:\n    dataset.loc[ dataset['Age'] <= 16, 'Age']  = 0\n    dataset.loc[(dataset['Age'] > 16) & (dataset['Age'] <= 32), 'Age'] = 1\n    dataset.loc[(dataset['Age'] > 32) & (dataset['Age'] <= 48), 'Age'] = 2\n    dataset.loc[(dataset['Age'] > 48) & (dataset['Age'] <= 64), 'Age'] = 3\n    dataset.loc[ dataset['Age'] > 64, 'Age'] = 4 \n    dataset['Age'] = dataset['Age'].astype(int)\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5ed3db7b7b863988eff14f2b5e8d4d03a67f5ff"},"cell_type":"code","source":"train_data = train_data.drop(['Title','AgeBin','FareBin'], axis = 1)\ntest_data = test_data.drop(['Title','AgeBin','FareBin'], axis = 1)\ncombine_data = [train_data, test_data]\n\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88fd3fe6a1d323d84980f44064ed59a952a6bc56"},"cell_type":"code","source":"for dataset in combine_data:\n    dataset['Embarked'] = dataset['Embarked'].map( {'S': 0, 'C': 1, 'Q': 2} ).astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df2bd0b3defc0510e6862cec7d741ee1ce7603a9"},"cell_type":"code","source":"#there is a missing 'Sex' value in test_data\nfor dataset in combine_data:\n    dataset['Sex'] = dataset['Sex'].map({'male' : 0, 'female' : 1}).astype(int)\n\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85c57be800501f3bc97fcd4ff35bbd3677cd446a"},"cell_type":"code","source":"#Now that all the categorical features are converted into numeric, \n#Sex --> Already one hot encoded #Age and Fare -->Ordinal data #Embarked, Pclass, Titlecode --> need to one hot encode\nfeatures = train_data[['Pclass', 'Sex', 'Age', 'Fare', 'Embarked', 'Familysize', 'Titlecode']]\ntarget = train_data['Survived']\npreprocess = make_column_transformer(    \n    (OneHotEncoder(sparse=False), ['Pclass', 'Sex', 'Embarked','Titlecode']), \n    remainder= StandardScaler())\n\npreprocess.transformers\ntrain_x = preprocess.fit_transform(features)\ntrain_y = target.values\n\ntrain_x[1]\n\n#Preprocess and transform test features\nfeatures_test = test_data[['Pclass', 'Sex', 'Age', 'Fare', 'Embarked', 'Familysize', 'Titlecode']]\npreprocess = make_column_transformer(    \n    (OneHotEncoder(sparse=False), ['Pclass', 'Sex', 'Embarked','Titlecode']), \n    remainder= StandardScaler())\npreprocess.transformers\ntest_x = preprocess.fit_transform(features_test)\n#model = make_pipeline(preprocess, LogisticRegression())\n#model.fit(train_x, train_y)\n#print(\"logistic regression score: %f\" % model.score(test_x, test_y))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"829c5a23892ad8048e8d65b9cf12edf8c7ad5988"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Activation, Dropout\nimport keras\n#from keras.optimizers import SGD\nfrom keras.wrappers.scikit_learn import KerasClassifier\nfrom sklearn.model_selection import GridSearchCV\nimport graphviz","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0057c4fbf0029b22851ad6268c8b45492615df5b"},"cell_type":"code","source":"#creating the model\ndef build_classifier(optimizer):  \n    #initialize ANN\n    classifier = Sequential()\n    #Adding the input parameters and first layer to ANN\n    classifier.add(Dense(units = 20, \n                         kernel_initializer = 'uniform', \n                         activation = 'relu', \n                         input_dim = 16))\n    \n    #Adding the dropout layer\n    classifier.add(Dropout(0.5))\n    \n    #Adding the second layer to ANN\n    classifier.add(Dense(units = 60, \n                         kernel_initializer = 'uniform', \n                         activation = 'relu'))\n    \n    #Adding the dropout layer\n    classifier.add(Dropout(0.5))\n    \n    #Adding the output layer that is binary\n    classifier.add(Dense(units = 1, \n                         kernel_initializer = 'uniform', \n                         activation = 'sigmoid'))\n    \n    classifier.summary()\n    \n    #with the scalar sigmoid output on a binary classification problem, use the binary cross entropy loss function\n    classifier.compile(optimizer = optimizer, \n                       loss = 'binary_crossentropy', \n                       metrics = ['accuracy'] )\n    \n    return classifier\n\n#Use KerasClassifier\nclassifier = KerasClassifier(build_fn = build_classifier)\n\n#create a dictionary for the hyper parameters\nparameters = {'batch_size' : [60, 30],\n               'nb_epoch' : [30, 50],\n               'optimizer' : ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']}\n\n# 'optimizer' : ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']}\n#GridSearchCV implements fit and predict\ngrid_search = GridSearchCV(estimator = classifier,\n                           param_grid = parameters,\n                           scoring = 'accuracy',\n                           cv = 3)\n\ngrid_search = grid_search.fit(train_x, train_y)\nbest_estimator = grid_search.best_estimator_\nbest_parameters = grid_search.best_params_\nbest_score = grid_search.best_score_\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bdaf20f82a0622f3e39c589ff42048791ee0acd9"},"cell_type":"code","source":"print(\"best_estimator %s\" %best_estimator)\nprint(\"best_parameters %s\" %best_parameters)\nprint(\"best_score %f\" %best_score)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}