https://numpy.org/doc/stable/user/basics.creation.html
age = np.array(35) # zero-dimensional array print(age) # 35 print(age.shape) # (), empty tuple print(age.ndim) # 0
a = np.array( [2, 3, 4] ) # from a regular Python list or tuple # a = np.array( [2, 3, 4], dtype=int ) # explicite print(a.ndim) # 1, one-dimensional array print(a.shape) # (3,) print(b.dtype) # dtype('int64'), deduced
b = np.array( [(1.5, 2, 3), [4, 5, 6]] ) # note mix of tuple and list # b = np.array( [(1.5, 2, 3), [4, 5, 6]], dtype=float ) # explicite print(b.ndim) # 2, two-dimensional array print(b.shape) # (2, 3) print(b.dtype) # dtype('float64'), deduced
c = np.array( [[1, 2], [3, 4], [5, 6]], dtype=complex ) print(c.ndim) # 2, two-dimensional array print(c.shape) # (3, 2) print(c.dtype) # dtype('complex128')
# numpy.zeros(shape, dtype=float, order='C') x = np.zeros( 8 ) # float zeros, x.shape=(8,) y = np.zeros( (3, 4), dtype=int ) # int zeros, x.shape=(3, 4)
# numpy.ones(shape, dtype=None, order='C') x = np.ones( 4 ) # default dtype=float y = np.ones( (2, 3, 4), dtype=int ) # y.shape=(2, 3, 4)
# numpy.arange([start,] stop[, step,], dtype=None) x = np.arange(8) # array([0, 1, 2, 3, 4, 5, 6, 7]), dtype('int64') y = np.arange(8.0) # array([0., 1., 2., 3., 4., 5., 6., 7.]), dtype('float64')
# numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0) x = np.linspace(0, 2, num=9, dtype=float) # 9 numbers float (default), default num=50 # array([0., 0.25, 0.5, 0.75, 1., 1.25, 1.5, 1.75, 2.])
# numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0) # In linear space, the sequence starts at 'base ** start' and ends with 'base ** stop'. x = np.logspace(1, 2, 11) # array([ 10. , 12.58925412, 15.84893192, 19.95262315, # 25.11886432, 31.6227766 , 39.81071706, 50.11872336, # 63.09573445, 79.43282347, 100. ]) # np.logspace is equivalent to the code: # y = np.linspace(start, stop, num, endpoint) # x = np.power(base, y).astype(dtype)
# numpy.empty(shape, dtype=float, order='C') x = np.empty( (2, 3) ) # uninitialized, default float
# numpy.full(shape, fill_value, dtype=None, order='C') x = np.full((2, 3), 4) # array([[4, 4, 4], [4, 4, 4]]), dtype=int deduced # This is faster than # x = 4 * np.ones((2, 3), dtype=int)
# https://github.com/ufkapano/pyquats from pyquats.quats import Quat a = np.array([Quat(1, 2, 3, 4), Quat(5, 6, 7, 8)]) # general objects # array([Quat(1.0, 2.0, 3.0, 4.0), Quat(5.0, 6.0, 7.0, 8.0)], dtype=object) print(a.itemsize) # 4 (32-bit system) or 8 (64-bit system) print(a.sum()) # Quat(6.0, 8.0, 10.0, 12.0) b = np.empty((10, 20), dtype=object)
s = np.array(["alpha", "beta", "gamma"]) # array(['alpha', 'beta', 'gamma'], dtype='|S5') # Py2 # array(['alpha', 'beta', 'gamma'], dtype='<U5') # Py3 # 5 is the length of the longest string in the given array object. print(s.itemsize ) # 5 for python2, 20 for python3 s[0] = "abcdefghij" # there is the limitation to the length # array(['abcde', 'beta', 'gamma'], dtype='|S5') # python2 # Solution: s = np.array(["alpha", "beta", "gamma"], dtype=object) print(s.itemsize) # 4 or 8 np.array(["ab","żółw"]) # array(['ab', '\xc5\xbc\xc3\xb3\xc5\x82w'], dtype='|S7') # Py2 # array(['ab', 'żółw'], dtype='<U4') # Py3
# numpy.identity(n, dtype=None, *, like=None) np.identity(3) # array([[1., 0., 0.], # [0., 1., 0.], # [0., 0., 1.]])
# numpy.eye(N, M=None, k=0, dtype=float, order='C', *, like=None) np.eye(3, k=1) # array([[0., 1., 0.], # [0., 0., 1.], # [0., 0., 0.]]) np.eye(3, k=-1) # array([[0., 0., 0.], # [1., 0., 0.], # [0., 1., 0.]])
# numpy.diag(v, k=0) # Extract a diagonal or construct a diagonal array. np.diag([1, 2, 3]) # array([[1, 0, 0], # [0, 2, 0], # [0, 0, 3]]) creation x = np.arange(9).reshape((3,3)) # array([[0, 1, 2], # [3, 4, 5], # [6, 7, 8]]) np.diag(x) # array([0, 4, 8]) extraction np.diag(x, k=1) # array([1, 5]) extraction np.diag(x, k=-1) # array([3, 7]) extraction