Python中的numpy函数的使用ones,zeros,eye
在看别人写的代码时,看到的不知道的函数,就在这里记下来。
原文是这样用的:
1 weights = ones((numfeatures,1))
在python中help():
import numpy as np help(np.ones)
1 Help on function ones in module numpy.core.numeric: 2 3 ones(shape, dtype=None, order='C') 4 Return a new array of given shape and type, filled with ones. 5 6 Parameters 7 ---------- 8 shape : int or sequence of ints 9 Shape of the new array, e.g., ``(2, 3)`` or ``2``. 10 dtype : data-type, optional 11 The desired data-type for the array, e.g., `numpy.int8`. Default is 12 `numpy.float64`. 13 order : {'C', 'F'}, optional 14 Whether to store multidimensional data in C- or Fortran-contiguous 15 (row- or column-wise) order in memory. 16 17 Returns 18 ------- 19 out : ndarray 20 Array of ones with the given shape, dtype, and order. 21 22 See Also 23 -------- 24 zeros, ones_like 25 26 Examples 27 -------- 28 >>> np.ones(5) 29 array([ 1., 1., 1., 1., 1.]) 30 31 >>> np.ones((5,), dtype=np.int) 32 array([1, 1, 1, 1, 1]) 33 34 >>> np.ones((2, 1)) 35 array([[ 1.], 36 [ 1.]]) 37 38 >>> s = (2,2) 39 >>> np.ones(s) 40 array([[ 1., 1.], 41 [ 1., 1.]])
zeros:
1 Help on built-in function zeros in module numpy.core.multiarray: 2 3 zeros(...) 4 zeros(shape, dtype=float, order='C') 5 6 Return a new array of given shape and type, filled with zeros. 7 8 Parameters 9 ---------- 10 shape : int or sequence of ints 11 Shape of the new array, e.g., ``(2, 3)`` or ``2``. 12 dtype : data-type, optional 13 The desired data-type for the array, e.g., `numpy.int8`. Default is 14 `numpy.float64`. 15 order : {'C', 'F'}, optional 16 Whether to store multidimensional data in C- or Fortran-contiguous 17 (row- or column-wise) order in memory. 18 19 Returns 20 ------- 21 out : ndarray 22 Array of zeros with the given shape, dtype, and order. 23 24 See Also 25 -------- 26 zeros_like : Return an array of zeros with shape and type of input. 27 ones_like : Return an array of ones with shape and type of input. 28 empty_like : Return an empty array with shape and type of input. 29 ones : Return a new array setting values to one. 30 empty : Return a new uninitialized array. 31 32 Examples 33 -------- 34 >>> np.zeros(5) 35 array([ 0., 0., 0., 0., 0.]) 36 37 >>> np.zeros((5,), dtype=np.int) 38 array([0, 0, 0, 0, 0]) 39 40 >>> np.zeros((2, 1)) 41 array([[ 0.], 42 [ 0.]]) 43 44 >>> s = (2,2) 45 >>> np.zeros(s) 46 array([[ 0., 0.], 47 [ 0., 0.]]) 48 49 >>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype 50 array([(0, 0), (0, 0)], 51 dtype=[('x', '<i4'), ('y', '<i4')])
eye:
1 Help on function eye in module numpy.lib.twodim_base: 2 3 eye(N, M=None, k=0, dtype=<class 'float'>) 4 Return a 2-D array with ones on the diagonal and zeros elsewhere. 5 6 Parameters 7 ---------- 8 N : int #行数 9 Number of rows in the output. 10 M : int, optional#列数 11 Number of columns in the output. If None, defaults to `N`. 12 k : int, optional#对角线的位置,0的时候是正对角线,+1就是对角线向上移,-1就是对角线向下移 13 Index of the diagonal: 0 (the default) refers to the main diagonal, 14 a positive value refers to an upper diagonal, and a negative value 15 to a lower diagonal. 16 dtype : data-type, optional 17 Data-type of the returned array. 18 19 Returns 20 ------- 21 I : ndarray of shape (N,M) 22 An array where all elements are equal to zero, except for the `k`-th 23 diagonal, whose values are equal to one. 24 25 See Also 26 -------- 27 identity : (almost) equivalent function 28 diag : diagonal 2-D array from a 1-D array specified by the user. 29 30 Examples 31 -------- 32 >>> np.eye(2, dtype=int) 33 array([[1, 0], 34 [0, 1]]) 35 >>> np.eye(3, k=1) 36 array([[ 0., 1., 0.], 37 [ 0., 0., 1.], 38 [ 0., 0., 0.]])