python实现svm算法
时间: 2023-07-26 15:33:21 浏览: 79
下面是一个简单的 Python 实现 SVM 算法的代码示例,使用了 Scikit-learn 库:
```python
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
# 加载数据集
iris = datasets.load_iris()
X = iris.data[:, :2]
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)
# 创建分类器
clf = SVC(kernel='linear')
# 训练模型
clf.fit(X_train, y_train)
# 预测结果
y_pred = clf.predict(X_test)
# 打印准确率
print("Accuracy:", clf.score(X_test, y_test))
```
这段代码中使用了鸢尾花数据集,将其特征向量缩减为前两个特征,创建了一个线性 SVM 分类器,并使用 train_test_split 函数将数据集划分为训练集和测试集。然后,使用 fit 函数训练模型,并用 predict 函数进行预测。最后,使用 score 函数计算准确率。
相关问题
使用python实现svm算法实例
在Python中,我们可以使用scikit-learn库来实现支持向量机(SVM)算法。下面是一个简单的示例代码:
```python
# 导入所需的库
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 创建特征矩阵 X 和目标向量 y
X = [[1, 2], [2, 3], [3, 4], [4, 5]] # 特征矩阵
y = [0, 0, 1, 1] # 目标向量
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建SVM模型并拟合训练数据
model = SVC()
model.fit(X_train, y_train)
# 在测试集上进行预测
y_pred = model.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("准确率:", accuracy)
```
在这个示例中,我们首先导入了所需的库,然后创建了特征矩阵 `X` 和目标向量 `y`。接下来,我们使用 `train_test_split` 函数将数据集划分为训练集和测试集。然后,我们创建了一个支持向量机模型,并使用训练数据拟合该模型。最后,我们使用测试集进行预测,并计算准确率来评估模型的性能。
请注意,这只是一个简单的SVM实现示例,实际应用中可能还需要进行特征工程、数据预处理和模型调参等步骤。
用python实现svm算法对鸢尾花的分类,并计算分类准确度
好的,下面是使用Python实现SVM算法对鸢尾花进行分类,并计算分类准确度的代码:
```python
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.metrics import accuracy_score
# 加载鸢尾花数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
# 数据集划分,70%作为训练集,30%作为测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# SVM分类器训练
clf = svm.SVC(kernel='linear', C=1.0)
clf.fit(X_train, y_train)
# 预测测试集
y_pred = clf.predict(X_test)
# 计算准确度
acc = accuracy_score(y_test, y_pred)
print("分类准确度:", acc)
```
在这段代码中,首先使用`sklearn`库加载鸢尾花数据集,并将其特征向量和标签分别存储在`X`和`y`中。然后使用`train_test_split`函数将数据集划分为训练集和测试集,其中测试集占总数据集的30%。接着,使用`svm.SVC`函数定义一个SVM分类器,并使用训练集对其进行训练。最后,使用`predict`函数对测试集进行预测,并计算分类准确度。
需要注意的是,这里使用的是线性核函数,如果要尝试其他核函数,可以将`kernel`参数设为`poly`或`rbf`,并根据需要调整其他参数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)