From 9647e7fae14bae7855cde34991285afef51b4893 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Wed, 7 Feb 2024 15:24:41 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- scikit-learn/svm/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/scikit-learn/svm/README.md b/scikit-learn/svm/README.md index c9849ad..cfdc80e 100644 --- a/scikit-learn/svm/README.md +++ b/scikit-learn/svm/README.md @@ -117,4 +117,31 @@ performance = 100.0 * (prediction == label_test).sum() / prediction.shape[0] print(f"Performance correct: {performance}%") # -> Performance correct: 95.4% ``` +Sometimes it is useful to scale the value range of the individual features to the same range: +```pythonv +import numpy as np +import sklearn.svm # type:ignore + +data_train = np.load("data_train.npy") +data_test = np.load("data_test.npy") +label_train = np.load("label_train.npy") +label_test = np.load("label_test.npy") + +svm = sklearn.svm.SVC() + +min_value = data_train.min(axis=0, keepdims=True) +data_train -= min_value +data_test -= min_value + +min_value = data_train.max(axis=0, keepdims=True) +data_train /= min_value +data_test /= min_value + +svm.fit(X=data_train, y=label_train) +prediction = svm.predict(X=data_test) + +performance = 100.0 * (prediction == label_test).sum() / prediction.shape[0] + +print(f"Performance correct: {performance}%") +```