<jupyter_start><jupyter_text>About This notebook will cover a basic model with a basic EDA about the competition.  Imports<jupyter_code># Imports
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import tree
from sklearn import metrics

# Visualization
# Ploty Imports
from itertools import combinations
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot

# Figures inline and set visualization style
%matplotlib inline
sns.set() #Different type of visualization

# import the necessary modelling algorithms

# Regression
from sklearn.linear_model import LinearRegression,Ridge,Lasso,RidgeCV
from sklearn.ensemble import RandomForestRegressor,BaggingRegressor,GradientBoostingRegressor,AdaBoostRegressor
from sklearn.svm import SVR
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import ExtraTreesRegressor
import xgboost as xgb
from xgboost.sklearn import XGBRegressor

# Model selection
from sklearn.model_selection import train_test_split,cross_validate
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV

# Preprocessing
from sklearn.preprocessing import MinMaxScaler,StandardScaler,Imputer,LabelEncoder,PolynomialFeatures

# Evaluation metrics
from sklearn.metrics import mean_squared_log_error,mean_squared_error, r2_score,mean_absolute_error # for regression
from sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score  # for classification

# Show multiple statements at once
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code># Train Data
train = pd.read_csv('input/train.csv')

# Test Data
test = pd.read_csv('input/test.csv')

sub = pd.read_csv('input/sample_submission.csv')
structures = pd.read_csv('input/structures.csv')<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code># Lets take a look at the main csv files
train.head()
sub.head()
structures.head()
print(f'{train.shape[0]} rows in the train data.')
print(f'{test.shape[0]} rows in the test data.')
print(f"{train['molecule_name'].nunique()} different molecules in the train data.")
print(f"There are {test['molecule_name'].nunique()} different molecules in the test data.")
print(f"There are {structures['atom'].nunique()} unique atoms.")
print(f"There are {train['type'].nunique()} unique types.")
# We take a first look at the dataset
train.info()
print ('#################################################')
print ('#################################################')
test.info()
sub.info()
sub['scalar_coupling_constant'].unique()<jupyter_output><empty_output><jupyter_text>Lets take a look at a nicer visualization.<jupyter_code># Plotly notebook mode
init_notebook_mode(connected=True)
'''
This function will plot a mollecule
'''
def plot_molecule(molecule_name, structures_df):
    """Creates a 3D plot of the molecule"""
    
    atomic_radii = dict(C=0.68, F=0.64, H=0.23, N=0.68, O=0.68)  
    cpk_colors = dict(C='black', F='green', H='white', N='blue', O='red')

    molecule = structures[structures.molecule_name == molecule_name]
    coordinates = molecule[['x', 'y', 'z']].values
    x_coordinates = coordinates[:, 0]
    y_coordinates = coordinates[:, 1]
    z_coordinates = coordinates[:, 2]
    elements = molecule.atom.tolist()
    radii = [atomic_radii[element] for element in elements]
    
    def get_bonds():
        """Generates a set of bonds from atomic cartesian coordinates"""
        ids = np.arange(coordinates.shape[0])
        bonds = set()
        coordinates_compare, radii_compare, ids_compare = coordinates, radii, ids
        
        for i in range(len(ids)):
            coordinates_compare = np.roll(coordinates_compare, -1, axis=0)
            radii_compare = np.roll(radii_compare, -1, axis=0)
            ids_compare = np.roll(ids_compare, -1, axis=0)
            distances = np.linalg.norm(coordinates - coordinates_compare, axis=1)
            bond_distances = (radii + radii_compare) * 1.3
            mask = np.logical_and(distances > 0.1, distances <  bond_distances)
            bonds.update(map(frozenset, zip(ids[mask], ids_compare[mask])))
        
        return bonds            
            
    def atom_trace():
        """Creates an atom trace for the plot"""
        colors = [cpk_colors[element] for element in elements]
        markers = dict(color=colors, line=dict(color='lightgray', width=2), size=7, symbol='circle', opacity=0.8)
        trace = go.Scatter3d(x=x_coordinates, y=y_coordinates, z=z_coordinates, mode='markers', marker=markers,
                             text=elements)
        return trace

    def bond_trace():
        """"Creates a bond trace for the plot"""
        trace = go.Scatter3d(x=[], y=[], z=[], hoverinfo='none', mode='lines',
                             marker=dict(color='grey', size=7, opacity=1))
        for i, j in bonds:
            trace['x'] += (x_coordinates[i], x_coordinates[j], None)
            trace['y'] += (y_coordinates[i], y_coordinates[j], None)
            trace['z'] += (z_coordinates[i], z_coordinates[j], None)
        return trace
    
    bonds = get_bonds()
    atoms = zip(range(len(elements)), x_coordinates, y_coordinates, z_coordinates)
    annotations = [dict(text=num, x=x, y=y, z=z, showarrow=False, yshift=15)
                   for num, x, y, z in atoms]
    data = [atom_trace(), bond_trace()]
    axis_params = dict(showgrid=False, showticklabels=False, zeroline=False, titlefont=dict(color='white'))
    layout = go.Layout(scene=dict(xaxis=axis_params, yaxis=axis_params, zaxis=axis_params, annotations=annotations),
                       margin=dict(r=0, l=0, b=0, t=0), showlegend=False)
    fig = go.Figure(data=data, layout=layout)
    iplot(fig)
# Plots

plot_molecule('dsgdb9nsd_133885', structures)

plot_molecule('dsgdb9nsd_105227', structures)

plot_molecule('dsgdb9nsd_099964', structures)<jupyter_output><empty_output><jupyter_text>Duplicate Check<jupyter_code># We check if we have duplicates
train.duplicated().any()
test.duplicated().any()<jupyter_output><empty_output><jupyter_text>Label Encoding Before modeling it would be a good idea to do a Label Encoding to variables which are still as objects in order to improve the model's performance.<jupyter_code>categoricals = train.select_dtypes(include='object').columns
categoricals = test.select_dtypes(include='object').columns
for c in categoricals:
    lbl = LabelEncoder()
    lbl.fit(list(train[c].values))
    train[c] = lbl.transform(list(train[c].values))
for c in categoricals:
    lbl = LabelEncoder()
    lbl.fit(list(test[c].values))
    test[c] = lbl.transform(list(test[c].values))
# Check it has been done properly
train.dtypes
test.dtypes<jupyter_output><empty_output><jupyter_text>Metric<jupyter_code>def metric(df, preds):
    
    df["prediction"] = preds
    maes = []
    
    for t in df.type.unique():
        
        y_true = df[df.type==t].scalar_coupling_constant.values
        y_pred = df[df.type==t].prediction.values
        mae = np.log(metrics.mean_absolute_error(y_true, y_pred))
        maes.append(mae)
    
    return np.mean(maes)<jupyter_output><empty_output><jupyter_text>Modeling  Setting K-Folds<jupyter_code># Setting a 5-fold stratified cross-validation (note: shuffle=True)
skf = KFold(n_splits=5, shuffle=True, random_state=8)
params = {'booster' : 'gbtree',
          #'nthread' : 5,
          'objective' : 'reg:linear',
          'eval_metric' : 'mae', 
          'max_depth' : 8,
          'eta' : 0.3,
          'subsample' : 0.7, 
          'colsample_bytree' : 0.7 
        }
# We define de label
y = train['scalar_coupling_constant']
dtrain = xgb.DMatrix(train, label = y)
res = xgb.cv(params,
             dtrain,
             num_boost_round = 4000,
             folds=skf,
             seed=2019,
             early_stopping_rounds = 10,
             verbose_eval=True)
best_round=[i for i, e in enumerate(res['test-rmse-mean']) if e == min(res['test-rmse-mean'])][0]
best_round
res.iloc[best_round]<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup