{"cells":[{"metadata":{},"cell_type":"markdown","source":"![](https://deelyee.files.wordpress.com/2011/01/i_hate_organic_chemistry_by_laughingwarlock.jpg?w=880&h=312&crop=1)"},{"metadata":{},"cell_type":"markdown","source":"This is a **Work in progress** I will write the narrative and develop it further soon. Stay tuned...."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_ = pd.read_csv('../input/train.csv', index_col = \"id\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_ = pd.read_csv('../input/test.csv', index_col = \"id\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_['atom_index_0'] = train_.atom_index_0.astype('category')\ntrain_['atom_index_1'] = train_.atom_index_1.astype('category')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_['atom_index_0'] = test_.atom_index_0.astype('category')\ntest_['atom_index_1'] = test_.atom_index_1.astype('category')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_.describe(include = 'all')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dipole_ = pd.read_csv('../input/dipole_moments.csv')\n# magnetic_ = pd.read_csv('../input/mag')\npotential_ = pd.read_csv('../input/potential_energy.csv')\nscalar_ = pd.read_csv('../input/scalar_coupling_contributions.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"scalar_['atom_index_0'] = scalar_.atom_index_0.astype('category')\nscalar_['atom_index_1'] = scalar_.atom_index_1.astype('category')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"scalar_.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"potential_.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dm_ = pd.merge(train_, dipole_, how = 'inner', on = 'molecule_name')\ntrain_dm_pe = pd.merge(train_dm_, potential_, how = 'inner', on = 'molecule_name')\ntrain_dm_pe_s = pd.merge(train_dm_pe, scalar_, how = 'inner', on = ['molecule_name', 'atom_index_0', 'atom_index_1', 'type'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dm_pe_s.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dm_pe_s.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\nsns.set(rc={'figure.figsize':(15,12)})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.heatmap(train_dm_pe_s.corr(), annot=True, fmt=\"f\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.regplot(x = \"fc\", y = \"scalar_coupling_constant\", data = train_dm_pe_s)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.lmplot(x = \"fc\", y = \"scalar_coupling_constant\", data = train_dm_pe_s)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.lmplot(x = \"dso\", y = \"scalar_coupling_constant\", data = train_dm_pe_s)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.lmplot(x = \"fc\", y = \"scalar_coupling_constant\", col = \"type\", col_wrap = 3, data = train_dm_pe_s)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.lmplot(x = \"fc\", y = \"scalar_coupling_constant\", col = \"atom_index_0\", col_wrap = 2, data = train_dm_pe_s)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.lmplot(x = \"fc\", y = \"scalar_coupling_constant\", col = \"atom_index_1\", col_wrap = 2, data = train_dm_pe_s)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_.describe(include = 'all')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_['scalar_coupling_constant'].describe().apply(lambda x: format(x, 'f'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_mol = train_.loc[train_['molecule_name'] == 'dsgdb9nsd_042139']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_mol.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = sns.heatmap(pd.crosstab(train_.atom_index_0, train_.type), annot = True, fmt = \"d\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = sns.heatmap(pd.crosstab(test_.atom_index_0, test_.type), annot = True, fmt = \"d\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ay = sns.heatmap(pd.crosstab(train_.atom_index_1, train_.type), annot = True, fmt = \"d\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ay = sns.heatmap(pd.crosstab(test_.atom_index_1, test_.type), annot = True, fmt = \"d\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.set(rc={'figure.figsize':(25,15)})\naz = sns.heatmap(pd.crosstab(train_.atom_index_1, train_.atom_index_0), annot = True, fmt = \"d\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"az = sns.heatmap(pd.crosstab(test_.atom_index_1, test_.atom_index_0), annot = True, fmt = \"d\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**This is a disjoint set.**"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# library\nimport matplotlib.pyplot as plt\nfrom matplotlib_venn import venn2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"venn2([set(train_.atom_index_0), set(test_.atom_index_0)])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"set(train_.atom_index_0).symmetric_difference(set(test_.atom_index_0))\n# set(train_.atom_index_0).intersection(set(test_.atom_index_0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"venn2([set(train_.atom_index_1), set(test_.atom_index_1)])\n# set(train_.atom_index_1).intersection(set(test_.atom_index_1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"venn2([set(train_.type), set(test_.type)])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_grp_all = pd.DataFrame(train_.groupby(['molecule_name', 'atom_index_0', 'atom_index_1', 'type'])['scalar_coupling_constant'].mean())\ntrain_grp_mn = pd.DataFrame(train_.groupby(['molecule_name'])['scalar_coupling_constant'].mean())\ntrain_grp_ai0 = pd.DataFrame(train_.groupby(['atom_index_0'])['scalar_coupling_constant'].mean())\ntrain_grp_ai1 = pd.DataFrame(train_.groupby(['atom_index_1'])['scalar_coupling_constant'].mean())\ntrain_grp_t = pd.DataFrame(train_.groupby(['type'])['scalar_coupling_constant'].mean())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_grp_ai0.reset_index(level = 0, inplace = True)\ntrain_grp_ai0.head()\ntrain_grp_t.reset_index(level = 0, inplace = True)\ntrain_grp_t.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ai0_0 = train_.loc[train_['atom_index_0'] == 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ai0_0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# sns.set_style(\"whitegrid\")\nsns.distplot(train_grp_t.scalar_coupling_constant, rug = True)\nplt.style.use(\"dark_background\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.set_palette('colorblind')\nsns.kdeplot(train_grp_t.scalar_coupling_constant, shade=True, color = \"yellow\", alpha = 0.9)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.kdeplot(train_grp_ai0.scalar_coupling_constant)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.kdeplot(train_grp_ai1.scalar_coupling_constant)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(x = train_.type, y = train_.scalar_coupling_constant)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(x = train_.atom_index_0, y = train_.scalar_coupling_constant)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(x = train_.atom_index_1, y = train_.scalar_coupling_constant)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"h = sns.jointplot(x = train_grp_ai0.scalar_coupling_constant, y = train_grp_ai1.scalar_coupling_constant, kind = \"kde\")\nh.set_axis_labels('train_grp_ai0.scalar_coupling_constant', 'train_grp_ai1.scalar_coupling_constant', fontsize=16)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_.loc[train_['molecule_name'] == 'dsgdb9nsd_042139']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structure_ = pd.read_csv('../input/structures.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structure_.loc[structure_['molecule_name'] == 'dsgdb9nsd_042139']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Refer here\nhttps://www.kaggle.com/joydeb28/ml-models"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder,OneHotEncoder\nfrom sklearn import preprocessing\n\nimport graphviz\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\ntry:\n    # To enable interactive mode you should install ipywidgets\n    # https://github.com/jupyter-widgets/ipywidgets\n    from ipywidgets import interact, SelectMultiple\n    INTERACTIVE = True\nexcept ImportError:\n    INTERACTIVE = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_train_data():\n    # load dataset\n    dataset_train = pandas.read_csv(\"../input/train.csv\")\n    dataset_test = pandas.read_csv(\"../input/test.csv\")\n    #['id' 'molecule_name' 'atom_index_0' 'atom_index_1' 'type' 'scalar_coupling_constant']\n    cat_columns =['molecule_name', 'type']\n    label_encoders = {}\n    for col in cat_columns:\n        new_le = LabelEncoder()\n        dataset_train[col] = new_le.fit_transform(dataset_train[col])\n        dataset_test[col] = new_le.fit_transform(dataset_test[col])\n    X_train = pandas.DataFrame(dataset_train, columns=['molecule_name','atom_index_0','atom_index_1','type'])\n    X_test = pandas.DataFrame(dataset_test, columns=['id','molecule_name','atom_index_0','atom_index_1','type'])\n    Y_train = dataset_train['scalar_coupling_constant']\n    return X_train,X_test,Y_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"min_max_scaler = preprocessing.MinMaxScaler()\nX_train,X_test_with_id,Y_train = get_train_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test_with_id.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Y_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = min_max_scaler.fit_transform(X_train)\n# 0\t1\t0\t0 the first row in the pandas dataframe becomes scaled numpy array \n# [0.        , 0.03571429, 0.        , 0.        ],\nX_test = pandas.DataFrame(X_test_with_id, columns=['molecule_name','atom_index_0','atom_index_1','type'])\nX_test = min_max_scaler.fit_transform(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(X_train, Y_train, test_size=0.1, random_state=42)\nprint(x_train[0:5])\nprint(x_test[0:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"evals_result = {}\n#Parameters for LightGBM Model\nparams_lgb = {'num_leaves': 5,\n          'min_child_samples': 79,\n          'objective': 'regression',\n          'max_depth': 9,\n          'learning_rate': 0.1,\n          \"boosting_type\": \"gbdt\",\n          \"subsample_freq\": 1,\n          \"subsample\": 0.9,\n          \"bagging_seed\": 47,\n          \"metric\": ['mae'],\n          \"verbosity\": -1,\n          'reg_alpha': 0.1302650970728192,\n          'reg_lambda': 0.3603427518866501,\n          'colsample_bytree': 1.0,\n          'n_estimators':1500}\n\n\nimport lightgbm as lgb\nlgtrain = lgb.Dataset(x_train, label=y_train)\nlgval = lgb.Dataset(x_test, label=y_test)\nmodel_lgb = lgb.train(params_lgb, \n                      lgtrain, 10000, \n                      valid_sets=[lgtrain, lgval], \n                      verbose_eval=500,\n                      evals_result=evals_result)\ny_out = model_lgb.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def render_metric(metric_name):\n    ax = lgb.plot_metric(evals_result, metric=metric_name, figsize=(15, 10))\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if INTERACTIVE:\n    # create widget to switch between metrics\n    interact(render_metric, metric_name=params_lgb['metric'])\nelse:\n    render_metric(params['metric'][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def render_plot_importance(importance_type, max_features=10,\n                           ignore_zero=True, precision=4):\n    ax = lgb.plot_importance(model_lgb, importance_type=importance_type,\n                             max_num_features=max_features,\n                             ignore_zero=ignore_zero, figsize=(12, 8),\n                             precision=precision)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if INTERACTIVE:\n    # create widget for interactive feature importance plot\n    interact(render_plot_importance,\n             importance_type=['split', 'gain'],\n             max_features=(1, X_train.shape[-1]),\n             precision=(0, 10))\nelse:\n    render_plot_importance(importance_type='split')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def render_tree(tree_index, show_info, precision=4):\n    show_info = None if 'None' in show_info else show_info\n    return lgb.create_tree_digraph(model_lgb, tree_index=tree_index,\n                                   show_info=show_info, precision=precision)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if INTERACTIVE:\n    # create widget to switch between trees and control info in nodes\n    interact(render_tree,\n             tree_index=(0, model_lgb.num_trees() - 1),\n             show_info=SelectMultiple(  # allow multiple values to be selected\n                 options=['None',\n                          'split_gain',\n                          'internal_value',\n                          'internal_count',\n                          'leaf_count'],\n                 value=['None']),\n             precision=(0, 10))\n    tree = None\nelse:\n    tree = render_tree(84, ['None'])\ntree","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"my_submission = pandas.DataFrame({'id': X_test_with_id.id, 'scalar_coupling_constant': y_out})\n# you could use any filename. We choose submission here\nmy_submission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}