<jupyter_start><jupyter_text>Basic Modeling in scikit-learn> Before we can validate models, we need an understanding of how to create and work with them. This chapter provides an introduction to running regression and classification models in scikit-learn. We will use this model building foundation throughout the remaining chapters. This is the Summary of lecture "Model Validation in Python", via datacamp.- toc: true - badges: true- comments: true- author: <NAME>- categories: [Python, Datacamp, Machine_Learning]- image:<jupyter_code>import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>Introduction to model validation- Model validation    - Ensuring your model performs as expected on new data    - Testing model performance on holdout datasets    - Selecting the best model, parameters, and accuracy metrics    - Achieving the best accuracy for the given data      Seen vs. unseen dataModel's tend to have higher accuracy on observations they have seen before. In the candy dataset, predicting the popularity of Skittles will likely have higher accuracy than predicting the popularity of Andes Mints; Skittles is in the dataset, and Andes Mints is not.You've built a model based on 50 candies using the dataset `X_train` and need to report how accurate the model is at predicting the popularity of the 50 candies the model was built on, and the 35 candies (`X_test`) it has never seen. You will use the mean absolute error, `mae()`, as the accuracy metric.<jupyter_code>candy = pd.read_csv('./dataset/candy-data.csv')
candy.head()
X = candy.drop(['competitorname', 'winpercent'], axis=1)
y = candy['winpercent']
X
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error as mae
from sklearn.ensemble import RandomForestRegressor

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)

model = RandomForestRegressor(n_estimators=50)
# The model is fit using X_train and y_train
model.fit(X_train, y_train)

# Create vectors of predictions
train_predictions = model.predict(X_train)
test_predictions = model.predict(X_test)

# Train/Test Errors
train_error = mae(y_true=y_train, y_pred=train_predictions)
test_error = mae(y_true=y_test, y_pred=test_predictions)

# Print the accuracy for seen and unseen data
print("Model error on seen data: {0:.2f}.".format(train_error))
print("Model error on unseen data: {0:.2f}.".format(test_error))<jupyter_output>Model error on seen data: 3.71.
Model error on unseen data: 8.67.<jupyter_text>When models perform differently on training and testing data, you should look to model validation to ensure you have the best performing model.   Regression models- Random forest parameters    - `n_estimators`: the number of trees in the forest    - `max_depth`: the maximum depth of the trees    - `random_state`: random seed  Set parameters and fit a modelPredictive tasks fall into one of two categories: regression or classification. In the candy dataset, the outcome is a continuous variable describing how often the candy was chosen over another candy in a series of 1-on-1 match-ups. To predict this value (the win-percentage), you will use a **regression** model.<jupyter_code>from sklearn.ensemble import RandomForestRegressor

rfr = RandomForestRegressor()
# Set the number of trees
rfr.n_estimators = 100

# Add a maximum depth
rfr.max_depth = 6

# Set the random date
rfr.random_state = 1111

# Fit the model
rfr.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>You have updated parameters after the model was initialized. This approach is helpful when you need to update parameters. Before making predictions, let's see which candy characteristics were most important to the model.  Feature importancesAlthough some candy attributes, such as chocolate, may be extremely popular, it doesn't mean they will be important to model prediction. After a random forest model has been fit, you can review the model's attribute, `.feature_importances_`, to see which variables had the biggest impact. You can check how important each variable was in the model by looping over the feature importance array using `enumerate()`.<jupyter_code># Print how important each column is to the model
for i, item in enumerate(rfr.feature_importances_):
    # Use i and item to print out the feature importance of each column
    print("{0:s}: {1:.2f}".format(X_train.columns[i], item))<jupyter_output>chocolate: 0.23
fruity: 0.04
caramel: 0.04
peanutyalmondy: 0.06
nougat: 0.01
crispedricewafer: 0.01
hard: 0.02
bar: 0.02
pluribus: 0.03
sugarpercent: 0.24
pricepercent: 0.30<jupyter_text>No surprise here - chocolate is the most important variable. `.feature_importances_` is a great way to see which variables were important to your random forest model.  Classification models  Classification predictionsIn model validation, it is often important to know more about the predictions than just the final classification. When predicting who will win a game, most people are also interested in how likely it is a team will win.| Probability      | Prediction | Meaning || ----------- | ----------- | ---------- || 0 < .5      | 0       | Team Loses || .5 < 1   | 1        | Team Wins |In this exercise, you look at the methods, `.predict()` and `.predict_proba()` using the `tic_tac_toe` dataset. The first method will give a prediction of whether Player One will win the game, and the second method will provide the probability of Player One winning.<jupyter_code>tic_tac_toe = pd.read_csv('./dataset/tic-tac-toe.csv')
tic_tac_toe.head()
y = tic_tac_toe['Class'].apply(lambda x: 1 if x == 'positive' else 0)
X = tic_tac_toe.drop('Class', axis=1)
X = pd.get_dummies(X)
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.8)
rfc = RandomForestClassifier()
# Fit the rfc model
rfc.fit(X_train, y_train)

# Create arrays of predictions
classification_predictions = rfc.predict(X_test)
probability_predictions = rfc.predict_proba(X_test)

# Print out count of binary predictions
print(pd.Series(classification_predictions).value_counts())

# Print the first value from probability_predictions
print('The first predicted probabilities are: {}'.format(probability_predictions[0]))<jupyter_output>1    568
0    199
dtype: int64
The first predicted probabilities are: [0.76 0.24]<jupyter_text>You can see there were 563 observations where Player One was predicted to win the Tic-Tac-Toe game. Also, note that the `predicted_probabilities` array contains lists with only two values because you only have two possible responses (win or lose). Remember these two methods, as you will use them a lot throughout this course.  Reusing model parametersReplicating model performance is vital in model validation. Replication is also important when sharing models with co-workers, reusing models on new data or asking questions on a website such as [Stack Overflow](https://stackoverflow.com/). You might use such a site to ask other coders about model errors, output, or performance. The best way to do this is to replicate your work by reusing model parameters.In this exercise, you use various methods to recall which parameters were used in a model.<jupyter_code>rfc = RandomForestClassifier(n_estimators=50, max_depth=6, random_state=1111)

# Print the classification model
print(rfc)

# Print the classification model's random state parameter
print('The random state is: {}'.format(rfc.random_state))

# Print all parameters
print('Printing the parameters dictionary: {}'.format(rfc.get_params()))<jupyter_output>RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None,
                       criterion='gini', max_depth=6, max_features='auto',
                       max_leaf_nodes=None, max_samples=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, n_estimators=50,
                       n_jobs=None, oob_score=False, random_state=1111,
                       verbose=0, warm_start=False)
The random state is: 1111
Printing the parameters dictionary: {'bootstrap': True, 'ccp_alpha': 0.0, 'class_weight': None, 'criterion': 'gini', 'max_depth': 6, 'max_features': 'auto', 'max_leaf_nodes': None, 'max_samples': None, 'min_impurity_decrease': 0.0, 'min_impurity_split': None, 'min_samples_leaf': 1, 'min_samples_split': 2, 'min_weight_fraction_leaf': 0.0, 'n_estimators': 50, 'n_jobs': None, 'oob_score': False, 'random_state': 1111, 'verbose': 0,[...]<jupyter_text>Recalling which parameters were used will be helpful going forward. Model validation and performance rely heavily on which parameters were used, and there is no way to replicate a model without keeping track of the parameters used!  Random forest classifierThis exercise reviews the four modeling steps discussed throughout this chapter using a random forest classification model. You will:1. Create a random forest classification model.2. Fit the model using the tic_tac_toe dataset.3. Make predictions on whether Player One will win (1) or lose (0) the current game.4. Finally, you will evaluate the overall accuracy of the model.<jupyter_code># Create a random forest classifier
rfc = RandomForestClassifier(n_estimators=50, max_depth=6, random_state=1111)

# Fit rfc using X_train and y_train
rfc.fit(X_train, y_train)

# Create predictions on X_test
predictions = rfc.predict(X_test)
print(predictions[0:5])

# Print model accuracy using score() and the testing data
print(rfc.score(X_test, y_test))<jupyter_output>[0 1 1 0 1]
0.8213820078226858
{'fraction_non_alphanumeric': 0.07893925377736663, 'fraction_numerical': 0.018295816630691745, 'mean_word_length': 4.889830508474576, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 26, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '373349', 'license': 'Apache-2.0', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2747, 'n_words': 1135, 'path': '_notebooks/2020-07-13-01-Basic-Modeling-in-scikit-learn.ipynb', 'repo_name': 'AntonovMikhail/chans_jupyter', 'size': '9736'}