<jupyter_start><jupyter_text>Intro to  Linear Regression with cuMLCorresponding notebook to [*Beginner’s Guide to Linear Regression in Python with cuML*](http://bit.ly/cuml_lin_reg_friend) story on MediumLinear Regression is a simple machine learning model where the response `y` is modelled by a linear combination of the predictors in `X`. The `LinearRegression` function implemented in the `cuML` library allows users to change the `fit_intercept`, `normalize`, and `algorithm` parameters. Here is a brief on RAPIDS' Linear Regression parameters:- `algorithm`: 'eig' or 'svd' (default = 'eig')    - `Eig` uses a eigen decomposition of the covariance matrix, and is much faster    - `SVD` is slower, but guaranteed to be stable- `fit_intercept`: boolean (default = True)  - If `True`, `LinearRegresssion` tries to correct for the global mean of `y`  - If `False`, the model expects that you have centered the data.- `normalize`: boolean (default = False)  - If True, the predictors in X will be normalized by dividing by it’s L2 norm  - If False, no scaling will be doneMethods that can be used with `LinearRegression` are:- `fit`: Fit the model with `X` and `y`- `get_params`: Sklearn style return parameter state- `predict`: Predicts the `y` for `X`- `set_params`: Sklearn style set parameter state to dictionary of params`cuML`'s `LinearRegression` expects expects either `cuDF` DataFrame or `cuPy`/`NumPy` matrix inputs  Load data- for this demo, we will be utilizing the Boston housing dataset from `sklearn`  - start by loading in the set and printing a map of the contents<jupyter_code>from sklearn.datasets import load_boston

# load Boston dataset
boston = load_boston()

# let's see what's inside
print(boston.keys())<jupyter_output>dict_keys(['data', 'target', 'feature_names', 'DESCR', 'filename'])<jupyter_text>Boston house prices dataset- a description of the dataset is provided in `DESCR`  - let's explore<jupyter_code># what do we know about this dataset?
print(boston.DESCR)<jupyter_output>.. _boston_dataset:

Boston house prices dataset
---------------------------

**Data Set Characteristics:**  

    :Number of Instances: 506 

    :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.

    :Attribute Information (in order):
        - CRIM     per capita crime rate by town
        - ZN       proportion of residential land zoned for lots over 25,000 sq.ft.
        - INDUS    proportion of non-retail business acres per town
        - CHAS     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
        - NOX      nitric oxides concentration (parts per 10 million)
        - RM       average number of rooms per dwelling
        - AGE      proportion of owner-occupied units built prior to 1940
        - DIS      weighted distances to five Boston employment centres
        - RAD      index of accessibility to radial highways
        - TAX      full-value property-tax rate per $10,000
        - PTRATIO  pu[...]<jupyter_text>Build Dataframe- Import `cuDF` and input the data into a DataFrame   - Then add a `PRICE` column equal to the `target` key<jupyter_code>import cudf

# build dataframe from data key
bos = cudf.DataFrame(list(boston.data))
# set column names to feature_names
bos.columns = boston.feature_names

# add PRICE column from target
bos['PRICE'] = boston.target

# let's see what we're working with
bos.head()<jupyter_output><empty_output><jupyter_text>Split Train from Test- For basic Linear Regression, we will predict `PRICE` (Median value of owner-occupied homes) based on `TAX` (full-value property-tax rate per $10,000)  - Go ahead and trim data to just these columns<jupyter_code># simple linear regression X and Y
X = bos['TAX']
Y = bos['PRICE']<jupyter_output><empty_output><jupyter_text>We can now set training and testing sets for our model- Use `cuML`'s `train_test_split` to do this  - Train on 70% of data  - Test on 30% of data<jupyter_code>from cuml.preprocessing.model_selection import train_test_split

# train/test split (70:30)
sX_train, sX_test, sY_train, sY_test = train_test_split(X, Y, train_size = 0.7)

# see what it looks like
print(sX_train.shape)
print(sX_test.shape)
print(sY_train.shape)
print(sY_test.shape)<jupyter_output><empty_output><jupyter_text>Predict Values1. fit the model with `TAX` (*X_train*) and corresponding `PRICE` (*y_train*) values   - so it can build an understanding of their relationship 2. predict `PRICE` (*y_test*) for a test set of `TAX` (*X_test*) values  - and compare `PRICE` predictions to actual median house (*y_test*) values    - use `sklearn`'s `mean_squared_error` to do this<jupyter_code>from cuml import LinearRegression
from sklearn.metrics import mean_squared_error
import cupy

# call Linear Regression model
slr = LinearRegression()

# train the model
slr.fit(sX_train, sY_train)

# make predictions for test X values
sY_pred = slr.predict(sX_test)

# calculate error
mse = mean_squared_error(cupy.asnumpy(sY_test), 
                         cupy.asnumpy(sY_pred))
print(mse)<jupyter_output><empty_output><jupyter_text>3. visualize prediction accuracy with `matplotlib`<jupyter_code>import matplotlib.pyplot as plt

# scatter actual and predicted results
plt.scatter(cupy.asnumpy(sY_test), cupy.asnumpy(sY_pred))

# label graph
plt.xlabel("Actual Prices: $Y_i$")
plt.ylabel("Predicted prices: $\hat{Y}_i$")
plt.title("Prices vs Predicted prices: $Y_i$ vs $\hat{Y}_i$")

plt.show()<jupyter_output><empty_output><jupyter_text>Multiple Linear Regression - Our mean squared error for Simple Linear Regression looks kinda high.  - Let's try Multiple Linear Regression (predicting based on multiple variables rather than just `TAX`) and see if that produces more accurate predictions1. Set X to contain all values that are not `PRICE` from the unsplit data  - i.e. `CRIM`, `ZN`, `INDUS`, `CHAS`, `NOX`, `RM`, `AGE`, `DIS`, `RAD`, `TAX`, `PTRATIO`, `B`, `LSTAT`  - Y to still represent just 1 target value (`PRICE`)    - also from the unsplit data<jupyter_code># set X to all variables except price
mX = bos.drop('PRICE', axis=1)
# and, like in the simple Linear Regression, set Y to price
mY = bos['PRICE']<jupyter_output><empty_output><jupyter_text>2. Split the data into `multi_X_train`, `multi_X_test`, `Y_train`, and `Y_test`  - Use `cuML`'s `train_test_split`    - And the same 70:30 train:test ratio<jupyter_code># train/test split (70:30)
mX_train, mX_test, mY_train, mY_test = train_test_split(mX, mY, train_size = 0.7)

# see what it looks like
print(mX_train.shape)
print(mX_test.shape)
print(mY_train.shape)
print(mY_test.shape)<jupyter_output><empty_output><jupyter_text>3. fit the model with `multi_X_train` and corresponding `PRICE` (*y_train*) values   - so it can build an understanding of their relationships 4. predict `PRICE` (*y_test*) for the test set of independent (*multi_X_test*) values  - and compare `PRICE` predictions to actual median house (*y_test*) values    - use `sklearn`'s `mean_squared_error` to do this<jupyter_code># call Linear Regression model
mlr = LinearRegression()

# train the model for multiple regression
mlr.fit(mX_train, mY_train)

# make predictions for test X values
mY_pred = mlr.predict(mX_test)

# calculate error
mmse = mean_squared_error(cupy.asnumpy(mY_test), 
                         cupy.asnumpy(mY_pred))
print(mmse)<jupyter_output><empty_output><jupyter_text>5. visualize with `matplotlib`<jupyter_code># scatter actual and predicted results
plt.scatter(cupy.asnumpy(mY_test), cupy.asnumpy(mY_pred))

# label graph
plt.xlabel("Actual Prices: $Y_i$")
plt.ylabel("Predicted prices: $\hat{Y}_i$")
plt.title("Prices vs Predicted prices: $Y_i$ vs $\hat{Y}_i$")

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1018590240123935, 'fraction_numerical': 0.007875032274722438, 'mean_word_length': 4.597543352601156, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '19256', 'license': 'Apache-2.0', 'n_tokens_mistral': 2596, 'n_tokens_neox': 2452, 'n_words': 872, 'path': 'community_tutorials_and_guides/linear_regression_demo.ipynb', 'repo_name': 'DaceT/notebooks-contrib', 'size': '7746'}