{"cells":[{"metadata":{},"cell_type":"markdown","source":"I am using only 5% train data set. I have not find out any missing values and duplicate rows (All Columns). There are duplicate values in single column. \n\n\nI am just start yet kaggle compitition so i don't wish to win this compition but if you like this karnel please up vote me or any suggestion please comment me also notifiy my misstake.\n\n### This kernel is not completed !\n\n## Workflow stages\n<ol>\n    <li>Visualize, Explore the data </li>\n    <li> Model, predict </li>\n    <li> Supply or submit the results.</li>\n<ol>\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\nimport os\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split, cross_val_score\nfrom sklearn.preprocessing import StandardScaler\nlbl = LabelEncoder()\ncolor = sns.color_palette()\nsns.set_style('darkgrid')\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = '../input/champs-scalar-coupling/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(path+'/train.csv')\ntest = pd.read_csv(path+'test.csv')","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.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(8,6))\nsns.countplot(train['type'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(8,6))\nsns.countplot(train['atom_index_0'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(8,6))\nsns.countplot(train['atom_index_1'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = train.sample(frac=0.09, random_state=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv(path+'/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"potential_energy = pd.read_csv(path+'/potential_energy.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pd.isnull(train).sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['type'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[['type','scalar_coupling_constant']].groupby(['type'], as_index=False ).mean().sort_values(by='scalar_coupling_constant', ascending=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[['atom_index_0','scalar_coupling_constant']].groupby(['atom_index_0'], as_index=False ).mean().sort_values(by='scalar_coupling_constant', ascending=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[['atom_index_1','scalar_coupling_constant']].groupby(['atom_index_1'], as_index=False ).mean().sort_values(by='scalar_coupling_constant', ascending=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for col in train['type'].unique():\n    sns.distplot(train[train['type'] == col]['scalar_coupling_constant'])\n    plt.show()\n    #print(col)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"potential_energy.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"potential_energy.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train  = pd.merge(train, potential_energy, how='left', on='molecule_name', right_index=False)\ntest  = pd.merge(test, potential_energy, how='left', on='molecule_name', right_index=False )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#test  = pd.merge(test, potential_energy, how='left', on='molecule_name')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = train[['id','molecule_name','atom_index_0','atom_index_1','type','potential_energy','scalar_coupling_constant']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures = pd.read_csv(path+'/structures.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['atom1'] = train['type'].str[2]\ntest['atom1'] = test['type'].str[2]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['atom2'] = train['type'].str[3]\ntest['atom2'] = test['type'].str[3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['coupling_type'] = train['type'].str[0:2]\ntest['coupling_type'] = test['type'].str[0:2]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures.rename(columns={'x':'x1','y':'y1','z':'z1'}, inplace=True)\nstructures.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train, structures, how ='left', left_on=['molecule_name', 'atom_index_0','atom1'], right_on=['molecule_name', 'atom_index','atom'], right_index=False)\ntest = pd.merge(test, structures, how ='left', left_on=['molecule_name', 'atom_index_0','atom1'], right_on=['molecule_name', 'atom_index','atom'], right_index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"structures.rename(columns={'x1':'x2','y1':'y2','z1':'z2'}, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train, structures, how ='left', left_on=['molecule_name', 'atom_index_1','atom2'], right_on=['molecule_name', 'atom_index','atom'], right_index=False)\ntest = pd.merge(test, structures, how ='left', left_on=['molecule_name', 'atom_index_1','atom2'], right_on=['molecule_name', 'atom_index','atom'], right_index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['x2-x1'] = train['x2']-train['x1']\ntrain['y2-y1'] = train['y2']-train['y1']\ntrain['z2-z1'] = train['z2']-train['z1']\n\ntest['x2-x1'] = test['x2']-test['x1']\ntest['y2-y1'] = test['y2']-test['y1']\ntest['z2-z1'] = test['z2']-test['z1']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['pow(x2-x1)'] = train['x2-x1']**2\ntrain['pow(y2-y1)'] = train['y2-y1']**2\ntrain['pow(z2-z1)'] = train['z2-z1']**2\n\ntest['pow(x2-x1)'] = test['x2-x1']**2\ntest['pow(y2-y1)'] = test['y2-y1']**2\ntest['pow(z2-z1)'] = test['z2-z1']**2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['pow(x2-x1)+pow(y2-y1)+pow(z2-z1)'] = train['pow(x2-x1)']+train['pow(y2-y1)']+train['pow(z2-z1)']\n\ntest['pow(x2-x1)+pow(y2-y1)+pow(z2-z1)'] = test['pow(x2-x1)']+test['pow(y2-y1)']+test['pow(z2-z1)']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['distance'] = np.sqrt(train['pow(x2-x1)+pow(y2-y1)+pow(z2-z1)'])\n\ntest['distance'] = np.sqrt(test['pow(x2-x1)+pow(y2-y1)+pow(z2-z1)'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir(path))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mulliken_charges = pd.read_csv(path+'/mulliken_charges.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mulliken_charges.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train, mulliken_charges, how='left', left_on=['molecule_name', 'atom_index_0'], right_on=['molecule_name','atom_index'], right_index=False)\n\ntest = pd.merge(test, mulliken_charges, how='left', left_on=['molecule_name', 'atom_index_0'], right_on=['molecule_name','atom_index'], right_index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.rename(columns ={'mulliken_charge':'mulliken_charge_0'}, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train, mulliken_charges, how='left', left_on=['molecule_name', 'atom_index_1'], right_on=['molecule_name','atom_index'], right_index=False)\n\ntest = pd.merge(test, mulliken_charges, how='left', left_on=['molecule_name', 'atom_index_1'], right_on=['molecule_name','atom_index'], right_index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.rename(columns ={'mulliken_charge':'mulliken_charge_1'}, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sc_contributions = pd.read_csv(path+'/scalar_coupling_contributions.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train, sc_contributions, how='left', left_on=['molecule_name', 'atom_index_0', 'atom_index_1', 'type'], right_on=['molecule_name', 'atom_index_0', 'atom_index_1', 'type'], right_index=False)\n\ntest = pd.merge(test, sc_contributions, how='left', left_on=['molecule_name', 'atom_index_0', 'atom_index_1', 'type'], right_on=['molecule_name', 'atom_index_0', 'atom_index_1', 'type'], right_index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dipole_moments = pd.read_csv(path+'/dipole_moments.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dipole_moments.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train, dipole_moments, how='left', left_on=['molecule_name'], right_on=['molecule_name'], right_index=False)\n\ntest = pd.merge(test, dipole_moments, how='left', left_on=['molecule_name'], right_on=['molecule_name'], right_index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ms_tensors = pd.read_csv(path+'/magnetic_shielding_tensors.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ms_tensors.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[['atom1','scalar_coupling_constant']].groupby(['atom1'], as_index=False ).mean().sort_values(by='scalar_coupling_constant', ascending=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[['atom2','scalar_coupling_constant']].groupby(['atom2'], as_index=False ).mean().sort_values(by='scalar_coupling_constant', ascending=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[['coupling_type','scalar_coupling_constant']].groupby(['coupling_type'], as_index=False ).mean().sort_values(by='scalar_coupling_constant', ascending=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train1 = train.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train1['atom1'] = lbl.fit_transform(train1['atom1'])\n\ntest['atom1'] = lbl.fit_transform(test['atom1'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train1['atom2'] = lbl.fit_transform(train1['atom2'])\n\ntest['atom2'] = lbl.fit_transform(test['atom2'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train1['coupling_type'] = lbl.fit_transform(train1['coupling_type'])\n\ntest['coupling_type'] = lbl.fit_transform(test['coupling_type'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train1['potential_energy'] = lbl.fit_transform(train1['potential_energy'])\n\ntest['potential_energy'] = lbl.fit_transform(test['potential_energy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#train1['scalar_coupling_constant'] = lbl.fit_transform(train1['scalar_coupling_constant'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = np.array(train1[['atom_index_0', 'atom_index_1', 'atom1','atom2', 'coupling_type', 'potential_energy']])\nY = np.array(train1['scalar_coupling_constant'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size=0.999900, random_state=52)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.kernel_ridge import KernelRidge\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import RobustScaler\nfrom sklearn.ensemble import RandomForestRegressor,  GradientBoostingRegressor\nfrom sklearn.svm import SVC, LinearSVC\nfrom sklearn.metrics import mean_squared_error\nimport xgboost as xgb\nimport lightgbm as lgb","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_name = []\nmodel_score =[]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''linsvc = LinearSVC()\nlinsvc.fit(X_train,Y_train)\nlinsvc_score = round(linsvc.score(X_train,Y_train)*100, 2)\nmodel_name.append('LinearSVC')\nmodel_score.append(linsvc_score)\nlinsvc_score'''\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''svc = SVC()\nsvc.fit(X_train,Y_train)\nsvc_score = round(svc.score(X_train,Y_train)*100, 2)\nmodel_name.append('SVC')\nmodel_score.append(svc_score)\nsvc_score'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''kneighbors = KNeighborsClassifier()\nkneighbors.fit(X_train,Y_train)\nkneighbors_score = round(kneighbors.score(X_train,Y_train)*100, 2)\nmodel_name.append('KNeighborsClassifier')\nmodel_score.append(kneighbors_score)\nkneighbors_score'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"randomforest = RandomForestRegressor()\nrandomforest.fit(X_train,Y_train)\nrandomforest_score = round(randomforest.score(X_train,Y_train)*100, 2)\nmodel_name.append('RandomForestRegressor')\nmodel_score.append(randomforest_score)\nrandomforest_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gradient = GradientBoostingRegressor()\ngradient.fit(X_train,Y_train)\ngradient_score = round(gradient.score(X_train,Y_train)*100, 2)\nmodel_name.append('GradientBoostingRegressor')\nmodel_score.append(gradient_score)\ngradient_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"all_score = pd.DataFrame({'model_name':model_name, 'model_score':model_score})\nall_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"selected_col = ['atom_index_0', 'atom_index_1', 'atom1','atom2', 'coupling_type', 'potential_energy']\n\npredict_result = randomforest.predict(test[selected_col])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({'id':test['id'], 'scalar_coupling_constant':predict_result})\nsubmission.to_csv('my_submission.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.1"}},"nbformat":4,"nbformat_minor":1}