参考自:用Python从头开始实现一个神经网络 首先我们需要一个可以操作的数据集,scikit-learn提供了一些有用的数据集生成器,只需使用make_moons这个函数就可以。 np.random.seed(5) 相当于设置了一个盛有随机数的“聚宝盆”,一个数字代表一个“聚宝盆 x,y=sklearn.datasets.make_moons(300,noise=0.30) def make_moons(n_samples=100, shuffle=True, noise=None, random_state=None): plt.scatter(x[:,0],x[:,1],s=40,c=y,cmap=plt.cm.Spectral) X = np.array([[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[16,17],[18,19]]) 训练一个Logistic回归分类器,这个分类器的输入是坐标x、y,它的输出是预测的数据类型(0或1)。为了方便,我们使用scikit-learn中的Logistic Regression类。 clf=sklearn.linear_model.LogisticRegressionCV() clf.fit(x,y) 类中的一个方法:训练模型 x_min, x_max = x[:, 0].min() – .5, x[:, 0].max() + .5 Z = pred_func(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contour(xx, yy, Z, cmap=plt.cm.Spectral)
基于python和sklearn的Logistic回归分类器
虽然我很菜,但要一直努力鸭!<&.&>import numpy as np import sklearn from sklearn import* import matplotlib.pyplot as plt np.random.seed(5) x,y=sklearn.datasets.make_moons(300,noise=0.30) #print(x,y) plt.scatter(x[:,0],x[:,1],s=40,c=y,cmap=plt.cm.Spectral) #plt.show() clf=sklearn.linear_model.LogisticRegressionCV() clf.fit(x,y) def plot_decision_boundary(pred_func): # Set min and max values and give it some padding # x_min, x_max = x[:, 0].min() - .5, x[:, 0].max() + .5 y_min, y_max = x[:, 1].min() - .5, x[:, 1].max() + .5 h = 0.01 # Generate a grid of points with distance h between them xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # Predict the function value for the whole gid Z = pred_func(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) # Plot the contour and training examples plt.contour(xx, yy, Z, cmap=plt.cm.Spectral) plt.scatter(x[:, 0], x[:, 1], c=y, cmap=plt.cm.Spectral) # Plot the decision boundary plot_decision_boundary(lambda x: clf.predict(x)) plt.title("Logistic Regression") plt.show()
1. 生成数据集
np.random.seed(5) x,y=sklearn.datasets.make_moons(300,noise=0.30) #print(x,y) plt.scatter(x[:,0],x[:,1],s=40,c=y,cmap=plt.cm.Spectral) #plt.show()
参考于here and here
300: 点的数目
noise: 加入高斯噪声,大小可表示其混乱程度
plt.scatter() 为绘制散点图函数
print(X[:,0] )
Output:[ 0 2 4 6 8 10 12 14 16 18]
x[:,0] 即取出位置信息的x坐标值
x[:,1] 即取出位置信息的y坐标值
s=40可改变点的大小
c=y
cmap=plt.cm.Spectral
是给label为1的点一种颜色,给label为0的点另一种颜色2. 训练Logistic回归器
clf=sklearn.linear_model.LogisticRegressionCV() clf.fit(x,y) def plot_decision_boundary(pred_func): # Set min and max values and give it some padding # x_min, x_max = x[:, 0].min() - .5, x[:, 0].max() + .5 y_min, y_max = x[:, 1].min() - .5, x[:, 1].max() + .5 h = 0.01 # Generate a grid of points with distance h between them xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # Predict the function value for the whole gid Z = pred_func(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) # Plot the contour and training examples plt.contour(xx, yy, Z, cmap=plt.cm.Spectral) plt.scatter(x[:, 0], x[:, 1], c=y, cmap=plt.cm.Spectral) # Plot the decision boundary plot_decision_boundary(lambda x: clf.predict(x)) plt.title("Logistic Regression") plt.show()
定义一个LogisticRegressionCV类clf
def fit(self, X, y, sample_weight=None):
可以直接Go to去看其定义
y_min, y_max = x[:, 1].min() – .5, x[:, 1].max() + .5
h = 0.01
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
定义X、Y轴,然后生成网格点矩阵
np.meshgrid: Look here
np.arrange: Look here
注意函数的传递
xx.ravel():扁平化操作 Look here
np.c_添加列 np.r_[]添加行 Look here
按照xx的格式修改矩阵 shape/reshape: here
plt.coutour与plt.cout.coutourf的区别:绘制轮廓,填充轮廓
plot_decision_boundary(lambda x: clf.predict(x))
lambda:here
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算