<jupyter_start><jupyter_text>XGBoost Getting Started Example on Linear Regression Importing and Organizing DataIn this example we will be predicting prices of houses in California based on the features of each house using Intel optimizations for XGBoost shipped as a part of the oneAPI AI Analytics Toolkit.Let's start by **importing** all necessary data and packages.<jupyter_code>import xgboost as xgb
from sklearn.metrics import mean_squared_error
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np<jupyter_output>Intel(R) Extension for Scikit-learn* enabled (https://github.com/intel/scikit-learn-intelex)<jupyter_text>Now let's **load** in the dataset and **organize** it as necessary to work with our model.<jupyter_code>#loading the data
california = fetch_california_housing()

#converting data into a pandas dataframe
data = pd.DataFrame(california.data)
data.columns = california.feature_names

#setting price as value to be predicted
data['PRICE'] = california.target

#extracting rows
X, y = data.iloc[:,:-1],data.iloc[:,-1]

#using dmatrix values for xgboost
data_dmatrix = xgb.DMatrix(data=X,label=y)

#splitting data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1693)<jupyter_output><empty_output><jupyter_text>**Instantiate and define XGBoost regresion object** by calling the XGBRegressor() class from the library. Use hyperparameters to define the object. Intel optimizations for XGBoost trainingcan be used by calling the `hist` tree method in the parameters, as shown below.<jupyter_code>xg_reg = xgb.XGBRegressor(objective ='reg:squarederror', colsample_bytree = 0.3, learning_rate = 0.1,max_depth = 5, alpha = 10, n_estimators = 10, tree_method='hist')<jupyter_output><empty_output><jupyter_text>Training and Saving the model **Fitting and training model** using training datasets and predicting values. Note that Intel optimizations for XGBoost inference are enabled by default.<jupyter_code>xg_reg.fit(X_train,y_train)
preds = xg_reg.predict(X_test)<jupyter_output><empty_output><jupyter_text>**Finding root mean squared error** of predicted values.<jupyter_code>rmse = np.sqrt(mean_squared_error(y_test, preds))
print("RMSE:",rmse)<jupyter_output>RMSE: 1.0823382872176526<jupyter_text>Saving the Results Now let's **export the predicted values to a CSV file**.<jupyter_code>pd.DataFrame(preds).to_csv('foo.csv',index=False)
print("[CODE_SAMPLE_COMPLETED_SUCCESFULLY]")<jupyter_output>[CODE_SAMPLE_COMPLETED_SUCCESFULLY]
{'fraction_non_alphanumeric': 0.08950019372336304, 'fraction_numerical': 0.013560635412630763, 'mean_word_length': 7.521452145214521, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 1, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '118825', 'license': 'MIT', 'n_tokens_mistral': 881, 'n_tokens_neox': 837, 'n_words': 241, 'path': 'AI-and-Analytics/Getting-Started-Samples/IntelPython_XGBoost_GettingStarted/IntelPython_XGBoost_GettingStarted.ipynb', 'repo_name': 'krzeszew/oneAPI-samples', 'size': '2581'}