[Python] SVM-Training and Test

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    To apply my test set to SVM I don't want to use train_test_Split function. I want to apply my test set to svm without training to machine. I wrote a simple code like following one. when I apply my sets to svm the accuracy gets worth but when I use train_test_Split function the accuracy is fine. what's wrong with my code? `

    #Training dataset
    F_HED = np.array(np.abs(ZZ_1))
    F_NonHED = np.array(np.abs(ZZ_N1))
    Y1 = np.ones(F_HED.shape[0]) #label 1 for HED
    Y2 = np.zeros(F_NonHED.shape[0]) #label 0 for Non-HED


    #Combining Train sets
    X = np.vstack((F_HED, F_NonHED))
    Y = np.hstack((Y1, Y2))


    #Testing dataset
    X_Test1 = np.array(np.abs(ZZ_X))
    X_Test2 = np.array(np.abs(ZZ_NX))
    Y_Test1 = np.ones(X_Test1.shape[0]) #label 1 Due to being HED
    Y_Test2 = np.zeros(X_Test2.shape[0]) #label 0 Due to being HED


    #Combining Test Sets
    X_Test = np.vstack((X_Test2, X_Test2))
    Y_Test = np.hstack((Y_Test2, Y_Test2))

    #Normalizing
    Scaler = StandardScaler()
    X_Train_S = Scaler.fit_transform(X)
    X_Test_S = Scaler.transform(X_Test)

    #Classifier
    clf = SVC(kernel='rbf', C=10)
    clf.fit(X_Train_S, Y)

    #Predict part
    Y_pred = clf.predict(X_Test_S)


    TN, FP, FN, TP = confusion_matrix(Y_Test, Y_pred, labels=[0, 1]).ravel()
    print("Confusion Matrix:")
    print(f"TN: {TN}, FP: {FP}, FN: {FN}, TP: {TP}")`
    Confusion Matrix:
    TN: 24, FP: 12, FN: 0, TP: 0


    This is the output of my code which I expected that at least FP and FN would be less than 5 but, in that output, the worth thing is with TP which is Zero. In fact, it should be near to 22.

    Continue reading...

Compartilhe esta Página