<jupyter_start><jupyter_text>Split F028<jupyter_code>df_F028 = df[df['USGS_ID']=='F042']
df = df[df['USGS_ID']!='F042']
df
dataset = df[[
    'CAL', 'GR', 'DT', 'SP', 'DENS', 'PE',
    'RESD', 'PHIN', 'PHID', 'GR_smooth', 
    'PE_smooth',
    'gz_pchip_interp' # Trying to predict gz_pchip_interp
]]

dataset.replace('NaN',np.nan, regex=True, inplace=True)# Should be good already
# dataset = dataset.dropna() # not needed

dataset.head(3)
# Features we will use for prediction
X = dataset[['CAL', 'GR', 'DT', 'SP', 'DENS', 'PE',
        'RESD', 'PHIN', 'PHID', 
        'GR_smooth', 
        'PE_smooth']]

# What we are trying to predict
Y = dataset[['gz_pchip_interp']]

Y_array = np.array(Y.values)
seed = 42 # random seed is only used if you want to compare exact answers with friends 
test_size = 0.25 # how much data you want to withold, .15 - 0.3 is a good starting point

X_train, X_test, y_train, y_test = train_test_split(X.values, Y_array, test_size=test_size)
preds = 0
preds2 = 0

export_name = 'test_test_test.csv'
max_iter=500

# def catboost_gz(X_train, X_test, y_train, y_test, export_name, max_iter=500):

model = CatBoostRegressor(objective='RMSE',
                            task_type='GPU',
                            iterations=max_iter)
  
model.fit(X_train, y_train, verbose=1000 )
preds = model.predict(X_test)

  # Stats
rmse = mean_squared_error(y_test, preds, squared=False)
print("Root Mean Squared Error: %f" % (rmse))
max = max_error(y_test, preds)
print("Max Error: %f" % (max))
MAE= median_absolute_error(y_test, preds)
print("Median Abs Error: %f" % (max))

  # Let's Tune
grid = {'learning_rate': [0.1, 0.2, 0.4],
        'depth': [6, 8, 10,  12, 14],
        'l2_leaf_reg': [2, 3, 4 , 5, 6 ]}

model_grid = CatBoostRegressor(objective='RMSE',
                                 task_type='GPU',
                                 iterations=max_iter)

  # Grid Search
grid_search_result = model_grid.grid_search(grid, 
                                            X=X_train, 
                                            y=y_train, 
                                            cv=5,
                                            verbose=False)
model2 = CatBoostRegressor(objective='RMSE',
                           depth=grid_search_result['params']['depth'],
                           l2_leaf_reg=grid_search_result['params']['l2_leaf_reg'],
                           learning_rate=grid_search_result['params']['learning_rate'],
                           iterations=max_iter)

model2.fit(X_train, y_train, verbose=500)
preds2 = model2.predict(X_test)

# Stats for the second round
rmse2 = mean_squared_error(y_test, preds2, squared=False)
print("Root Mean Squared Error: %f" % (rmse2))
max2 = max_error(y_test, preds2)
print("Max Error: %f" % (max2))
MAE2= median_absolute_error(y_test, preds2)
print("Median Abs Error: %f" % (MAE2))

x = datetime.datetime.now()

d = {'target': [Y.columns.values, Y.columns.values],
     'MSE': [rmse, rmse2],
     'MAE': [MAE, MAE2],
     'MaxError': [max, max2], 
     'iter': [max_iter, max_iter],
     'day': [x.day, x.day], 
     'month':[x.month, x.month], 
     'year':[x.year, x.year],
     'model':['catboost', 'catboost'],
     'version':[cats.__version__, cats.__version__ ]}

results = pd.DataFrame(data=d)

filepath = 'drive/My Drive/1_lewis_research/analysis/experiments/catboost/catboost_results/'
#results.to_csv(filepath+export_name)

 # return results
grid_search_result['params']['depth']
results<jupyter_output><empty_output><jupyter_text>Now Run it on F028<jupyter_code>dataset2 = df_F028[[
    'CAL', 'GR', 'DT', 'SP', 'DENS', 'PE',
    'RESD', 'PHIN', 'PHID', 'GR_smooth', 
    'PE_smooth',
    'gz_pchip_interp' # Trying to predict gz_pchip_interp
]]

dataset2.replace('NaN',np.nan, regex=True, inplace=True)# Should be good already
# dataset = dataset.dropna() # not needed

dataset2.head(3)
# Features we will use for prediction
X2 = dataset2[['CAL', 'GR', 'DT', 'SP', 'DENS', 'PE',
        'RESD', 'PHIN', 'PHID', 
        'GR_smooth', 
        'PE_smooth']]

# What we are trying to predict
Y2 = dataset2[['gz_pchip_interp']]

Y2_array = np.array(Y2.values)
preds3 = model2.predict(X2)
preds3
# Stats for the second round
rmse2 = mean_squared_error(Y2_array, preds3, squared=False)
print("Root Mean Squared Error: %f" % (rmse2))
max2 = max_error(Y2_array, preds3)
print("Max Error: %f" % (max2))
MAE2= median_absolute_error(Y2_array, preds3)
print("Median Abs Error: %f" % (MAE2))
Y2.gz_pchip_interp
np.shape(Y2.gz_pchip_interp.values)
np.shape(preds3[:])
smooth_window = np.ones(7)/7
np.convolve(smooth_window, preds3, mode='same')
window = 3
smooth_window = np.ones(window)/window
plt.figure(figsize=(10,6))
plt.plot(Y2.gz_pchip_interp.values)
#plt.plot(preds3)
plt.plot(np.convolve(smooth_window, preds3, mode='same'))
#plt.plot(np.convolve(smooth_window, Y2.gz_pchip_interp.values, mode='same'))
plt.grid(True)

yo = (preds3 - preds.min())/preds3.max()
mo = ((Y2 - Y2.min())/Y2.max())
plt.plot(1-yo, color='black')
plt.plot(mo, color='g')
yo-mo.T
ted = (preds3 - Y2_array.T)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup