1.python的一些用法
np.arange(start,stop,step,dtype):return array
Return evenly spaced values within a given interval.
Values are generated within the half-open interval [start, stop)
np.zeros(shape, dtype=None, order='C')
Return a new array of given shape and type, filled with zeros.
shape : int or tuple of ints
Shape of the new array, e.g., (2, 3) or 2.
dtype : data-type, optional
The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
order : {'C', 'F'}, optional, default: 'C'
Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
np.meshgrid(*xi, **kwargs)
Return coordinate matrices from coordinate vectors.(从坐标向量返回坐标矩阵)
import matplotlib.pyplot as plt
plt.contourf:画三维等高线
plt.plot:画点
plt.xlim: limit of x(x轴取值范围),plt.ylim同理
plt.xlabel(r'$b$') Set the x-axis label of the current axes.(设置x轴标签)
plt.show():display the figure
2.自己犯的错误:在设置y轴范围是将(-5,5)写成了(-5,-5),导致图像怎么画都不正确,一度怀疑是数据出错了,结果只是手动设置范围时出错
3.
import numpy as np
np.random.shuffle(X)
对数组的第一维度进行随机打乱,即在训练中打乱每个样本的顺序,经常在epoch之后使用,充分打乱数据可以增加训练的多样性
4. python一次可以返回多个参数
def func_test():
return (1,2,3)
test1, test2, test3 = func_test()
5.np.concatenate:拼接数组,axis=0表示拼接行,axis=1表示拼接列
a = np.array([[1,2],[3,4]])
b = np.array([[5,6]])
np.concatenate((a,b),axis = 0)
array([[1, 2],
[3, 4],
[5, 6]])
np.concatenate((a,b.T),axis = 1)
array([[1, 2, 5],
[3, 4, 6]])
# 作为参数的数组形状必须要一致
6.np.std:计算标准差,无axis表示全局标准差,axis = 0表示每列的标准差,axis=1表示每行的标准差
>>> a = np.array([[1,2],[3,4]])
>>> np.std(a)
1.118033988749895
>>> np.std(a,axis=0)
array([1., 1.])
>>> np.std(a,axis=1)
array([0.5, 0.5])
#方差=标准差的平方
7.np.tile:复制数组,np.tile(a,2)表示将column复制2倍,np.tile(b,(2,1)):将row复制2倍,将column复制1倍
>>> b = np.array([[1,2],[3,4]])
>>> np.tile(b,2)
array([[1, 2, 1, 2],
[3, 4, 3, 4]])
>>> np.tile(b,(2,1))
array([[1, 2],
[3, 4],
[1, 2],
[3, 4]])
8.reshape:改变数组的维度
np.squeeze():把shape中为1的维度去掉
>>> e = np.arange(10)
>>> e
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> e.reshape(1,1,10)
array([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]])
>>> e.reshape(1,10,1)
array([[[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]]])
>>> np.squeeze(e)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
9.np.clip(num,bound1,bound2)
给定区间[bound1,bound2],如果num<bound1,num=bound1;如果num>bound2,num=bound2
>>> np.clip(0.5555,0,1)
0.5555