<jupyter_start><jupyter_text>Train a model with linear data using XGBoost algorithm  Model is trained with XGBoost installed in notebook instance  In the later examples, we will train using SageMaker's XGBoost algorithm<jupyter_code># Install xgboost in notebook instance.
#### Command to install xgboost
!conda install -y -c conda-forge xgboost
%matplotlib inline
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import xgboost as xgb
df = pd.read_csv('linear_all.csv')
df.head()
plt.scatter(x=df.x,y=df.y,label='ideal fit')
plt.scatter(x=df.x,y=df.y_noisy, color='r',marker='+',label='Target')
plt.grid(True)
plt.xlabel('Input Feature')
plt.ylabel('Target')
plt.legend()
train_file = 'linear_train.csv'
validation_file = 'linear_validation.csv'
# Specify the column names as the file does not have column header
df_train = pd.read_csv(train_file,names=['y_noisy','x'])
df_validation = pd.read_csv(validation_file,names=['y_noisy','x'])
df_train.head()
df_validation.head()
plt.scatter(x=df_train.x,y=df_train.y_noisy,label='Training')
plt.scatter(x=df_validation.x,y=df_validation.y_noisy, color='r',marker='+',label='Validation')
plt.grid(True)
plt.xlabel('Input Feature')
plt.ylabel('Target')
plt.title('Training and Validation Data')
plt.legend()
X_train = df_train.iloc[:,1:] # Features: 1st column onwards 
y_train = df_train.iloc[:,0].ravel() # Target: 0th column

X_validation = df_validation.iloc[:,1:]
y_validation = df_validation.iloc[:,0].ravel()
# max_depth = 5,objective="reg:linear",num_round = 50
regressor = xgb.XGBRegressor()
regressor
regressor.fit(X_train,y_train, eval_set = [(X_train, y_train), (X_validation, y_validation)])
eval_result = regressor.evals_result()
eval_result
training_rounds = range(len(eval_result['validation_0']['rmse']))
print(training_rounds)
plt.scatter(x=training_rounds,y=eval_result['validation_0']['rmse'],label='Training Error')
plt.scatter(x=training_rounds,y=eval_result['validation_1']['rmse'],label='Validation Error')
plt.grid(True)
plt.xlabel('Iterations')
plt.ylabel('RMSE')
plt.title('Training Vs Validation Error')
plt.legend()
xgb.plot_importance(regressor)
df.head()
X_test = df.iloc[:,:1]
print(X_test[:5])
# This works
type(X_test)
# This does not work
type(df.iloc[:5,0])
result = regressor.predict(X_test)
result[:5]
df['y_predicted'] = result
df.head()
plt.scatter(x=df.x,y=df.y_noisy,color='r',label='actual')
plt.scatter(x=df.x,y=df.y_predicted,color='b',label='predicted')
plt.grid(True)
plt.legend()
plt.boxplot([df.y_noisy,df.y_predicted], labels=['actual','predicted'])
plt.title('Box Plot - Actual, Predicted')
plt.ylabel('Target')
plt.grid(True)
# Over prediction and Under Prediction needs to be balanced
# Training Data Residuals
residuals = (df.y_predicted - df.y_noisy)

plt.hist(residuals)
plt.grid(True)
plt.xlabel('(Predicted - Actual)')
plt.ylabel('Count')
plt.title('Residuals Distribution')
plt.axvline(color='g')<jupyter_output><empty_output><jupyter_text>Tree Based Algorithms cap the upper and lower values that are predicted.<jupyter_code># True Function
def straight_line(x):
    return 5*x + 8
# X is outside range of training samples
X = np.array([1000,25,-100,5000])
tmp_df = pd.DataFrame({'x':X})
print("True Function:\n{0}".format(straight_line(X)))
print("Predicted:\n{0}".format(regressor.predict(tmp_df)))
# X is inside range of training samples
X = np.array([0,1,3,5,7,9,11,15,18])
tmp_df = pd.DataFrame({'x':X})
print("True Function:\n{0}".format(straight_line(X)))
print("Predicted:\n{0}".format(regressor.predict(tmp_df)))
df.describe()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16268739589117157, 'fraction_numerical': 0.013325930038867296, 'mean_word_length': 7.962686567164179, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '71687', 'license': 'Apache-2.0', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1326, 'n_words': 256, 'path': 'xgboost/LinearAndQuadraticFunctionRegression/linear_xgboost_localmode.ipynb', 'repo_name': 'srinivas02/AmazonSageMakerCourse', 'size': '3602'}