How to save prediction result from a ML model (SVM, kNN) using sklearn

Hello Good People,

I have some label data and I am using the classification ML model (SVM, kNN) to train and test the dataset.

My input features are look likes:
(442, 443, 0.608923884514436), (444, 443, 0.6418604651162789)

The label is look likes:
0, 1

Then I used sklearn to train and test (after splitting the dataset 80% for train and 20% for the test). Code sample is given below:

classifiers = [
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
trainingData    = X_train
trainingScores  = y_train
for item in classifiers:
    clf = item, trainingScores)
    y_pred = clf.predict(X_test)

    print("Accuracy Scor:")
    print(accuracy_score(y_pred, y_test))
    print("Confusion Matrix:")
    print(confusion_matrix(y_pred, y_test))
    print("Classification Report:")
    print(classification_report(y_pred, y_test))

The SVC Accuracy Score: 0.6639580602883355
The kNN Accuracy Score: 0.7171690694626475

I can guess, that the model is predicting some data correctly. Now, my question is,

  1. How can I save the prediction data including the label given by the model in a CSV file.
  2. Is it possible to use the cross-validation concept here? For example, if I want to apply 5 cross-validations. Then, how can I do that?

Though I am not sure, I am asking a freaking question or not (I am new in the ML fields).

Any kind of answer is appreciable.

Hello Akib, and welcome.

Start with the documentation:

If that’s not enough, there are literally dozens of tutorials and blog
posts explaining how to read and write CSV files:

Many people consider this a good one:

By the way, you have misspelled “Score” as “Scor” in your code:

The SVC Accuracy Scor: 0.6639580602883355
The kNN Accuracy Scor: 0.7171690694626475

Hi @steven.daprano thanks for your suggestion.

Thanks for catching the mistake and the correction is done.

However, I know how to read and write a CSV file in python (basic). But, my question was, how can I save the predicted data from the model (after prediction). Though, I am not sure about my question (freaking or not).

Akib asked:

“But, my question was, how can I save the predicted data from the model
(after prediction).”

The same as you would save any other data.

I don’t understand your question. Do you have the predicted data? Can
you put it into a table format with at least one row and column? Then
you can write it out to a CSV file.

Maybe you will get a better class of responses if you show some sample
predicted data (simplified). There’s no need to show it all, just a
small sample.