svm算法python实现
时间: 2023-09-09 10:09:52 浏览: 37
以下是使用Python实现SVM算法的示例代码:
```python
from sklearn import svm
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# 加载数据集
iris = load_iris()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=0)
# 创建SVM分类器
clf = svm.SVC(kernel='linear', C=1)
# 训练模型
clf.fit(X_train, y_train)
# 预测测试集结果
y_pred = clf.predict(X_test)
# 计算准确率
accuracy = clf.score(X_test, y_test)
print('Accuracy:', accuracy)
```
在上面的示例中,我们使用了sklearn库中的SVM分类器模型,加载了鸢尾花数据集,将数据集划分为训练集和测试集,并使用训练集训练模型。最后,我们根据测试集数据进行预测并计算准确率。
请注意,SVM算法的性能和准确性取决于所选择的内核和正则化参数。在上面的示例中,我们使用了线性内核和C=1的正则化参数。您可以尝试使用不同的内核和正则化参数,以获得更好的性能和准确性。
相关问题
svm算法python实现鸢尾花
分类问题的代码是怎样的?
您好,以下是实现鸢尾花分类问题的 SVM 算法 Python 代码示例:
``` python
# 导入所需要的库
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# 加载鸢尾花数据集
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=42)
# 创建 SVM 模型
model = SVC(kernel='linear')
# 训练模型
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() 函数划分数据集,得到训练集 X_train,y_train 和测试集 X_test,y_test。接着创建 SVM 模型,这里使用线性 SVM(kernel='linear')。然后使用 fit() 函数训练模型,预测测试集的标签并计算准确率。最后输出准确率即可。
svm算法python代码实现
下面是一个简单的 SVM 算法 Python 代码实现,使用 scikit-learn 库:
```python
from sklearn import svm
from sklearn import datasets
from sklearn.model_selection import train_test_split
# 加载 iris 数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
# 划分训练集和测试集
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)
# 训练模型
clf.fit(X_train, y_train)
# 预测测试集
y_pred = clf.predict(X_test)
# 输出分类器的准确率
print("Accuracy:", clf.score(X_test, y_test))
```
在这个例子中,我们使用了 iris 数据集进行 SVM 分类器的训练和测试。首先,我们将数据集划分为训练集和测试集。然后,我们创建了一个 SVM 分类器,使用线性核函数和 C 值为 1。接着,我们使用训练集拟合 SVM 模型,并使用测试集测试模型的准确率。最后,我们输出分类器的准确率。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)