[Data analysis] ndarray study notes day06 created with the data type randomly created ndarray + + ndarray of multidimensional arrays + + data type to create a sequence of +1 ndarray. `Dtype` parameters 2.` astype` party

ndarray multidimensional array (N Dimension Array)

NumPy array is a multi-dimensional array of objects (matrix), it referred to ndarray, and having the ability to complex vector arithmetic broadcast capabilities, and has performed fast and space-saving characteristics.

Note: ndarray subscript starts at 0, and the array of all elements must be of the same type

ndarray owned property

  1. ndim属性: The number of dimensions
  2. shape属性: Dimension Size
  3. dtype属性:type of data

ndarray of randomly created

Random data generated by random sampling (numpy.random).

Sample code:

# 导入numpy,别名np
import numpy as np

# 生成指定维度大小(3行4列)的随机多维浮点型数据(二维),rand固定区间0.0 ~ 1.0
arr = np.random.rand(3, 4)
print(arr)
print(type(arr))

# 生成指定维度大小(3行4列)的随机多维整型数据(二维),randint()可以指定区间(-1, 5)
arr = np.random.randint(-1, 5, size = (3, 4)) # 'size='可省略
print(arr)
print(type(arr))

# 生成指定维度大小(3行4列)的随机多维浮点型数据(二维),uniform()可以指定区间(-1, 5)
arr = np.random.uniform(-1, 5, size = (3, 4)) # 'size='可省略
print(arr)
print(type(arr))

print('维度个数: ', arr.ndim)
print('维度大小: ', arr.shape)
print('数据类型: ', arr.dtype)

operation result:

[[ 0.09371338  0.06273976  0.22748452  0.49557778]
 [ 0.30840042  0.35659161  0.54995724  0.018144  ]
 [ 0.94551493  0.70916088  0.58877255  0.90435672]]
<class 'numpy.ndarray'>

[[ 1  3  0  1]
 [ 1  4  4  3]
 [ 2  0 -1 -1]]
<class 'numpy.ndarray'>

[[ 2.25275308  1.67484038 -0.03161878 -0.44635706]
 [ 1.35459097  1.66294159  2.47419548 -0.51144655]
 [ 1.43987571  4.71505054  4.33634358  2.48202309]]
<class 'numpy.ndarray'>

维度个数:  2
维度大小:  (3, 4)
数据类型:  float64

ndarray sequence created

1. np.array(collection)

collection is the object type sequence (list), the sequence of nested objects (list of list).

Sample code:

# list序列转换为 ndarray
lis = range(10)
arr = np.array(lis)

print(arr)            # ndarray数据
print(arr.ndim)        # 维度个数
print(arr.shape)    # 维度大小

# list of list嵌套序列转换为ndarray
lis_lis = [range(10), range(10)]
arr = np.array(lis_lis)

print(arr)            # ndarray数据
print(arr.ndim)        # 维度个数
print(arr.shape)    # 维度大小

operation result:

# list序列转换为 ndarray
[0 1 2 3 4 5 6 7 8 9]
1
(10,)

# list of list嵌套序列转换为 ndarray
[[0 1 2 3 4 5 6 7 8 9]
 [0 1 2 3 4 5 6 7 8 9]]
2
(2, 10)

2. np.zeros()

0 specify the size of the entire array. NOTE: The first parameter is a tuple, to the specified size, such as (3, 4).

3. np.ones()

1 whole array specified size. NOTE: The first parameter is a tuple, to the specified size, such as (3, 4).

4. np.empty()

Initialize the array is not always return all 0s, sometimes return is not the initial random value (random value in the memory).

Sample code (2,3,4):

# np.zeros
zeros_arr = np.zeros((3, 4))

# np.ones
ones_arr = np.ones((2, 3))

# np.empty
empty_arr = np.empty((3, 3))

# np.empty 指定数据类型
empty_int_arr = np.empty((3, 3), int)

print('------zeros_arr-------')
print(zeros_arr)

print('\n------ones_arr-------')
print(ones_arr)

print('\n------empty_arr-------')
print(empty_arr)

print('\n------empty_int_arr-------')
print(empty_int_arr)

operation result:

------zeros_arr-------
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]

------ones_arr-------
[[ 1.  1.  1.]
 [ 1.  1.  1.]]

------empty_arr-------
[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]

------empty_int_arr-------
[[0 0 0]
 [0 0 0]
 [0 0 0]]

5. np.arange()andreshape()

arange () python similar to the range (), create a one-dimensional array ndarray.

reshape () will re-adjust the dimensions of the array.

Sample Code (5):

# np.arange()
arr = np.arange(15) # 15个元素的 一维数组
print(arr)
print(arr.reshape(3, 5)) # 3x5个元素的 二维数组
print(arr.reshape(1, 3, 5)) # 1x3x5个元素的 三维数组

operation result:

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

[[[ 0  1  2  3  4]
  [ 5  6  7  8  9]
  [10 11 12 13 14]]]

6. np.arange()andrandom.shuffle()

random.shuffle () array to disrupt the sequence (similar to shuffling).

Sample code (6):

arr = np.arange(15)
print(arr)

np.random.shuffle(arr)
print(arr)
print(arr.reshape(3,5))

operation result:

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

[ 5  8  1  7  4  0 12  9 11  2 13 14 10  3  6]

[[ 5  8  1  7  4]
 [ 0 12  9 11  2]
 [13 14 10  3  6]]

ndarray data types

1. dtypeParameters

Specifies the data type of the array, the number of bits + type name, such as float64, int32

2. astypeMethods

Data type conversion array

Sample code (1,2):

# 初始化3行4列数组,数据类型为float64
zeros_float_arr = np.zeros((3, 4), dtype=np.float64)
print(zeros_float_arr)
print(zeros_float_arr.dtype)

# astype转换数据类型,将已有的数组的数据类型转换为int32
zeros_int_arr = zeros_float_arr.astype(np.int32)
print(zeros_int_arr)
print(zeros_int_arr.dtype)

operation result:

[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
float64

[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]
int32

Copyright © BigCat all right reserved,powered by Gitbook「Revision Time: 2017-03-12 22:35:01」

Published 176 original articles · won praise 56 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_35456045/article/details/104084388