资源简介
支持向量机SVM求解鸢尾花分类问题,分别用rbf、poly、linear核函数求解
代码片段和文件信息
print(__doc__)
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets svm
from pylab import *
mpl.rcParams[‘font.sans-serif‘] = [‘SimHei‘]
import pandas as pd
iris = datasets.load_iris()
print(iris.keys())
iris = datasets.load_iris()
X = iris.data # X.shape=(1504)
y = iris.target # y.shape=(150)
X = X[y != 0 :2] # X.shape=(1002) select the first two features of X
y = y[y != 0];print(y) # y.shape=(100)
n_sample = len(X) # n_sample=100
np.random.seed(0)
order = np.random.permutation(n_sample) # 排列,置换 If ‘x‘ is an integer randomly permute ‘‘np.arange(x)‘‘.If ‘x‘ is an array make a copy and shuffle the elements randomly.
X = X[order]
y = y[order].astype(np.float)
X_train = X[:int(.9 * n_sample)]
y_train = y[:int(.9 * n_sample)]
X_test = X[int(.9 * n_sample):]
y_test = y[int(.9 * n_sample):]
# fit the model
for fig_num kernel in enumerate((‘linear‘ ‘rbf‘ ‘poly‘)): # 径向基函数 (Radial Basis Function 简称 RBF)常用的是高斯基函数
clf = svm.SVC(kernel=kernel gamma=10) # gamma is the Kernel coefficient for ‘rbf‘ ‘poly‘ and ‘sigmoid‘.
clf.fit(X_train y_train)
print(clf.support_); # Indices of support vectors: array-like shape = [n_SV] indices of support vectors in x_train
print(clf.support_vectors_) # Support vectors: array-like shape = [n_SV n_features] support vectors in x_train
print(clf.n_support_) # Number of support vectors for each class: array-like dtype=int32 shape = [n_class]
print(clf.decision_function(X_train)) # Distance of the samples X to the separating hyperplane.
plt.figure(str(kernel))
plt.xlabel(‘x1‘)
plt.ylabel(‘x2‘)
# plt
相关资源
- python svm算法源码
- 基于Python的SVM解决异或问题
- ga_aco_opt_on_anfis_svm-master 采用GA、ACO分
- python粒子群算法优化svm模型
- pima_data.csv印第安人糖尿病数据集
- SVM支持向量机(python)
- python Support Vector Machine(SVM)
- svm-simple.py(matplotlib)
- 机器学习实战python2SVM与核函数 训练数
- sift_kmeans_svm
- SVM代码_python
- Python实现基于SVM的有效手写识别程序
- 粒子群优化SVM.rar
- SVM算法Python实现
- 机器学习经典分类算法
- 对泰坦尼克号数据集的简单分析决策
- 简单SVM支持向量机Python代码
- libsvm gridregression.py
评论
共有 条评论