一:生成数据
1. make_blobs(from sklearn.datasets.samples_generator import make_blobs):
用于生成训练样本,https://www.jianshu.com/p/069d8841bd8e
2.make_circles(from sklearn.datasets.samples_generator import make_circles):
生成圆形样本,其中factor参数为内圆与外圆的半径之比。
2. contour:画等高线(在matplotlib中)
3. meshgrid(在numpy):生成坐标网格。
4. 用以下的方法可以画3维图形
from mpl_toolkits import mplot3d r = np.exp(-(X**2).sum(1)) def plot3d(X, y): ax = plt.subplot(projection=‘3d‘) ax.scatter3D(X[:, 0], X[:, 1], r, c=y) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") plot3d(X, y)
5. format格式化字符串:https://www.runoob.com/python/att-string-format.html
二:好玩的数据集
1. 一些有名字的人脸数据:from sklearn.datasets import fetch_lfw_people
faces = fetch.lfw.people(min_faces_per_person=60) 选出用60个脸图的人。
faces有target属性,target[i]是第i张脸的名字的索引
target_name属性,存放人名。例如target_name[target[i]]就是第i张脸的名字
images属性,images是一个列表,每个元素是一个图像,也就是一个矩阵。可以用imshow(images[i])显示的i张脸的图像。这里images的shape为(1348, 62, 47)的,说明有1348张图,每张图是62*43的矩阵
data属性:就是把images属性的每个矩阵变成一行数据,用于训练。所以data属性的shape是(1348, 2914)
三:参数的选择
1.GridSearchCV:用法如下,用于参数的选择
from sklearn.model_selection import GridSearchCV param_grid = {‘svc__C‘: [1, 5, 10], ‘svc__gamma‘: [0.0001, 0.0005, 0.001]} grid = GridSearchCV(model, param_grid) grid.fit(Xtrain, ytrain) print(grid.best_params_)
属性best_params_: 最好的参数
属性best_estimator:最好的参数对应的训练模型。
四:评估值
1.混淆矩阵
from sklearn.metrics import confusion_matrix from seaborn import heatmap c = confusion_matrix(y_test, y_predict) heatmap(data=c, annot=True, xticklabels=target_names, yticklabels=target_names) ax2.set_xlabel("predict") ax2.set_ylabel("true") plt.show()
原文:https://www.cnblogs.com/loubin/p/11575689.html