Data analysis study notes (1) - numpy: creation and type conversion of array objects

create array object

function illustrate
arange Similar to python's built-in range, but returns an ndarray instead of a list
array Convert the input data (list, tuple, array, or other sequence type) to an ndarray. Either infer the dtype, or explicitly specify the dtype. Copy the input data directly by default
asarray Convert the input data to an ndarray, and do not copy if the input itself is an ndarray
ones、ones_like Creates an array of all 1s according to the specified shape and dtype. ones_like takes another array as argument and creates an array of all ones based on its shape and dtype
zeros、zeros_like Similar to ones and ones_like, but produces an array of all 0s
empty、empty_like Creates a new array, allocating memory but not filling any values
eye、identity Create a square NxN identity matrix (1 for Diagon Alley and 0 for the rest)

Array object properties and methods

property/method illustrate
help dimension
size number of elements
shape the shape of the array
itemsize bytes per element
nbytes total bytes
real For the real part of complex types
imag Imaginary part for complex types
dtype Type used to specify or output the elements of an array object
reshape Divide the array into a specified shape: data.reshape(2,3,4)
astype Type conversion: arr.astype(np.float64)

some chestnuts

  • Create a one-dimensional array
a = np.arange(24) # 0-23的一维数组
  • output some information
print(a)        # 输出数组
print(a.dtype)  # 输出类型
print(a.shape)  # 输出类型
输出:
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
int64
(24,)
  • Create a multidimensional array
# 将之前创建的一维数组 a 转换为 2维度,34列数组
b = a.reshape(2,3,4)
print(b)        # 输出数组
print(b.dtype)  # 输出类型
print(b.shape)  # 输出类型
输出:
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
int64
(2, 3, 4)
  • array creates a one-dimensional array
np.array([0,1,2,3]) # 等同于 np.arange(4)
  • array creates a multidimensional array
np.array([np.arange(4),[4,5,6,7]])
  • Create an array of all 0s/1s
np.ones(10) # 创建全1的一维数组 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
np.ones((2,3,4))  # 创建多维数组
输出:
[[[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]

 [[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]]

np.zeros(10)    # 创建全0的一维数组:[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
np.zeros((4,4)) # 创建全0的多维数组
输出:
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
  • array properties
a = np.arange(24).reshape(2,3,4)
a.ndim   # 维数    3
a.size   # 数量    24
a.itemsize   # 每个元素所占的字节     8
a.nbytes     # 总占字节              192 (24*8)
a.dtype      # 元素的类型             int64
a.flat       # <numpy.flatiter object at 0x10214b600>
[item for item in a.flat]    # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
a.flat[2]    # 2
# 创建复数数组
b = np.array(np.arange(4),dtype=np.complex) 
b.real       # [0. 1. 2. 3.]
b.imag       # [0. 0. 0. 0.]

type of data

Types of type code illustrate
int8、uint8 i1、u1 Signed and unsigned 8-bit (1 byte) integers
int16 、 uint16 i2、u2 Signed and unsigned 8-bit (2 bytes) integers
int32、uint32 i4、u4 Signed and unsigned 8-bit (4 bytes) integers
int64、uint8 i8 、 u8 Signed and unsigned 8-bit (8 bytes) integers
float16 f2 half-precision floating point
float32 f4 or f Standard single-precision floating-point number. Compatible with C's float
float64 f8 or d Standard single-precision floating-point number. Compatible with C's double and Python's float objects
float128 f16 or g extended precision floating point
complex64、complex128、complex258 c8、c16、c32 A complex number represented by two 32-bit, 64-bit, and 128-bit floating-point numbers, respectively
bool ? Boolean type that stores True and False values
object O Python object type
string_ S Fixed-length string type (1 byte per character), for example, to create a string of length 10, you should use S10
unicode_ U Fixed-length unicode type (number of bytes is platform-dependent). The same way as strings are defined, such as U10

some chestnuts

  • conversion type 1
np.float64(10)  # 转换为浮点型    10.
np.int(13.2)    # 转换为整型     13
np.bool(12.3)   # 转换为布尔型    True
np.complex(3)   # 转换为复数类型   (3+0j)
np.int((2+3j))  # 不能将复数转换为整型或者浮点型
  • conversion type 2
# 创建一个例子数组
a = np.arange(1,5)
print(a.dtype)          # 该数组类型为:int64
# 将其转换为浮点型
float_a = a.astype(np.float32)  
print(float_a.dtype)    # float32
# 将其转换为整型
int_a = float_a.astype(np.int8)
print(int_a.dtype)      # int8
# 创建字符数组
numeric_strings = np.array(['1.25', '-9.6', '42'], dtype='S')
# 将其转换为浮点型
numeric_strings = numeric_strings.astype(np.float)
print(numeric_strings.dtype)    # float64
  • Specify type
a = np.arange(4,dtype=np.float) # 指定其类型为float
print(a.dtype)  # 输出:float64
  • Properties of the dtype class
# dtype类的属性
t = np.dtype('d')
print(t.char)   # d,即通过指定'd'即可获取类型float64
print(t.type)   # <class 'numpy.float64'>
print(t.str)    # <f8
  • data type in bytes
a = np.array([[1,2],[3,4]])
print(a.dtype.itemsize) # 8 
  • Create custom data types
# 自定义一个新类型
myType = np.dtype([('name', np.string_, 40), ('age', np.int32), ('height', np.float)])
print(myType)   # [('name', 'S40'), ('age', '<i4'), ('height', '<f8')]
# 使用新类型创建一个数组
array = np.array([('lolita0164', 25, 170.0), ('luo', 19, 165.3)], dtype=myType)
# 取出第0个
print(array[0])     # (b'lolita0164', 25, 170.)
# 取出第0个,'name'属性的值
print(array[0]['name']) # b'lolita0164'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325698215&siteId=291194637