0. 理论
在数学上,支持向量机能够找出更高维的支持向量,由支持向量定义更高维的平面来将数据划分为不同的簇。通过不同的核函数可以得到不同的结果。
1.使用scikit-learn通过SVM进行人员聚集
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import scale
def createClusteredData(N, k):
"""生成随机的收入、年龄数据
Args:
N (int): N个人
k (int): k个聚类
Returns:
tuple: X为人员的收入、年龄, y为分类结果
"""
# 生成随机种子
np.random.seed(10)
# 每个聚类的数据量
pointsPerCluster = float(N)/k
X = []
y = []
# 生成k个聚类
for i in range (k):
incomeCentroid = np.random.uniform(20000.0, 200000.0)
ageCentroid = np.random.uniform(20.0, 70.0)
# 每个聚类生成数据
for j in range(int(pointsPerCluster)):
X.append([np.random.normal(incomeCentroid, 10000.0), np.random.normal(ageCentroid, 2.0)])
y.append(i)
return np.array(X), np.array(y)
N = 100
k = 5
X, y = createClusteredData(N, k)
# 画图
plt.scatter(X[:,0], X[:,1], c=y)
plt.show()
from sklearn import svm
def plotPredictions(clf):
xx, yy = np.meshgrid(np.arange(0, 250000, 10),
np.arange(10, 70, 0.5))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
plt.figure(figsize=(8, 6))
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
plt.scatter(X[:,0], X[:,1], c=y.astype(np.float))
plt.show()
def try_kernel(kernel_list):
"""使用不同的核函数查看分类结果
Args:
kernel_list (list): 核函数列表
"""
for kernel in kernel_list:
# 训练
svc = svm.SVC(kernel= kernel).fit(X, y)
# 预测
# 1)年龄为40岁,年收入为20W美金的人的分类
# 2)年龄为65岁,年收入为5W美军的人的分类
print('核函数:{}'.format(kernel))
print('预测结果: {}'.format(svc.predict([[200000, 40], [50000, 65]])))
print('可视化结果:')
# 画图
plotPredictions(svc)
print()
kernel_list = ['linear','poly','rbf','sigmoid']
try_kernel(kernel_list)
核函数:linear
预测结果: [0 3]
可视化结果:
C:\Users\Administrator\AppData\Local\Temp\ipykernel_14148\3712667999.py:11: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
plt.scatter(X[:,0], X[:,1], c=y.astype(np.float))
核函数:poly
预测结果: [0 3]
可视化结果:
C:\Users\Administrator\AppData\Local\Temp\ipykernel_14148\3712667999.py:11: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
plt.scatter(X[:,0], X[:,1], c=y.astype(np.float))
核函数:rbf
预测结果: [0 3]
可视化结果:
C:\Users\Administrator\AppData\Local\Temp\ipykernel_14148\3712667999.py:11: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
plt.scatter(X[:,0], X[:,1], c=y.astype(np.float))
核函数:sigmoid
预测结果: [3 0]
可视化结果:
C:\Users\Administrator\AppData\Local\Temp\ipykernel_14148\3712667999.py:11: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
plt.scatter(X[:,0], X[:,1], c=y.astype(np.float))
2.参考资料
Python数据科学与机器学习:从入门到实践
作者:
[美]弗兰克•凯恩(Frank Kane)