<filename>25_Supervised Learning with scikit-learn/02_Regression/06_5-fold-cross-validation.py<gh_stars>1-10
'''
06 - 5-fold cross-validation

Cross-validation is a vital step in evaluating a model. It maximizes the amount 
of data that is used to train the model, as during the course of training, the 
model is not only trained, but also tested on all of the available data.

In this exercise, you will practice 5-fold cross validation  on  the  Gapminder  data. 
By default, scikit-learn's cross_val_score() function  uses  R^2  as  the metric  of 
choice for regression. Since you are performing 5-fold cross-validation, the  function
 will return 5 scores. Your job is to compute these 5 scores and then take their average.

The DataFrame has been loaded as df and split into the feature/target variable arrays X and y.
 The modules pandas and numpy have been imported as pd and np, respectively.

INSTRUCTIONS:

- Import LinearRegression from sklearn.linear_model and cross_val_score from sklearn.model_selection.
- Create a linear regression regressor called reg.
- Use the cross_val_score() function to perform 5-fold cross-validation on X and y.
- Compute and print the average cross-validation score. You can use NumPy's mean() function to compute 
  the average.
'''
# Import the necessary modules
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score


# Create a linear regression object: reg
reg = LinearRegression()

# Compute 5-fold cross-validation scores: cv_scores
cv_scores = cross_val_score(reg, X, y, cv=5)

# Print the 5-fold cross-validation scores
print(cv_scores)

print("Average 5-Fold CV Score: {}".format(np.mean(cv_scores)))

'''
<script.py> output:
    [0.81720569 0.82917058 0.90214134 0.80633989 0.94495637]
    Average 5-Fold CV Score: 0.8599627722793232
'''
