import numpy as np
a = np.arange(15).reshape(3,5)
a
array([[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14]])
a.shape
(3,5)
a.ndim
2
a.dtype.name
'int64'
a.itemsize
8
a.size
15type(a)<class'numpy.ndarray'>
b = np.array([6,7,8])
b
array([6,7,8])type(b)<class'numpy.ndarray'>
c = np.array([[1,2],[3,4]], dtype=complex)>>> c
array([[1.+0.j,2.+0.j],[3.+0.j,4.+0.j]])
np.arange(10,30,5)
array([10,15,20,25])>>> np.arange(0,2,0.3)# it accepts float arguments
array([0.,0.3,0.6,0.9,1.2,1.5,1.8])
>>>from numpy import pi
>>> np.linspace(0,2,9)# 9 numbers from 0 to 2
array([0.,0.25,0.5,0.75,1.,1.25,1.5,1.75,2.])>>> x = np.linspace(0,2* pi,100)# useful to evaluate function at lots of points>>> f = np.sin(x)
>>> a = np.array([20,30,40,50])>>> b = np.arange(4)>>> b
array([0,1,2,3])>>> c = a - b
>>> c
array([20,29,38,47])>>> b**2
array([0,1,4,9])>>>10* np.sin(a)
array([9.12945251,-9.88031624,7.4511316,-2.62374854])>>> a <35
array([True,True,False,False])>>> A = np.array([[1,1],...[0,1]])>>> B = np.array([[2,0],...[3,4]])>>> A * B # elementwise product
array([[2,0],[0,4]])>>> A @ B # matrix product
array([[5,4],[3,4]])>>> A.dot(B)# another matrix product
array([[5,4],[3,4]])
>>> rg = np.random.default_rng(1)# create instance of default random number generator>>> a = np.ones((2,3), dtype=int)>>> b = rg.random((2,3))>>> a *=3>>> a
array([[3,3,3],[3,3,3]])>>> b += a
>>> b
array([[3.51182162,3.9504637,3.14415961],[3.94864945,3.31183145,3.42332645]])>>> a += b # b is not automatically converted to integer type
Traceback (most recent call last):...
numpy.core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'add' output from dtype('float64') to dtype('int64')with casting rule 'same_kind'
>>> b = np.arange(12).reshape(3,4)>>> b
array([[0,1,2,3],[4,5,6,7],[8,9,10,11]])>>>>>> b.sum(axis=0)# sum of each column
array([12,15,18,21])>>>>>> b.min(axis=1)# min of each row
array([0,4,8])>>>>>> b.cumsum(axis=1)# cumulative sum along each row
array([[0,1,3,6],[4,9,15,22],[8,17,27,38]])
>>> B = np.arange(3)>>> B
array([0,1,2])>>> np.exp(B)
array([1.,2.71828183,7.3890561])>>> np.sqrt(B)
array([0.,1.,1.41421356])>>> C = np.array([2.,-1.,4.])>>> np.add(B, C)
array([2.,0.,6.])