在机器学习任务中经常做的一件事就是初始化参数,需要用常数值或者随机值来创建一个固定大小的矩阵。
zeros()函数:返回给定形状和类型的零数组。
x = np.zeros(5) x array([0., 0., 0., 0., 0.]) x = np.zeros([2, 3]) x array([[0., 0., 0.], [0., 0., 0.]])zeros_like()函数:返回与给定数组形状和类型相同的零数组。
x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.zeros_like(x) print(x) print("*"*20) print(y) [[1 2 3] [4 5 6]] ******************** [[0 0 0] [0 0 0]]ones()函数:返回给定形状和类型的1数组。
ones_like()函数:返回与给定数组形状和类型相同的1数组。
eye()函数:返回一个对角线上为1,其它地方为零的单位数组。
#对角阵 x = np.eye(4) print(x) [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]]不规则的对角阵
x = np.eye(2, 3) print(x) [[1. 0. 0.] [0. 1. 0.]]identity()函数:返回一个规则的对角单位矩阵。
x = np.identity(5) print(x) [[1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.]]diag()函数:提取对角线或构造对角数组。
x = np.arange(9).reshape((3, 3)) print(x) print("******************") # x 是矩阵,k是偏移量 print(np.diag(x)) print(np.diag(x, k=1)) print(np.diag(x, k=-1)) # 输出结果 [[0 1 2] [3 4 5] [6 7 8]] ****************** [0 4 8] [1 5] [3 7]可以根据给定的对角线元素自主创建对角矩阵
v = [1, 3, 5, 7] x = np.diag(v) x ****************** array([[1, 0, 0, 0], [0, 3, 0, 0], [0, 0, 5, 0], [0, 0, 0, 7]])full()函数:返回一个常数数组。
#根据形状,来填充需要的数字 x = np.full((3, 7), 2) x array([[2, 2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2, 2]])full_like()函数:返回与给定数组具有相同形状和类型的常数数组。
x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.full_like(x, 14) print(x) print(y) [[1 2 3] [4 5 6]] [[14 14 14] [14 14 14]]arange()函数:返回给定间隔内的均匀间隔的值。
x = np.arange(7) x array([0, 1, 2, 3, 4, 5, 6]) x = np.arange(1, 10, 2) x array([1, 3, 5, 7, 9])linspace()函数:返回指定间隔内的等间隔数字。
x = np.linspace(start=0, stop=2, num=9) x array([0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])logspace()函数:返回数以对数刻度均匀分布。
x = np.logspace(0, 1, 5) #np.around 返回四舍五入后的值,可指定精度。 print(np.around(x, 2)) [ 1. 1.78 3.16 5.62 10. ]numpy.random.rand() 返回一个由[0,1)内的随机数组成的数组。
x = np.linspace(start=0, stop=1, num=5) print(x) print(np.around(x, 2)) x = [1 + i for i in x] print(np.around(x, 2)) x [0. 0.25 0.5 0.75 1. ] [0. 0.25 0.5 0.75 1. ] [1. 1.25 1.5 1.75 2. ] [1.0, 1.25, 1.5, 1.75, 2.0]np.random.random返回半开区间[0,1)之间的浮点数
x = np.random.random(5) print(x) [0.91388877 0.6242679 0.28931741 0.85518187 0.48879178] x = np.random.random([2, 3]) print(x) [[0.38672352 0.60539949 0.27885679] [0.70738127 0.11434968 0.65635669]]结构数组,首先需要定义结构,然后利用np.array()来创建数组,其参数dtype为定义的结构。
personType = np.dtype({ 'names': ['name', 'age', 'weight'], 'formats': ['U30', 'i8', 'f8']}) a = np.array([('Liming', 24, 63.9), ('Mike', 15, 67.), ('Jan', 34, 45.8)], dtype=personType) print(a, type(a)) [('Liming', 24, 63.9) ('Mike', 15, 67. ) ('Jan', 34, 45.8)] <class 'numpy.ndarray'>在使用 numpy 时,你会想知道数组的某些信息。很幸运,在这个包里边包含了很多便捷的方法,可以给你想要的信息。
numpy.ndarray.ndim用于返回数组的维数(轴的个数)也称为秩,一维数组的秩为 1,二维数组的秩为 2,以此类推。
numpy.ndarray.shape表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
numpy.ndarray.size数组中所有元素的总量,相当于数组的shape中所有元素的乘积,例如矩阵的元素总量为行与列的乘积。
numpy.ndarray.dtype ndarray 对象的元素类型。
numpy.ndarray.itemsize以字节的形式返回数组中每一个元素的大小。
b = np.array([[1, 2, 3], [4, 5, 6.0]]) print(b) #维度 print(b.ndim) #形状 print(b.shape) #元素个数 print(b.size) #类型 print(b.dtype) #返回字节大小 print(b.itemsize) [[1. 2. 3.] [4. 5. 6.]] 2 (2, 3) 6 float64 8通过python之中的list来生成ndarray
list1 = [1,2,3,4] ndarray = np.array(list1) print(ndarray) print(type(ndarray)) [1 2 3 4] <class 'numpy.ndarray'>直接生成ndarray
#生成序列 list2 = np.array(range(10)) print(list2) print(type(list2)) [0 1 2 3 4 5 6 7 8 9] <class 'numpy.ndarray'>设置播下种子来固定每次生成的随机数
#设置种子来固定每次生成的随机数 np.random.seed(2) #随机生成10行两列 list3 = np.array(np.random.rand(10,2)) print(list3) print(type(list3)) [[0.4359949 0.02592623] [0.54966248 0.43532239] [0.4203678 0.33033482] [0.20464863 0.61927097] [0.29965467 0.26682728] [0.62113383 0.52914209] [0.13457995 0.51357812] [0.18443987 0.78533515] [0.85397529 0.49423684] [0.84656149 0.07964548]] <class 'numpy.ndarray'> #范围1-10,生成4个数字 list4 = np.array(np.random.randint(1,10,4)) print(list4) print(type(list4)) [5 7 4 3] <class 'numpy.ndarray'>在ndarray中所有元素必须是同一类型,否则会自动向下转换,int->float->str
a = np.array([1, 2, 3, 4, 5]) print(a) # [1 2 3 4 5] b = np.array([1, 2, 3, 4, '5']) print(b) # ['1' '2' '3' '4' '5'] c = np.array([1, 2, 3, 4, 5.0]) print(c) # [1. 2. 3. 4. 5.] [1 2 3 4 5] ['1' '2' '3' '4' '5'] [1. 2. 3. 4. 5.]要用is 来判断nan的数值 而不是等于号==
print(np.nan == np.nan) print(np.nan != np.nan) print(np.nan is np.nan) False True True创建以D(天为单位的时间类型)
a = np.datetime64('2020-03-01') print(a, a.dtype) 2020-03-01 datetime64[D]创建以M(月为单位的时间类型)
a = np.datetime64('2020-03') print(a, a.dtype) 2020-03 datetime64[M]以年为单位的方法同上
创建时间如果不写到具体的日,但是指定类型为D 会默认生成当月1号的日期
#指定生成类型单位 a = np.datetime64('2020-03', 'D') print(a, a.dtype) b =np.datetime64('2020-03-01') print(b,b.dtype) print(a==b) 2020-03-01 datetime64[D] 2020-03-01 datetime64[D]is_busday() 函数判断工作日
#2020-10-18 周日 a = np.is_busday('2020-10-18') b = np.is_busday('2020-10-19') print(a) print(b) False True统计一个 datetime64[D] 数组中的工作日天数。
begindates = np.datetime64('2020-10-18') enddates = np.datetime64('2020-10-30') a = np.arange(begindates, enddates, dtype='datetime64') b = np.count_nonzero(np.is_busday(a)) print(a) print(np.is_busday(a)) print(b) ['2020-10-18' '2020-10-19' '2020-10-20' '2020-10-21' '2020-10-22' '2020-10-23' '2020-10-24' '2020-10-25' '2020-10-26' '2020-10-27' '2020-10-28' '2020-10-29'] [False True True True True True False False True True True True] 9返回两个日期之间的工作日数量
begindates = np.datetime64('2020-10-18') enddates = np.datetime64('2020-10-30') a = np.busday_count(begindates, enddates) b = np.busday_count(enddates, begindates) print(a) print(b) # 根据起始时间,正数代表向后的日期,负数代表向前的日期 9 -9参考: np.random用法 numpy中的np.nan