在吴恩达老师DL系列课程的学习过程中,跟随做的一些小练习,在看懂别人代码基础上,整理的一些小笔记。
参考:https://blog.csdn.net/u013733326/article/details/79702148
带一个隐藏层的平面数据分类:对平面上随机生成的数据(坐标点(x,y),标签值(0或1)),进行平面分割。先将已有的测试数据集全丢进去训练,训练完成之中,用迭代完成后最新逻辑参数,对平面上的所有点进行预测,(平面上的所有点:是对原有数据集的x,y扩展而成的xmin-1、xmax+1、ymin-1、ymax+1平面,在平面上以0.01【自定义】为间隔进行取分布均匀的点)。
测试集包含训练集:此练习是为了验证带一个隐藏层的神经网络的学习能力,而不在于其在不同数据集上的表现能力。
在虚拟环境中装scikit-learn
包强烈建议按照次序装
#可以选择指定具体的版本,如
conda install numpy
conda install pandas
conda install scipy
conda install matplotlib
conda install scikit-learn==0.20.0
参考:https://blog.csdn.net/weixin_44753371/article/details/103902751
scikit-learn库的算法主要有四类:分类,回归,聚类,降维。其中:
常用的回归:线性、决策树、SVM、KNN ;集成回归:随机森林、Adaboost、GradientBoosting、Bagging、ExtraTrees
常用的分类:线性、决策树、SVM、KNN,朴素贝叶斯;集成分类:随机森林、Adaboost、GradientBoosting、Bagging、ExtraTrees
常用聚类:k均值(K-means)、层次聚类(Hierarchical clustering)、DBSCAN
常用降维:LinearDiscriminantAnalysis、PCA
Scipy包
SciPy库依赖于NumPy,NumPy提供了方便和快速的n维数组操作。它们一起可以运行在所有流行的操作系统上,安装简单,使用免费。
现在,组合使用NumPy、SciPy和Matplotlib,作为MATLAB的替代品已经成为趋势。相比MATLAB,Python功能更强大、编程更容易。
参考:https://www.qikegu.com/docs/3474
随机数种子
np.random.seed(a)
参数a代表种子堆(序列)的序号,每次都是按顺序从第a堆种子中取出种子(随机数)。
python中我们常会用到numpy.random.seed()函数。
seed( ) 用于指定随机数生成时所用算法开始的整数值,如果使用相同的seed( )值,则每次生成的随即数都相同
import numpy as np
a = 0
np.random.seed(1)
while(a<5):
print("a = "+str(np.random.rand()))
a+=1
print('-------------------------')
b = 0
np.random.seed(1)
while(b<5):
print("b = "+str(np.random.rand()))
b+=1
a = 0.417022004702574
a = 0.7203244934421581
a = 0.00011437481734488664
a = 0.30233257263183977
a = 0.14675589081711304
-------------------------
b = 0.417022004702574
b = 0.7203244934421581
b = 0.00011437481734488664
b = 0.30233257263183977
b = 0.14675589081711304
python array(10,1)之间的区别array(10,)
形状为(10,)的数组是一维数组。对此在结构上应该看起来相似: a=[0,1,2,3,4,5,6,7,8,9]
import numpy as np
a = np.array([0,1,2,3,4,5,6,7,8,9])
print(a.shape) # >>> (10,)
形状为(10,1)的数组是一维数组。b=[[0,1,2,3,4,5,6,7,8,9]]
。
import numpy as np
b = np.array([range(10)]) #range(10)本身就是一个一维数组,再加一对[]变为二维
print(b.shape)
# >>>(1,10)
# >>>[[0 1 2 3 4 5 6 7 8 9]]
将形状为(10,)的一维数组reshape为二维数组。
a_reshape = a.reshape(1,10)
print(a_reshape)
#>>> [[0 1 2 3 4 5 6 7 8 9]]
python matplotlib:plt.scatter() 大小和颜色参数
前两个参数为x与y坐标的列表,要求为1维数组(可用reshape(a)或列表名.[0,:]);
c为色彩或颜色序列,一般用于颜色区分类别;
s为散点图中散点的大小;
cmap = plt.cm.Spectral实现的功能是给label为1的点一种颜色,给label为0的点另一种颜色。
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1) # 产生相同的随机数
X = np.random.randn(1, 10)
Y = np.random.randn(1, 10)
print(X,Y)
label = np.array([1,1,0,0,0,0,0,1,1,1])
plt.scatter(X.reshape(10), Y.reshape(10), c =label, s = 180, cmap = plt.cm.Spectral)
plt.show()
#spectural光谱
交叉熵
交叉熵能够衡量同一个随机变量中的两个不同概率分布的差异程度,在机器学习中就表示为真实概率分布与预测概率分布之间的差异。交叉熵的值越小,模型预测效果就越好。
在线性回归问题中,常常使用MSE(Mean Squared Error)作为loss函数,而在分类问题中常常使用交叉熵作为loss函数。
下面通过一个例子来说明如何计算交叉熵损失值。
假设我们输入一张狗的图片,标签与预测值如下:
* | 猫 | 狗 | 马 |
---|---|---|---|
Label | 0 | 1 | 0 |
Pred | 0.2 | 0.7 | 0.1 |
其中m表示样本个数。
参考:https://blog.csdn.net/b1055077005/article/details/100152102
Numpy 的 ravel() 和 flatten() 函数
简介
首先声明两者所要实现的功能是一致的(将多维数组降位一维)。这点从两个单词的意也可以看出来,ravel(散开,解开),flatten(变平)。两者的区别在于返回拷贝(copy)还是返回视图(view),numpy.flatten()返回一份拷贝,对拷贝所做的修改不会影响(reflects)原始矩阵,而numpy.ravel()返回的是视图(view,也颇有几分C/C++引用reference的意味),会影响(reflects)原始矩阵。
In [14]: x=np.array([[1,2],[3,4]])
# flattenh函数和ravel函数在降维时默认是行序优先
In [15]: x.flatten()
Out[15]: array([1, 2, 3, 4])
In [17]: x.ravel()
Out[17]: array([1, 2, 3, 4])
# 传入'F'参数表示列序优先
In [18]: x.flatten('F')
Out[18]: array([1, 3, 2, 4])
In [19]: x.ravel('F')
Out[19]: array([1, 3, 2, 4])
#reshape函数当参数只有一个-1时表示将数组降为一维
In [21]: x.reshape(-1)
Out[21]: array([1, 2, 3, 4])
#x.T表示x的转置
In [22]: x.T.reshape(-1)
Out[22]: array([1, 3, 2, 4])
参考:https://blog.csdn.net/liuweiyuxiang/article/details/78220080
决策边界绘制函数plot_decision_boundary()和plt.contourf函数详解
def plot_decision_boundary(model, X, y):
# Set min and max values and give it some padding
x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
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 grid
Z = model(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# Plot the contour and training examples
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.ylabel('x2')
plt.xlabel('x1')
plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
parameters = nn_model(X, Y, n_h = 4, num_iterations=10000, print_cost=True)
plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
#绘制边界
#lambda为匿名函数,形参为x,此x从plot_decision_boundary函数中获得;此处的predict即为分类模型
#绘制此决策边界的思路是:生成间距很小的网格覆盖这些点(函数中用h表示网格点之间的距离),
#将网格的坐标送入训练好的神经网络,神经网络会为每个网格坐标输出一个预测值,
#注意,这个网格点非常多,占满了整张图,图中的红色和蓝色其实就是经过预测后的网格点所填充的颜色,
#以预测值0.5进行划分,预测值为0.5的这条线也即为红点和蓝点的区分线。
numpy.c_[ ] 连接列 & numpy.r_[ ] 连接行
c 是 column(列)的缩写,就是按列叠加两个矩阵,就是把两个矩阵左右组合,要求行数相等。
x = np.array([1,2,3,4]).reshape(2,2)
y = np.array([5,6,7,8]).reshape(2,2)
print(x)
print(y)
z = np.c_[x,y]
print(z)
print(z.shape)
h = np.r_[x,y]
print(h)
print(h.shape)
[[1 2]
[3 4]]
[[5 6]
[7 8]]
[[1 2 5 6]
[3 4 7 8]]
(2, 4)
[[1 2]
[3 4]
[5 6]
[7 8]]
(4, 2)
注意:当两一维数组使用np.c_连接时,组成配对的坐标对(函数中,将此例h转置后当做输入X矩阵)
x = np.array([1,2,3,4])
y = np.array([5,6,7,8])
print(x)
print(y)
z = np.c_[x,y]
print(z)
print(z.shape)
h = np.r_[x,y]
print(h)
print(h.shape)
[1 2 3 4]
[5 6 7 8]
[[1 5]
[2 6]
[3 7]
[4 8]]
(4, 2)
[1 2 3 4 5 6 7 8]
(8,)
numpy.meshgrid()
一句话解释numpy.meshgrid()——生成网格点坐标矩阵。# mesh网格,grid网格
参考:https://blog.csdn.net/lllxxq141592654/article/details/81532855
语法:X,Y = numpy.meshgrid(x, y)
输入的x,y,就是网格点的横纵坐标列向量(非矩阵)
输出的X,Y,就是坐标矩阵。X与Y矩阵的维度是一样的,维度为(x列向量元素个数,y列向量元素个数)。
import numpy as np
import matplotlib.pyplot as plt
x = np.array([0, 1, 2])
y = np.array([0, 1])
X, Y = np.meshgrid(x, y)
print(X)
print(Y)
#>>>[[0 1 2]
#>>> [0 1 2]]
#>>>[[0 0 0]
#>>> [1 1 1]]
#即六个点,用矩阵表示,(x,y)坐标从矩阵中取
plt.plot(X, Y,
color='red', # 全部点设置为红色
marker='.', # 点的形状为圆点
linestyle='') # 线型为空,也即点与点之间不用线连接
plt.grid(True)
plt.show()
画出图来:
matplotlib.contourf()
plt.contourf用来画出不同分类的边界线,也常常用来绘制等高线
x = np.arange(-5, 5, 0.1)
y = np.arange(0, 20, 0.2)
xx, yy = np.meshgrid(x, y)
z = np.square(xx) - yy > 0
plt.contourf(xx, yy, z, cmap=plt.cm.Spectral)
# plt.scatter(xx, yy, c=z) #绘制散点图
plt.show()