p300_detector.py
def svm(X, y):
    '''
    This function trains a SVM classifer on given data and evaluates its 
    performance by plotting the confusion matrix.
    
    Parameters
    ----------
    X : 2d array: sample x feature
        DESCRIPTION. Training data
    y : 1d array
        DESCRIPTION. Labels

    Returns
    -------
    clf_svm : SVM object
        DESCRIPTION. The trained SVM classifer

    '''
    # split the data into training and testing datasets
    X_train, X_test, y_train, y_test = train_test_split(X, y)
    # scale datasets
    X_train_scaled = scale(X_train)
    X_test_scaled = scale(X_test)
    
    # optimize parameters with cross validation
    # set up potential parameters
    print('Optimize parameters with cross validation...')
    param_grid = [
        {'C': [0.001, 0.01, 0.05, 0.1, 0.5, 1, 10, 100],
         'kernel': ['rbf']}
        ]
    # set up cross validation
    optimal_params = GridSearchCV(SVC(), param_grid, cv=5, scoring='accuracy')
    # compute the optimal parameters
    optimal_params.fit(X_train_scaled, y_train)
    # print the optimal parameters
    print('The optimal parameters are:', optimal_params.best_params_)
    # lock the values for C and gamma
    C = optimal_params.best_params_['C']
    
    # build the final svm
    clf_svm = SVC(C=C, kernel='linear')
    # train the model
    print('Train SVM model...')
    clf_svm.fit(X_train_scaled, y_train)
    print('Now the classifier has been trained.')
    
    # print report and plot confusion matrix
    label = 'svm_confusion_matrix'
    plot_matrix(X_test_scaled, y_test, clf_svm, label)
    # return trained classifier
    return clf_svm

    This function trains a SVM classifer on given data and evaluates its 
    performance by plotting the confusion matrix.
    
    Parameters
    ----------
    X : 2d array: sample x feature
        DESCRIPTION. Training data
    y : 1d array
        DESCRIPTION. Labels

    Returns
    -------
    clf_svm : SVM object
        DESCRIPTION. The trained SVM classifer

    
This function trains a SVM classifer on given data and evaluates its
performance by plotting the confusion matrix.

Parameters

X : 2d array: sample x feature
DESCRIPTION. Training data
y : 1d array
DESCRIPTION. Labels

Returns

clf_svm : SVM object
DESCRIPTION. The trained SVM classifer
This function trains a SVM classifer on given data and evaluates its performance by plotting the confusion matrix . Parameters X : 2d array : sample x feature DESCRIPTION . Training data y : 1d array DESCRIPTION . Labels Returns clf_svm : SVM object DESCRIPTION . The trained SVM classifer
def svm(X, y):
    X_train, X_test, y_train, y_test = train_test_split(X, y)
    X_train_scaled = scale(X_train)
    X_test_scaled = scale(X_test)
    print('Optimize parameters with cross validation...')
    param_grid = [
        {'C': [0.001, 0.01, 0.05, 0.1, 0.5, 1, 10, 100],
         'kernel': ['rbf']}
        ]
    optimal_params = GridSearchCV(SVC(), param_grid, cv=5, scoring='accuracy')
    optimal_params.fit(X_train_scaled, y_train)
    print('The optimal parameters are:', optimal_params.best_params_)
    C = optimal_params.best_params_['C']
    clf_svm = SVC(C=C, kernel='linear')
    print('Train SVM model...')
    clf_svm.fit(X_train_scaled, y_train)
    print('Now the classifier has been trained.')
    label = 'svm_confusion_matrix'
    plot_matrix(X_test_scaled, y_test, clf_svm, label)
    return clf_svm
def svm ( X , y ) : X_train , X_test , y_train , y_test = train_test_split ( X , y ) X_train_scaled = scale ( X_train ) X_test_scaled = scale ( X_test ) print ( 'Optimize parameters with cross validation...' ) param_grid = [ { 'C' : [ 0.001 , 0.01 , 0.05 , 0.1 , 0.5 , 1 , 10 , 100 ] , 'kernel' : [ 'rbf' ] } ] optimal_params = GridSearchCV ( SVC ( ) , param_grid , cv = 5 , scoring = 'accuracy' ) optimal_params . fit ( X_train_scaled , y_train ) print ( 'The optimal parameters are:' , optimal_params . best_params_ ) C = optimal_params . best_params_ [ 'C' ] clf_svm = SVC ( C = C , kernel = 'linear' ) print ( 'Train SVM model...' ) clf_svm . fit ( X_train_scaled , y_train ) print ( 'Now the classifier has been trained.' ) label = 'svm_confusion_matrix' plot_matrix ( X_test_scaled , y_test , clf_svm , label ) return clf_svm
This function trains a SVM classifer on given data and evaluates its
performance by plotting the confusion matrix.
This function trains a SVM classifer on given data and evaluates its performance by plotting the confusion matrix .
'''
    This function trains a SVM classifer on given data and evaluates its 
    performance by plotting the confusion matrix.
    
    Parameters
    ----------
    X : 2d array: sample x feature
        DESCRIPTION. Training data
    y : 1d array
        DESCRIPTION. Labels

    Returns
    -------
    clf_svm : SVM object
        DESCRIPTION. The trained SVM classifer

    ''' # split the data into training and testing datasets # scale datasets # optimize parameters with cross validation # set up potential parameters # set up cross validation # compute the optimal parameters # print the optimal parameters # lock the values for C and gamma # build the final svm # train the model # print report and plot confusion matrix # return trained classifier
X
y