<jupyter_start><jupyter_text>Import Data<jupyter_code>!chmod 777 ../Data/train.csv
!chmod 777 ../Data/test.csv

# get train and test sets
trainval = pd.read_csv('../Data/train.csv')
test = pd.read_csv('../Data/test.csv')
trainval_molecules = trainval['molecule_name'].unique()
test_molecules = test['molecule_name'].unique()
train_molecules, val_molecules = train_test_split(
    trainval_molecules, random_state=32)
train = trainval[trainval['molecule_name'].isin(train_molecules)]
val = trainval[trainval['molecule_name'].isin(val_molecules)]
train.shape, val.shape, test.shape<jupyter_output><empty_output><jupyter_text>Model<jupyter_code># features & target
features = ['type']
target = 'scalar_coupling_constant'
# Train
X_train = train[features]
y_train = train[target]

# Val
X_val = val[features]
y_val = val[target]<jupyter_output><empty_output><jupyter_text>Encode<jupyter_code>encoder = ce.OrdinalEncoder()
X_train = pd.DataFrame(encoder.fit_transform(X_train.values))
X_val = pd.DataFrame(encoder.transform(X_val.values))
# Instantiate rf
rf = RandomForestRegressor(n_estimators=100, random_state=32)

# fit
rf.fit(X_train, y_train)
# make predictions
y_pred = rf.predict(X_val)
# metric
groups = X_train[0]

def group_mean_log_mae(y_true, y_pred, floor=1e-9):
    maes = (y_true-y_pred).abs().groupby(groups).mean()
    return np.log(maes.map(lambda x: max(x, floor))).mean()
group_mean_log_mae(y_val, y_pred)<jupyter_output><empty_output><jupyter_text>Model with XGBoost<jupyter_code>from xgboost import XGBRegressor

# instantiate model
model = XGBRegressor(n_estimators=100, max_depth=10, random_state=32)

# fit model
model.fit(X_train, y_train)
# make predictions
y_pred = model.predict(X_val)
group_mean_log_mae(y_val, y_pred)<jupyter_output><empty_output><jupyter_text>XGBoost Early Stopping<jupyter_code>eval_set = [(X_train, y_train), (X_val, y_val)]
model = XGBRegressor(n_estimators=1000, n_jobs=-1)

model.fit(X_train, y_train, early_stopping_rounds=10,
          eval_metric='rmse', eval_set=eval_set)
results = model.evals_result()
val_rmse = results['validation_1']['rmse']
epoch = range(len(train_rmse))
plt.plot(epoch, val_rmse, label='Validation')
plt.legend();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13771377137713772, 'fraction_numerical': 0.01395139513951395, 'mean_word_length': 8.036585365853659, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '629495', 'license': 'MIT', 'n_tokens_mistral': 904, 'n_tokens_neox': 849, 'n_words': 126, 'path': 'EricWuerfel_XGBoostAssignment.ipynb', 'repo_name': 'ewuerfel66/DS-Unit-2-Applied-Modeling', 'size': '2222'}