crossValidationTemplate.py
def crossValidateKNN(features, labels):
    """
    This code is provided as a template for cross-validation of KNN classification.
    Pass into the variables "features", "labels" your own data. 

    As well, you can replace the code in the "BUILD" and "EVALUATE" sections
    to be useful with other types of Classifiers.
    """
    #
    # CROSS VALIDATION 
    # The features array is arranged as rows of instances, columns of features in our training set.
    numInstances, numFeatures = features.shape
    numFolds = min(10, numInstances) # how many cross-validation folds do you want - (default=10)
    # divide test set into 10 random subsets
    indices = cross_validation.KFold(numInstances, n_folds = numFolds)

    errors = np.empty(numFolds)
    for foldIndex, (train_index, test_index) in enumerate(indices):
        # SEGMENT DATA INTO FOLDS
        print('Fold: %d' % foldIndex) 
        print("TRAIN: %s" % train_index)
        print("TEST: %s" % test_index)
    
        # SCALE
        scaler = preprocessing.MinMaxScaler(feature_range = (-1, 1))
        trainingFeatures = scaler.fit_transform(features.take(train_index, 0))
        # BUILD NEW MODEL - ADD YOUR MODEL BUILDING CODE HERE...
        model = KNeighborsClassifier(n_neighbors = 3)
        model.fit(trainingFeatures, labels.take(train_index, 0))
        # RESCALE TEST DATA TO TRAINING SCALE SPACE
        testingFeatures = scaler.transform(features.take(test_index, 0))
        # EVALUATE WITH TEST DATA - ADD YOUR MODEL EVALUATION CODE HERE
        model_output = model.predict(testingFeatures)
        print("KNN prediction %s" % model_output) # Debugging.
        # CONVERT labels(test,:) LABELS TO SAME FORMAT TO COMPUTE ERROR 
        labels_test = labels.take(test_index, 0)
        # COUNT ERRORS. matches is a boolean array, taking the mean does the right thing.
        matches = model_output != labels_test
        errors[foldIndex] = matches.mean()
    print('cross validation error: %f' % errors.mean())
    print('cross validation accuracy: %f' % (1.0 - errors.mean()))
    return errors

    This code is provided as a template for cross-validation of KNN classification.
    Pass into the variables "features", "labels" your own data. 

    As well, you can replace the code in the "BUILD" and "EVALUATE" sections
    to be useful with other types of Classifiers.
    
This code is provided as a template for cross-validation of KNN classification.
Pass into the variables "features", "labels" your own data.

As well, you can replace the code in the "BUILD" and "EVALUATE" sections
to be useful with other types of Classifiers.
This code is provided as a template for cross - validation of KNN classification . Pass into the variables " features " " labels " your own data . As well you can replace the code in the " BUILD " and " EVALUATE " sections to be useful with other types of Classifiers .
def crossValidateKNN(features, labels):
     CROSS VALIDATION 
     The features array is arranged as rows of instances, columns of features in our training set.
    numInstances, numFeatures = features.shape
    numFolds = min(10, numInstances)  how many cross-validation folds do you want - (default=10)
     divide test set into 10 random subsets
    indices = cross_validation.KFold(numInstances, n_folds = numFolds)
    errors = np.empty(numFolds)
    for foldIndex, (train_index, test_index) in enumerate(indices):
         SEGMENT DATA INTO FOLDS
        print('Fold: %d' % foldIndex) 
        print("TRAIN: %s" % train_index)
        print("TEST: %s" % test_index)
         SCALE
        scaler = preprocessing.MinMaxScaler(feature_range = (-1, 1))
        trainingFeatures = scaler.fit_transform(features.take(train_index, 0))
         BUILD NEW MODEL - ADD YOUR MODEL BUILDING CODE HERE...
        model = KNeighborsClassifier(n_neighbors = 3)
        model.fit(trainingFeatures, labels.take(train_index, 0))
         RESCALE TEST DATA TO TRAINING SCALE SPACE
        testingFeatures = scaler.transform(features.take(test_index, 0))
         EVALUATE WITH TEST DATA - ADD YOUR MODEL EVALUATION CODE HERE
        model_output = model.predict(testingFeatures)
        print("KNN prediction %s" % model_output)  Debugging.
         CONVERT labels(test,:) LABELS TO SAME FORMAT TO COMPUTE ERROR 
        labels_test = labels.take(test_index, 0)
         COUNT ERRORS. matches is a boolean array, taking the mean does the right thing.
        matches = model_output != labels_test
        errors[foldIndex] = matches.mean()
    print('cross validation error: %f' % errors.mean())
    print('cross validation accuracy: %f' % (1.0 - errors.mean()))
    return errors
def crossValidateKNN ( features , labels ) : numInstances , numFeatures = features . shape numFolds = min ( 10 , numInstances ) indices = cross_validation . KFold ( numInstances , n_folds = numFolds ) errors = np . empty ( numFolds ) for foldIndex , ( train_index , test_index ) in enumerate ( indices ) : print ( 'Fold: %d' % foldIndex ) print ( "TRAIN: %s" % train_index ) print ( "TEST: %s" % test_index ) scaler = preprocessing . MinMaxScaler ( feature_range = ( - 1 , 1 ) ) trainingFeatures = scaler . fit_transform ( features . take ( train_index , 0 ) ) model = KNeighborsClassifier ( n_neighbors = 3 ) model . fit ( trainingFeatures , labels . take ( train_index , 0 ) ) testingFeatures = scaler . transform ( features . take ( test_index , 0 ) ) model_output = model . predict ( testingFeatures ) print ( "KNN prediction %s" % model_output ) labels_test = labels . take ( test_index , 0 ) matches = model_output != labels_test errors [ foldIndex ] = matches . mean ( ) print ( 'cross validation error: %f' % errors . mean ( ) ) print ( 'cross validation accuracy: %f' % ( 1.0 - errors . mean ( ) ) ) return errors
This code is provided as a template for cross-validation of KNN classification.
This code is provided as a template for cross - validation of KNN classification .
"""
    This code is provided as a template for cross-validation of KNN classification.
    Pass into the variables "features", "labels" your own data. 

    As well, you can replace the code in the "BUILD" and "EVALUATE" sections
    to be useful with other types of Classifiers.
    """ # # CROSS VALIDATION  # The features array is arranged as rows of instances, columns of features in our training set. # how many cross-validation folds do you want - (default=10) # divide test set into 10 random subsets # SEGMENT DATA INTO FOLDS # SCALE # BUILD NEW MODEL - ADD YOUR MODEL BUILDING CODE HERE... # RESCALE TEST DATA TO TRAINING SCALE SPACE # EVALUATE WITH TEST DATA - ADD YOUR MODEL EVALUATION CODE HERE # Debugging. # CONVERT labels(test,:) LABELS TO SAME FORMAT TO COMPUTE ERROR  # COUNT ERRORS. matches is a boolean array, taking the mean does the right thing.
features
labels