jmquintana79
3/30/2018 - 7:45 AM

Validation curve of any algorithm

#1- Validation curve of an algorithm vs any of his hyperparameters. Use a sklearn tool: http://scikit-learn.org/stable/modules/learning_curve.html#learning-curve #2- Validation curve of an algorithm vs the degree of features (or another parameter).

## degree of features validation
lerror_train = list(); lerror_cv = list(); lerror_test = list(); ldegree = list()
for vdegree in [0,1,2,3,4,5]:
    print('--> degree',vdegree)
    ## split data in training / test sets
    from sklearn.model_selection import train_test_split
    # prepare data
    X = data[lcol_features].as_matrix()
    y = data[starget].values
    # polynomaial features generation
    if vdegree>0:
        from sklearn.preprocessing import PolynomialFeatures
        poly = PolynomialFeatures(degree=vdegree, interaction_only=False, include_bias=True)
        X = poly.fit_transform(X)
    # split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,shuffle=False)
    ldt = data.index.tolist(); ldt_train = ldt[0:len(y_train)]; ldt_test = ldt[len(y_train):]
    ## estimator
    from sklearn.linear_model import Lasso
    clf = Lasso(alpha=1.0)
    # validation
    dresult = model_validation(clf,X_train,y_train,X_test,y_test,False)
    # store results
    ldegree.append(vdegree)
    lerror_train.append(dresult['train'])
    lerror_cv.append(dresult['cv'])
    lerror_test.append(dresult['test'])
    
# plot the validation curve
VALIDA = pd.DataFrame({'degree':ldegree,'error_train':lerror_train,'error_cv':lerror_cv,'error_test':lerror_test}).set_index('degree')
VALIDA.plot(title='ERROR vs degree of features')
import numpy as np
from sklearn.model_selection import validation_curve
from sklearn.datasets import load_iris
from sklearn.linear_model import Ridge

# prepare data
np.random.seed(0)
iris = load_iris()
X, y = iris.data, iris.target
indices = np.arange(y.shape[0])
np.random.shuffle(indices)
X, y = X[indices], y[indices]

# calculate validation curve for a Ridge estimator vs the regularization parameter
train_scores, cv_scores = validation_curve(Ridge(), X, y, "alpha",np.logspace(-7, 3, 3))