php asp网站开发教程,常州网站建设优质商家,视频软件观看免费高清下载,化妆顺序步骤照片由 Unsplash上的 vackground.com提供 一、说明 SVM#xff08;支持向量机#xff09;简单而优雅用于分类和回归的监督机器学习方法。该算法试图找到一个超平面#xff0c;将数据分为不同的类#xff0c;并具有尽可能最大的边距。本篇我们将介绍如果最大边距不存在的时候… 照片由 Unsplash上的 vackground.com提供 一、说明 SVM支持向量机简单而优雅用于分类和回归的监督机器学习方法。该算法试图找到一个超平面将数据分为不同的类并具有尽可能最大的边距。本篇我们将介绍如果最大边距不存在的时候如何创造最大边距。 二、让我们逐步了解 SVM 假设我们有一维湿度数据红点代表不下雨的日子蓝点代表下雨的日子。 虚拟一维分类数据。图片由作者提供。 根据我们拥有的一维观测数据我们可以确定阈值。该阈值将充当分类器。由于我们的数据是一维的分类器将有一个阈值。如果我们的数据是二维的我们会使用一条线。 观察到的数据最近的数据点与分类器阈值之间的最短距离称为边距。能够提供最大margin的阈值称为Maximal Margin Classifier (Hyperplane)。在我们的例子中它将位于双方最接近数据的中点。 最大边际分类器。图片由作者提供。 最大保证金在实践中不太适用。因为它对异常值没有抵抗力。想象一下我们有一个具有蓝色值的离群红点。在这种情况下分类器将非常接近蓝点远离红点。 对异常值敏感。图片由作者提供。 为了改善这一点我们应该允许异常值和错误分类。我们在系统中引入偏差并减少方差。现在边距称为软边距。使用软间隔的分类器称为支持向量分类器或软间隔分类器。边缘上和软边缘内的数据点称为支持向量。 支持向量。图片由作者提供。 我们使用交叉验证来确定软边距应该在哪里。 在 2D 数据中支持向量分类器是一条线。在 3D 中它是一个平面。在 4 个或更多维度中支持向量分类器是一个超平面。从技术上讲所有 SVC 都是超平面但在 2D 情况下更容易将它们称为平面。 2D 和 3D。资料来源 https: //www.analyticsvidhya.com/blog/2021/05/support-vector-machines/和https://www.sciencedirect.com/topics/computer-science/support-vector-machine 正如我们在上面看到的支持向量分类器可以处理异常值并允许错误分类。但是我们如何处理如下所示的重叠数据呢 数据重叠。图片由作者提供。 这就是支持向量机发挥作用的地方。让我们为问题添加另一个维度。我们有特征 X作为新的维度我们取 X 的平方并将其绘制在 y 轴上。 由于现在的数据是二维的我们可以画一条支持向量分类器线。 将问题二维化。图片由作者提供。 支持向量机获取低维数据将其移至更高维度并找到支持向量分类器。 与我们上面所做的类似支持向量机使用核函数来查找更高维度的支持向量分类器。核函数是一种函数它采用原始输入空间中的两个输入数据点并计算变换后高维特征空间中它们对应的特征向量的内积。 内部产品。图片由作者提供。 核函数允许 SVM 在变换后的特征空间中运行而无需显式计算变换后的特征向量这对于大型数据集或复杂的变换来说计算成本可能很高。相反核函数直接在原始输入空间中计算特征向量之间的内积。这称为内核技巧。 三、多项式核 多项式核用于将输入数据从低维空间变换到高维空间在高维空间中使用线性决策边界更容易分离类。 多项式核。 a和b是两个不同的观测值r是多项式系数d是多项式的次数。假设d为 2r为 1/2。 数学。 我们最终得到一个点积。第一项a和b是 x 轴第二项a²和b²是 y 轴。因此我们需要做的就是计算每对点之间的点积。例如更高维度中两点之间的关系a 9b 14 (9 x 114 1/2)² 16000,25。 四、径向内核 (RBF) 径向核在无限维度中查找支持向量分类器。 它为距离测试点较近的点分配较高的权重为较远的点如最近的邻居分配较低的权重。较远的观察对数据点的分类影响相对较小。 内核函数。 它计算两个数据之间的平方距离。Gamma 由交叉验证确定它会缩放平方距离这意味着它会缩放两个点彼此之间的影响。在此公式中随着两点之间的距离增加该值将接近于零。 当类之间的决策边界是非线性且复杂的时径向核特别有用因为它可以捕获输入特征之间的复杂关系。 五、Python实现 我们可以使用支持向量机sklearn. from sklearn.svm import SVC 具有不同内核的 SVC。来源 SVC接受一些参数 C是正则化参数。较大的值会使模型在训练数据上犯更多错误错误分类。因此它的目的是有一个更好的概括。默认值为 1。kernel设置核函数。默认为rbf。其他选择是Linear、poly、sigmoid和precompulated。此外您还可以传递自己的内核函数。degree指定多项式核的次数。仅当内核是多项式时它才可用。默认值为 3。gamma控制核函数的形状。它可用于rbf、poly和sigmoid内核较小的 gamma 值使决策边界更平滑较大的值使决策边界更复杂。默认值是比例等于 1 / (n_features x X.var())。auto是 1 / n_features。或者您可以传递一个浮点值。coef0仅用于 poly 和 sigmoid 内核。它控制多项式核函数中高阶项的影响。默认值为 0。shrinking控制是否使用收缩启发式。这是一个加速启发式过程。tol是停止标准的容差。当目标函数的变化小于tol时优化过程将停止。class_weight平衡分类问题中类别的权重。可以将其设置为平衡以根据课程频率自动调整权重。默认值为“无”。max_iter是迭代极限。-1 表示无限制默认。probability指定是否启用概率估计。当它设置为 True 时估计器将估计类概率而不仅仅是返回预测的类标签。当probability设置为 True 时可以使用predict_proba该类的方法来获取新数据点的类标签的估计概率。SVCcache_size用于设置SVM算法使用的内核缓存的大小。当训练样本数量非常大或者内核计算成本很高时内核缓存会很有用。通过将核评估存储在缓存中SVM 算法可以在计算正则化参数 C 的不同值的决策函数时重用结果。 SVC 使用具有不同参数的 RBF 内核。来源 一个简单的实现 from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC# cancer data
cancer load_breast_cancer()
X_train, X_test, y_train, y_test train_test_split(cancer.data, cancer.target, random_state42)# parameters
params {C: 1.0, kernel: rbf, gamma: scale,
probability: False, cache_size: 200}# training
svc SVC(**params)
svc.fit(X_train, y_train)# we can use svcs own score function
score svc.score(X_test, y_test)
print(Accuracy on test set: {:.2f}.format(score))
#Accuracy on test set: 0.95 六、回归 我们也可以在回归问题中使用支持向量机。 from sklearn.svm import SVR epsilon是指定回归线周围容差大小的参数。回归线由 SVR 算法确定使其在一定的误差范围内拟合训练数据该误差范围由参数定义epsilon。 from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error# the California Housing dataset
california fetch_california_housing()
X_train, X_test, y_train, y_test train_test_split(california.data, california.target, random_state42)# training
svr SVR(kernelrbf, C1.0, epsilon0.1)
svr.fit(X_train, y_train)# Evaluate the model on the testing data
y_pred svr.predict(X_test)
mse mean_squared_error(y_test, y_pred)print(MSE on test set: {:.2f}.format(mse))
#MSE on test set: 1.35 我们还可以使用 来绘制边界matplotlib。 import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.svm import SVC# Load the Iris dataset
iris load_iris()# Extract the first two features (sepal length and sepal width)
X iris.data[:, :2]
y iris.target# Create an SVM classifier
svm SVC(kernellinear, C1.0)
svm.fit(X, y)# Create a mesh of points to plot in
x_min, x_max X[:, 0].min() - 1, X[:, 0].max() 1
y_min, y_max X[:, 1].min() - 1, X[:, 1].max() 1
xx, yy np.meshgrid(np.arange(x_min, x_max, 0.02),np.arange(y_min, y_max, 0.02))# Plot the decision boundary
Z svm.predict(np.c_[xx.ravel(), yy.ravel()])
Z Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmapplt.cm.Paired, alpha0.8)# Plot the training points
plt.scatter(X[:, 0], X[:, 1], cy, cmapplt.cm.Paired)
plt.xlabel(Sepal length)
plt.ylabel(Sepal width)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.title(SVM decision boundary for Iris dataset)plt.show() 边界。图片由作者提供。 SVM 是一种相对较慢的方法。 import time
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split# Load the breast cancer dataset
data load_breast_cancer()
X, y data.data, data.target# Split the data into training and testing sets
X_train, X_test, y_train, y_test train_test_split(X, y, random_state42)# Fit a logistic regression model and time it
start_time time.time()
lr LogisticRegression(max_iter1000)
lr.fit(X_train, y_train)
end_time time.time()
lr_runtime end_time - start_time# Fit an SVM model and time it
start_time time.time()
svm SVC(kernellinear, C1.0)
svm.fit(X_train, y_train)
end_time time.time()
svm_runtime end_time - start_time# Print the runtimes
print(Logistic regression runtime: {:.3f} seconds.format(lr_runtime))
print(SVM runtime: {:.3f} seconds.format(svm_runtime))
Logistic regression runtime: 0.112 seconds
SVM runtime: 0.547 seconds支持向量机 (SVM) 可能会很慢原因如下 SVM 是计算密集型的SVM 涉及解决凸优化问题对于具有许多特征的大型数据集来说计算成本可能很高。SVM 的时间复杂度通常至少为 O(n²)其中 n 是数据点的数量对于非线性内核来说时间复杂度可能要高得多。用于调整超参数的交叉验证SVM需要调整超参数例如正则化参数C和核超参数这涉及使用交叉验证来评估不同的超参数设置。这可能非常耗时尤其是对于大型数据集或复杂模型。大量支持向量对于非线性SVM支持向量的数量会随着数据集的大小或模型的复杂性而快速增加。这可能会减慢预测时间尤其是在模型需要频繁重新训练的情况下。 我们可以通过尝试以下一些方法来加速 SVM 使用线性核线性 SVM 的训练速度比非线性 SVM 更快因为优化问题更简单。如果您的数据是线性可分的或者不需要高度复杂的模型请考虑使用线性核。 from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC, LinearSVC
import time# Load MNIST digits dataset
mnist fetch_openml(mnist_784, version1)
data, target mnist[data], mnist[target]
X_train, X_test, y_train, y_test train_test_split(data, target, test_size0.2, random_state42)# Train linear SVM
start_time time.time()
linear_svc LinearSVC()
linear_svc.fit(X_train, y_train)
linear_train_time time.time() - start_time# Train non-linear SVM with RBF kernel
start_time time.time()
rbf_svc SVC(kernelrbf)
rbf_svc.fit(X_train, y_train)
rbf_train_time time.time() - start_timeprint(Linear SVM training time:, linear_train_time)
print(Non-linear SVM training time:, rbf_train_time)
Linear SVM training time: 109.03955698013306
Non-linear SVM training time: 165.98812198638916使用较小的数据集如果您的数据集非常大请考虑使用较小的数据子集进行训练。您可以使用随机抽样或分层抽样等技术来确保子集代表完整数据集。使用特征选择如果您的数据集具有许多特征请考虑使用特征选择技术来减少特征数量。这可以降低问题的维度并加快训练速度。使用较小的值C正则化参数C控制最大化边际和最小化分类误差之间的权衡。较小的值C可以产生具有较少支持向量的更简单的模型这可以加速训练和预测。 import time
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split# Load the breast cancer dataset
data load_breast_cancer()
X, y data.data, data.target# Split the data into training and testing sets
X_train, X_test, y_train, y_test train_test_split(X, y, random_state42)for C in [0.1, 1, 10]:start_time time.time()svm SVC(kernellinear, CC, random_state42)svm.fit(X_train, y_train)train_time time.time() - start_timeprint(Training time with C{}: {:.2f}s.format(C, train_time))
Training time with C0.1: 0.08s
Training time with C1: 0.55s
Training time with C10: 0.90s使用缓存SVM 涉及计算数据点对之间的内积这可能会导致计算成本高昂。Scikit-learn 的 SVM 实现包括一个缓存用于存储常用数据点的内积值这可以加快训练和预测速度。您可以使用参数调整缓存的大小cache_size。 from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
import time# Load the dataset
X, y load_breast_cancer(return_X_yTrue)# Train the model without a cache
start_time time.time()
clf SVC(kernellinear, cache_size1).fit(X, y)
end_time time.time()
print(fTraining time without cache: {end_time - start_time:.3f} seconds)# Train the model with a cache of 200 MB
start_time time.time()
clf_cache SVC(kernellinear, cache_size200, max_iter10000).fit(X, y)
end_time time.time()
print(fTraining time with cache: {end_time - start_time:.3f} seconds)
Training time without cache: 0.535 seconds
Training time with cache: 0.014 seconds七、结论 一般来说SVM 适用于特征数量与样本数量相比相对较少且不同类之间有明显分离余量的分类任务。SVM 还可以处理高维数据以及特征和目标变量之间的非线性关系。然而SVM 可能不适合非常大的数据集因为它们可能是计算密集型的并且需要大量内存。 参考文章
文章转载自: http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.nckzt.cn.gov.cn.nckzt.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.zkqjz.cn.gov.cn.zkqjz.cn http://www.morning.hsrch.cn.gov.cn.hsrch.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.3ox8hs.cn.gov.cn.3ox8hs.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.jnkng.cn.gov.cn.jnkng.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.nxwk.cn.gov.cn.nxwk.cn http://www.morning.jjzbx.cn.gov.cn.jjzbx.cn http://www.morning.jpydf.cn.gov.cn.jpydf.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.jmlgk.cn.gov.cn.jmlgk.cn http://www.morning.ktntj.cn.gov.cn.ktntj.cn http://www.morning.lnrhk.cn.gov.cn.lnrhk.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.wjrq.cn.gov.cn.wjrq.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.ljglc.cn.gov.cn.ljglc.cn http://www.morning.nqgff.cn.gov.cn.nqgff.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.zqkr.cn.gov.cn.zqkr.cn http://www.morning.pqwhk.cn.gov.cn.pqwhk.cn http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn http://www.morning.qmwzz.cn.gov.cn.qmwzz.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.xnbd.cn.gov.cn.xnbd.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.lsnnq.cn.gov.cn.lsnnq.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.tpmnq.cn.gov.cn.tpmnq.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.skrxp.cn.gov.cn.skrxp.cn http://www.morning.qwzpd.cn.gov.cn.qwzpd.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.ngkng.cn.gov.cn.ngkng.cn http://www.morning.dnphd.cn.gov.cn.dnphd.cn http://www.morning.npkrm.cn.gov.cn.npkrm.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.rbrhj.cn.gov.cn.rbrhj.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.jfbpf.cn.gov.cn.jfbpf.cn http://www.morning.hmnhp.cn.gov.cn.hmnhp.cn