pointer
to a position in memory containing all the Python object information, including the bytes that contain the integer valueSummary
- The advantage of the list is flexibility : because each list element is a full structure containing both data and type information, the list can be filled with data of any desired type
- Fixed type Numpy-style array lack this flexibility but are much more efficient for storing and manipulating data.
array
module can be used to create dense arrays of a uniform type# In[1]
import array
L=list(range(10))
A=array.array('i',L)
A
# Out[1]
array('i',[0,1,2,3,4,5,6,7,8,9])
# we will start with the standard Numpy import
import numpy as np
# In[2]
np.array([1,4,2,5,3])
# Out[2]
array([1,4,2,5,3])
# In[3]
np.array([3.14,4,2,3])
# Out[3]
array([3.14,4., 2.,3.])
dtype
keyword to set the data type of the resulting array:# In[4]
np.array([1,2,3,4],dtype='float32')
# Out[4]
array([1.,2.,3.,4.],dtype=float32)
float32
means single precision float. Sign
bit, 8 bits Exponent
, 23 bits Mantissa
np.zeros
0
# In[5]
np.zeros(10,dtype=int)
# Out[5]
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
np.ones
1
# In[6]
np.ones((3,5),dtype=float)
# Out[6]
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
np.full
# In[7]
np.full((3,5),3.14)
# Out[7]
array([[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14]])
np.arange
# In[8]
np.arange(0,20,2)
# Out[8]
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
np.linspace
# In[9]
np.linspace(0,1,5)
# Out[9]
# create an array of five values spaced between 0 and 1
array([0. , 0.25, 0.5 , 0.75, 1. ])
np.random
# In[10]
np.random.random((3,3))
# Out[10]
# random values between 0 and 1
array([[0.45216671, 0.39915045, 0.28124739],
[0.69650235, 0.50474633, 0.7893374 ],
[0.9051 , 0.74894434, 0.3819369 ]])
# In[11]
np.random.normal(0,1,(3,3))
# Out[11]
# create an array with mean 0 and standard deviation 1
array([[ 0.60406596, -0.63038548, -0.09671667],
[-0.14400673, 0.0967629 , -1.03368308],
[ 0.24388749, 0.592264 , 0.09380424]])
np.random.normal
is normal distribution function provided by Numpy.(mean, standard deviation, number of data)
# In[12]
np.random.randint(0,10,(3,3))
# Out[12]
array([[7, 0, 3],
[5, 0, 7],
[8, 5, 1]])
np.eye
# In[13]
np.eye(3)
# Out[13]
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
np.empty
# In[14]
np.empty(3)
# Out[14]
array([1., 1., 1.])