神经网络
# 神经网络
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import make_moons # 获取数据集
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
# 生成环形数据集
x, y = make_moons(n_samples=200, noise=0.2, random_state=3)
x_train, x_test, y_train, y_test = train_test_split(x, y, stratify=y, random_state=42, test_size=0.25)
# stratify 保持训练集和测试集的数据分类比例与y一致
mlp = MLPClassifier(solver='lbfgs', random_state=0)
# solver 权重优化的求解器,lbfgs是准牛顿方法族的优化器
# 对小型数据集,lbfgs收敛较快,表现较好
mlp.fit(X_train, y_train)
# 使用不同的色块表示不同分类
# ListedColormap允许使用十六进制颜色码来定义所需的颜色库
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])
x_min, x_max = x_train[:,0].min()-1, x_train[:,0].max()+1
y_min, y_max = x_train[:,1].min()-1, x_train[:,1].max()+1
# 生成二维网格
# xx是np.arange(x_min, x_max, 0.02)重复(yy长度)次
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02))
# 绘制边界
Z = mlp.predict(np.c_[xx.ravel(), yy.ravel()]) # ravel()多维数据展平成一维 # 预测结果
# np.c_是按行连接两个矩阵,把两矩阵左右相联结,要求行数相等
Z = Z.reshape(xx.shape) # xx.shape= (206, 278) reshape重新返回网格
plt.contourf(xx, yy, Z, cmap=cmap_light) # 画出不同分类的边界线(等高线),Z高度
# 将数据特征用散点图表示出来
plt.scatter(x[:, 0], x[:, 1], c=y, edgecolors='k', s=60)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xlabel('Feature 0')
plt.ylabel('Feature 1')
plt.title('MLPClassifier')
plt.close()
K-means
# K-means
from sklearn import datasets
from sklearn.cluster import KMeans
iris = datasets.load_iris() # 获取数据集
x = iris.data # attributes
y = iris.target # 保留标签
kmeans = KMeans(n_clusters=3).fit(x) # 聚成3类
label = kmeans.labels_
print('the predicted result:\n', label) # 聚类结果
print('the real answer:\n', y) # 原始标签
# 对半环型数据集进行KMeans聚类
import matplotlib.pyplot as plt
# 生成环形数据集
X, Y = datasets.make_moons(n_samples=200, noise=0.05, random_state=0) # X为坐标数据,两个半圆弧,Y为分类
# 使用KMeans聚成两类
kmeans = KMeans(n_clusters=2)
kmeans.fit(X)
Y_pred = kmeans.predict(X)
# 绘制聚类结果
plt.scatter(X[:,0], X[:,1], c=Y_pred, s=60, edgecolor='b')
plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1],
marker='x', s=100, linewidth=2, edgecolor='k') # 绘制2个聚类中心
plt.xlabel('X')
plt.ylabel('Y')