gpt4 book ai didi

python - 如何将从缩放数据中学到的决策边界转移到原始数据(缩放后的数据)?

转载 作者:行者123 更新时间:2023-12-04 08:41:45 25 4
gpt4 key购买 nike

我在缩放的输入数据上安装了 SVM,现在我正试图找到一种方法,将从缩放的数据中学到的决策边界转移到原始数据(非缩放的数据)。我该怎么做?
我使用以下内容来绘制决策边界:

svc0.fit(Xs, y)
plot_decision_regions(X=Xs, y=y ,clf=svc0,legend=2)
enter image description here
然后我只是缩减了数据(svc0 仍然适合缩放数据),但决策边界看起来很奇怪:
Xs_scaledback=scaler.inverse_transform(Xs) 

plot_decision_regions(X=Xs_scaledback,y=y,clf=svc0,legend=2)
enter image description here
我需要显示原始(缩减)数据的决策边界。我怎样才能解决这个问题?!

最佳答案

您是否尝试将数据缩放合并到估算器中?
像这样的东西

import numpy as np
import matplotlib.pyplot as plt

from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

from sklearn.datasets import load_iris

data_dict = load_iris()

X, y = data_dict['data'][:, :2], data_dict['target']

model = make_pipeline(StandardScaler(), SVC())
model.fit(X,y) # You should do a train test split

def plot_decision_boundary(pred_func, X, y):
# Set min and max values and give it some padding
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
h = 0.01
# Generate a grid of points with distance h between them
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# Predict the function value for the whole gid
Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# Plot the contour and training examples
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)


plot_decision_boundary(model.predict, X, y)

Partly adapted from https://scikit-learn.org/stable/auto_examples/ensemble/plot_voting_decision_regions.html

关于python - 如何将从缩放数据中学到的决策边界转移到原始数据(缩放后的数据)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64530179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com