
# Load libraries
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import load_digits
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import validation_curve

#load data
digits = load_digits()
#create feature matrix and target vector
features, target = digits.data, digits.target

#create range of values for parameter
param_range = np.arange(1, 250, 2)

#caculate accuracy on training and test set using range of parameter values
train_scores, test_scores = validation_curve(
    #classifier
    RandomForestClassifier(),
    #Feature matrix
    features,
    #Targer vector
    target,
    #Hyperparameter to examine
    param_name='n_estimators',
    #Range of hyperparameter's values
    param_range = param_range,
    #Number of folds
    cv=3,
    #performance metric
    scoring='accuracy',
    #use all computer cores
    n_jobs=-1)

#calculate mean and standard deviation for training set scores
train_mean = np.mean(train_scores, axis=1)
train_std = np.std(train_scores, axis=1)

#calculate mean and standard deviation for test set scores
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)

#plot mean accuracy scores for training and test sets
plt.plot(param_range, train_mean, label="Training score", color="black")
plt.plot(param_range, test_mean, label="Cross-validation score", color="dimgrey")
# Plot accurancy bands for training and test sets
plt.fill_between(param_range, train_mean - train_std,
 train_mean + train_std, color="gray")
plt.fill_between(param_range, test_mean - test_std,
 test_mean + test_std, color="gainsboro")
# Create plot
plt.title("Validation Curve With Random Forest")
plt.xlabel("Number Of Trees")
plt.ylabel("Accuracy Score")
plt.tight_layout()
plt.legend(loc="best")
plt.show()






















