In [1]:
import numpy as np
1D Array documentation¶
In [2]:
myarr = np.array([3,6,32,7])
In [3]:
myarr
Out[3]:
array([ 3, 6, 32, 7])
In [4]:
#myarr = np.array([3,6,32888888,7], np.int8) => ❌ Memory size < number
myarr = np.array([3,6,32888888,7], np.int32)
myarr
Out[4]:
array([ 3, 6, 32888888, 7], dtype=int32)
In [5]:
myarr[0] #1D Array => [ ], 2D Array [ , ]
Out[5]:
np.int32(3)
2D Array¶
In [6]:
myarr = np.array([[3,6,32,7]], np.int64)
myarr
Out[6]:
array([[ 3, 6, 32, 7]])
In [7]:
myarr[0,1]
Out[7]:
np.int64(6)
In [8]:
myarr.dtype
Out[8]:
dtype('int64')
Change the no in Array¶
In [9]:
myarr[0,1] = 45
myarr
Out[9]:
array([[ 3, 45, 32, 7]])
Array_1) Conversion from other Python structures¶
In [10]:
listarray = np.array([[1,2,3], [5,8,5], [0,3,1]])
listarray
Out[10]:
array([[1, 2, 3], [5, 8, 5], [0, 3, 1]])
In [11]:
listarray.dtype
Out[11]:
dtype('int64')
In [12]:
listarray.shape
Out[12]:
(3, 3)
In [13]:
listarray.size
Out[13]:
9
In [14]:
np.array({34,23,53}) #not use Obj => use int or Float => for Mathematical cal...
Out[14]:
array({34, 53, 23}, dtype=object)
Array_2) Intrinsic NumPy array creation functions¶
In [15]:
zeros = np.zeros((2, 5))
zeros
Out[15]:
array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]])
In [16]:
# 3 in 1
print("Data type:", listarray.dtype)
print("Shape:", listarray.shape)
print("Size:", listarray.size)
Data type: int64 Shape: (3, 3) Size: 9
In [17]:
rng = np.arange(15) #arange create => numPy array
rng
Out[17]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
In [18]:
lspace = np.linspace(1,10,10) # 1 to 10 => total 10 equally space
lspace
Out[18]:
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
In [19]:
lspace = np.linspace(1,50,10) # 1 to 50 => total 10 equally space
lspace
Out[19]:
array([ 1. , 6.44444444, 11.88888889, 17.33333333, 22.77777778, 28.22222222, 33.66666667, 39.11111111, 44.55555556, 50. ])
Empty Array¶
In [20]:
emp = np.empty((4,6)) # 4X6 => Random elemants
emp
Out[20]:
array([[3.56043053e-307, 1.37961641e-306, 1.00132483e-307, 8.01097889e-307, 1.78020169e-306, 7.56601165e-307], [1.02359984e-306, 1.33510679e-306, 2.22522597e-306, 8.01097889e-307, 1.69120416e-306, 9.45751457e-308], [1.33508251e-307, 1.11257852e-307, 9.45697133e-308, 1.00132653e-307, 7.56562460e-307, 1.16824611e-307], [7.56561101e-307, 9.45739573e-308, 8.34425615e-308, 2.29179042e-312, 6.01334642e-154, 5.03032220e+180]])
In [21]:
emp_like = np.empty_like(lspace)
emp_like
Out[21]:
array([ 1. , 6.44444444, 11.88888889, 17.33333333, 22.77777778, 28.22222222, 33.66666667, 39.11111111, 44.55555556, 50. ])
identity Matrix¶
In [22]:
ide = np.identity(3)
ide
Out[22]:
array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
In [23]:
ide = np.identity(45)
ide
Out[23]:
array([[1., 0., 0., ..., 0., 0., 0.], [0., 1., 0., ..., 0., 0., 0.], [0., 0., 1., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 1., 0., 0.], [0., 0., 0., ..., 0., 1., 0.], [0., 0., 0., ..., 0., 0., 1.]])
In [24]:
ide.shape
Out[24]:
(45, 45)
ReShape (if possible)¶
In [25]:
arr = np.arange(33)
arr
Out[25]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32])
In [26]:
arr.reshape(11, 3)
Out[26]:
array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23], [24, 25, 26], [27, 28, 29], [30, 31, 32]])
In [27]:
arr.reshape(3, 11)
Out[27]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]])
But¶
arr.reshape(3, 10) ❌
Ravel , 2D => 1D Array¶
In [28]:
arr = arr.reshape(3, 11)
arr
Out[28]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]])
In [29]:
arr.shape
Out[29]:
(3, 11)
In [30]:
arr = arr.ravel()
arr
Out[30]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32])
In [31]:
arr.shape
Out[31]:
(33,)
Theory¶
In [32]:
x = [[1,2,3], [4,5,6], [7,1,0]]
In [33]:
ar = np.array(x)
ar
Out[33]:
array([[1, 2, 3], [4, 5, 6], [7, 1, 0]])
Sum¶
In [34]:
ar.sum(axis=0)
Out[34]:
array([12, 8, 9])
In [35]:
ar.sum(axis=1)
Out[35]:
array([ 6, 15, 8])
Transpose¶
In [36]:
ar.T
Out[36]:
array([[1, 4, 7], [2, 5, 1], [3, 6, 0]])
flat¶
In [37]:
ar.flat
Out[37]:
<numpy.flatiter at 0x1a39d095f00>
In [38]:
for item in ar.flat:
print(item)
1 2 3 4 5 6 7 1 0
In [39]:
ar.ndim # no of Dinension
Out[39]:
2
In [40]:
ar.size
Out[40]:
9
In [41]:
ar.nbytes # Total bite consumes
Out[41]:
72
Max - Min in Array¶
In [42]:
one = np.array([1,2,4,888,2]) # Create a 1D Array
In [43]:
one.argmax()
Out[43]:
np.int64(3)
In [44]:
one.argmin()
Out[44]:
np.int64(0)
In [45]:
one.argsort() # Max => Min
Out[45]:
array([0, 1, 4, 2, 3])
In [46]:
ar
Out[46]:
array([[1, 2, 3], [4, 5, 6], [7, 1, 0]])
In [47]:
ar.argmax() # Max value in the 6th place
Out[47]:
np.int64(6)
In [48]:
ar.argmin()
Out[48]:
np.int64(8)
In [49]:
ar.argmax(axis=0) # Max elimants in every Columns
Out[49]:
array([2, 1, 1])
In [50]:
ar.argmax(axis=1) # Max elements in every Row
Out[50]:
array([2, 2, 0])
In [51]:
ar.argsort(axis=0) # Max => Min elimants in every Columns
Out[51]:
array([[0, 2, 2], [1, 0, 0], [2, 1, 1]])
In [52]:
ar.argsort(axis=1) # Min => Max elimants in every Columns
Out[52]:
array([[0, 1, 2], [0, 1, 2], [2, 1, 0]])
In [53]:
ar.ravel()
Out[53]:
array([1, 2, 3, 4, 5, 6, 7, 1, 0])
In [54]:
ar.reshape(9, 1 )
Out[54]:
array([[1], [2], [3], [4], [5], [6], [7], [1], [0]])
In [55]:
ar.reshape(9,)
Out[55]:
array([1, 2, 3, 4, 5, 6, 7, 1, 0])
Sum of 2 Array¶
In [56]:
ar
Out[56]:
array([[1, 2, 3], [4, 5, 6], [7, 1, 0]])
In [57]:
ar2 = np.array([[1, 2, 1],
[4, 0, 6],
[8, 1, 0]])
In [58]:
ar + ar2
Out[58]:
array([[ 2, 4, 4], [ 8, 5, 12], [15, 2, 0]])
But¶
In [59]:
[324, 34] + [34, 546]
Out[59]:
[324, 34, 34, 546]
X¶
In [60]:
ar * ar2
Out[60]:
array([[ 1, 4, 3], [16, 0, 36], [56, 1, 0]])
²√¶
In [61]:
np.sqrt(ar)
Out[61]:
array([[1. , 1.41421356, 1.73205081], [2. , 2.23606798, 2.44948974], [2.64575131, 1. , 0. ]])
Sum¶
In [62]:
ar.sum()
Out[62]:
np.int64(29)
Max - Min¶
In [63]:
ar.max()
Out[63]:
np.int64(7)
In [64]:
ar.min()
Out[64]:
np.int64(0)
Find / Filter¶
In [65]:
ar
Out[65]:
array([[1, 2, 3], [4, 5, 6], [7, 1, 0]])
In [66]:
np.where(ar>5)
Out[66]:
(array([1, 2]), array([2, 0]))
In [67]:
type(np.where(ar>5))
Out[67]:
tuple
In [68]:
np.count_nonzero(ar)
Out[68]:
8
In [69]:
np.nonzero(ar)
Out[69]:
(array([0, 0, 0, 1, 1, 1, 2, 2]), array([0, 1, 2, 0, 1, 2, 0, 1]))
In [70]:
ar[1, 2] = 0
np.nonzero(ar)
Out[70]:
(array([0, 0, 0, 1, 1, 2, 2]), array([0, 1, 2, 0, 1, 0, 1]))
it's give 'tuple' for every Dimension (for every axis)
In [71]:
import sys
In [72]:
python_array = [0,4,55,2]
In [73]:
numPy_array = np.array(python_array)
In [74]:
sys.getsizeof(1) * len(python_array)
Out[74]:
112
In [75]:
numPy_array.itemsize * numPy_array.size
Out[75]:
32