{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn import *\n\ntrain = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')\nsub = pd.read_csv('../input/sample_submission.csv')\nprint(train.shape, test.shape, sub.shape)\n\ntrain['atom1'] = train['type'].map(lambda x: str(x)[2])\ntrain['atom2'] = train['type'].map(lambda x: str(x)[3])\ntest['atom1'] = test['type'].map(lambda x: str(x)[2])\ntest['atom2'] = test['type'].map(lambda x: str(x)[3])\n\nlbl = preprocessing.LabelEncoder()\nfor i in range(4):\n    train['type'+str(i)] = lbl.fit_transform(train['type'].map(lambda x: str(x)[i]))\n    test['type'+str(i)] = lbl.transform(test['type'].map(lambda x: str(x)[i]))\n\nstructures = pd.read_csv('../input/structures.csv').rename(columns={'atom_index':'atom_index_0', 'x':'x0', 'y':'y0', 'z':'z0', 'atom':'atom1'})\ntrain = pd.merge(train, structures, how='left', on=['molecule_name', 'atom_index_0', 'atom1'])\ntest = pd.merge(test, structures, how='left', on=['molecule_name', 'atom_index_0', 'atom1'])\ndel structures\n\nstructures = pd.read_csv('../input/structures.csv').rename(columns={'atom_index':'atom_index_1', 'x':'x1', 'y':'y1', 'z':'z1', 'atom':'atom2'})\ntrain = pd.merge(train, structures, how='left', on=['molecule_name', 'atom_index_1', 'atom2'])\ntest = pd.merge(test, structures, how='left', on=['molecule_name', 'atom_index_1', 'atom2'])\ndel structures\n\nmc = pd.read_csv('../input/mulliken_charges.csv').rename(columns={'atom_index':'atom_index_0'})\ntrain = pd.merge(train, mc, how='left', on=['molecule_name', 'atom_index_0'])\ntest = pd.merge(test, mc, how='left', on=['molecule_name', 'atom_index_0'])\ndel mc\n\nscc = pd.read_csv('../input/scalar_coupling_contributions.csv')\ntrain = pd.merge(train, scc, how='left', on=['molecule_name', 'atom_index_0', 'atom_index_1', 'type'])\ntest = pd.merge(test, scc, how='left', on=['molecule_name', 'atom_index_0', 'atom_index_1', 'type'])\ndel scc\nprint(train.shape, test.shape, sub.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.drop(columns=['id', 'molecule_name', 'atom1', 'atom2', 'atom_index_0', 'atom_index_1'], inplace=True)\ntest.drop(columns=['molecule_name', 'atom1', 'atom2', 'atom_index_0', 'atom_index_1'], inplace=True)\nprint(train.shape, test.shape, sub.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#https://www.kaggle.com/artgor/molecular-properties-eda-and-models\ntrain_p0 = train[['x0', 'y0', 'z0']].values\ntrain_p1 = train[['x1', 'y1', 'z1']].values\ntest_p0 = test[['x0', 'y0', 'z0']].values\ntest_p1 = test[['x1', 'y1', 'z1']].values\n\ntrain['dist'] = np.linalg.norm(train_p0 - train_p1, axis=1)\ntest['dist'] = np.linalg.norm(test_p0 - test_p1, axis=1)\n\ntrain['dist_to_type_mean'] = train['dist'] / train.groupby('type')['dist'].transform('mean')\ntest['dist_to_type_mean'] = test['dist'] / test.groupby('type')['dist'].transform('mean')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def features(df, df2):\n    for c in ['mulliken_charge', 'fc', 'sd', 'pso', 'dso', 'dist', 'dist_to_type_mean']:\n        for agg in ['min', 'max', 'sum', 'mean']:\n            tmp = eval('df.groupby([\"type\"], as_index=False)[c].' + agg + '().rename(columns={\"' + c + '\":\"' + agg + '\"+ \"_\" +\"' + c + '\"})')\n            df = pd.merge(df, tmp, how='left', on=['type'])\n            df2 = pd.merge(df2, tmp, how='left', on=['type'])\n    return df, df2\n\ntrain, test = features(train, test)\nprint(train.shape, test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def features(df):\n    for c in ['0', '1']:\n        col = [c1 + c  for c1 in ['x','y','z']]\n        for agg in ['min', 'max', 'sum', 'mean', 'std', 'skew', 'kurtosis']:\n            df[c+agg] = eval('df[col].' + agg + '(axis=1)')\n            df[c+'a'+agg] = eval('df[col].abs().' + agg + '(axis=1)')\n    return df\n\ntrain = features(train).fillna(0)\ntest = features(test).fillna(0)\nprint(train.shape, test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.drop(columns=['type', 'mulliken_charge', 'fc', 'sd', 'pso', 'dso'], inplace=True)\ntest.drop(columns=['type', 'mulliken_charge', 'fc', 'sd', 'pso', 'dso'], inplace=True)\nprint(train.shape, test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"col = [c for c in train.columns if c not in ['scalar_coupling_constant']]\nreg = ensemble.ExtraTreesRegressor(n_jobs=-1, n_estimators=10, random_state=4)\n\n#x1, x2, y1, y2 = model_selection.train_test_split(train[col], train['scalar_coupling_constant'], test_size=0.2, random_state=99)\n#reg.fit(x1, y1)\n#print(np.log(metrics.mean_absolute_error(y2, reg.predict(x2))))\nreg.fit(train[col], train['scalar_coupling_constant'])\ntest['scalar_coupling_constant']  = reg.predict(test[col])\ntest[['id', 'scalar_coupling_constant']].to_csv('submission.csv', index=False) #float_format='%.9f'","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}