Understanding Data Types in Python

노정훈·2023년 7월 9일
0

Numpy

목록 보기
1/10

A Python Integer is more than just an Integer

  • C integer is essentially a label for a position in memory whose bytes encode an interger value
  • Python integer is a pointer to a position in memory containing all the Python object information, including the bytes that contain the integer value

A Python List is more than just a List

  • Because of Python's dynamic typing, we can even create heterogeneous lists.
  • To allow these flexible types, each item in the list must contain its own type, reference count, and other information

  • At the implementation level, the array essentially contains a single pointer to one contiguous block of data.
  • The Python list contains a pointer to a block of pointers each of which in turn points to a full Python object.

Summary

  • 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.

Fixed-Type Arrays in Python

  • The built-in 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])

Creating Arrays from Python Lists

# 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])
  • If types do not match, Numpy will upcast if possible. For example, integers are upcast to floating point like this:
# In[3]
np.array([3.14,4,2,3])
# Out[3]
array([3.14,4., 2.,3.])
  • We can use 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)
  • Here, float32 means single precision float. Sign bit, 8 bits Exponent, 23 bits Mantissa

Creating Arrays from Scratch

  • For larger arrays, it is more efficient to create arrays from scratch using routines built into Numpy.

np.zeros

  • Create array filled with 0
# In[5]
np.zeros(10,dtype=int)
# Out[5]
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

np.ones

  • Create array filled with 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

  • Create an array filled with a linear sequence
  • Similar to the built-in range function
# 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.
  • Typing parameters (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

  • Create identify matrix
# In[13]
np.eye(3)
# Out[13]
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

np.empty

  • Create an uninitialized array
  • memory location
# In[14]
np.empty(3)
# Out[14]
array([1., 1., 1.])
profile
노정훈

0개의 댓글