Numpy-- array of objects ndarray

1. What numpy?

Numpy basic data for scientific computing, scientific computing not only to complete the task, can be used as efficiently cube container. For storing and processing large matrix.

 

Python provides a module array, and a different list, save values ​​directly, but due to the Python module does not support multi-dimensional array, nor a variety of computing functions.

Numpy make up for this regret. Numpy is provided a single type of data stored in a multidimensional array --ndarray

2.ndarry property

Attributes Explanation
help Return an int. It represents a dimension of the array
shape Return tuple. It indicates the size of the array, for the matrix of n rows and m columns, the shape of (n, m)
size Return an int. Represents a total number of elements of the array, the array is equal to the shape of the product
dtype Returns data-type. Type array elements is described.
itemsize

Return an int. It represents the size of each element of the array (in bytes)

 

3. Create an array (create a one-dimensional or multidimensional arrays)

numpy.array(object,dtype=None,copy=True,order='K',subok=Flase,ndmin=0)

parameter name Explanation
object Receiving array. An array you want to create. No default.
dtype Receiving data-type. Data needed to represent an array type. If not given, then selecting the minimum required to save the object type. The default is None.
ndmin Receiving int. Specifies the minimum dimension of the array should have generated. The default is None.

4. Create and view an array of array property

import numpy as np   ##导入numpy库

arr1 = np.array([1,2,3,4])  ##创建一维数组

print("创建的数组为:",arr1)


输出结果:

创建的数组为:[1 2 3 4]


arr2 = np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])  ##创建二维数组

print("数组维度为:"arr2.ndim)      ##查看数组维度

print("数组形状为:",arr2.shape)    ##查看数组结构

print("数组类型为:",arr2.dtype)    ##查看数组类型

print("数组大小为:",arr2.size)     ##查看数组大小

print("数组每个元素大小为:",arr2.itemsize)   ##查看数组每个元素大小


输出结果:

数组维度为:2

数组形状为:(3,4)

数组类型为: int32

数组大小为:12

数组每个元素大小为:4








5. Reset the array shape attribute

arr2.shape = 4,3   ###重新设置arr2数组的shape

print("重置shape后的arr2为:\n",arr2)

输出结果:

[[ 1  2  3]
 [ 4  4  5]
 [ 6  7  7]
 [ 8  9 10]]

6. creating an array with arange function (by setting the start value, the final value, the step size to create a one-dimensional array)

import numpy as np

## 参数1:开始  包含
## 参数2  截止  不包含
## 参数3  步长

arr1 = np.arange(1,10,2)

print("使用arange函数创建的数组为:\n",arr1)


输出结果:

使用arange函数创建的数组为:

[1 3 5 7 9]

7. linspace function used to create an array (set start value, final value, number of elements)

import numpy as np

# ## 参数1 开始 ,包含
# ## 参数2 截止 ,包含
# ## 参数3 元素个数

arr1 = np.linspace(0,1,6)

print("使用linspace创建的数组为:\n",arr1)


输出结果为:

使用linspace创建的数组为:

[0.  0.2 0.4 0.6 0.8 1. ]

8. Create a geometric sequence logspace function using (generated (10 ^ 2 to 10 ^ 0) 20 geometric series)

import numpy as np

###参数1  以10的开始次方  开始,包含
###参数2  以10的结束次方  结束,包含
###参数3  生成数组个数

arr1 = np.logspace(0,2,20)

print("使用logspace函数创建的数组为:\n",arr1)


输出结果:

使用logspace函数创建的数组为:

[  1.           1.27427499   1.62377674   2.06913808   2.6366509
   3.35981829   4.2813324    5.45559478   6.95192796   8.8586679
  11.28837892  14.38449888  18.32980711  23.35721469  29.76351442
  37.92690191  48.32930239  61.58482111  78.47599704 100.        ]

9. Create the array using zeros function

impory numpy as np

##参数  生成数组的shape

arr1 = np.zeros((2,3))

print("使用zeros函数创建的数组为:\n",arr1)


输出结果:

使用zeros函数创建的数组为:

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

10. Create an array using the eye function

import numpy as np

###参数 生成对角都为1的数,里面参数是一个数,表示生成数组为几行几列

arr1 = np.eye(3)

print("使用eye函数创建的数组为:\n",arr1)


输出结果:

使用eye函数创建的数组为:

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

11. Create an array using the diag function

import numpy as np

## 生成指定对角元素的对角数组,传入参数多大就生成几行几列

arr1 = np.diag([1,2,3,4])

print("使用diag创建的数组为:\n",arr1)



输出结果:

使用diag创建的数组为:

[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]

12. Create an array using the ones function: to create elements of 1

import numpy as np

##参数 生成数组的shape  全部元素都为1

arr1 = np.ones((3,5))

print("使用ones函数创建的数组为:\n",arr1)



输出结果:

使用ones函数创建的数组为:

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

13. random function generates a random number

import numpy as np

###生成指定参数个0~1 之间的数组  包含0  不包含1

arr1 = np.random.random(4)

print("arr1:\n",arr1)


输出结果:

arr1:

[0.09890543 0.5631192  0.09417896 0.1554208 ]

##################################################################

###生成均匀分布的数组,里面参数为几行几列
arr2 = np.random.rand(6,2)

print("arr2:\n",arr2)



输出结果:

arr2:

[[0.60073526 0.40737395]
 [0.49359336 0.48587344]
 [0.58353272 0.7918001 ]
 [0.06910529 0.6781642 ]
 [0.47421911 0.51124696]
 [0.75780799 0.56415203]]

#################################################

####符合正太分布的数组
## u 均值     反映的正太分布对称线的位置
## a 标准差   反映数据的离散程度
## a 越大,越离散,图形越平缓
## a 越小,越密集,图形越陡峭

arr3 = np.random.randn((10,2))

print("arr3:\n",arr3)


输出结果:

arr3:

[[ 0.47601861 -0.53283289]
 [ 0.36274619 -0.27527158]
 [-0.00463033 -0.55391689]
 [-1.6325087  -1.56417882]
 [-1.25892241 -0.32421935]
 [ 1.09829841  0.23137011]
 [-0.35076178  0.82404005]
 [ 0.87361673  1.64870962]
 [ 0.21036936  1.09305065]
 [ 1.92894355 -1.80894203]]

#################################################

arr4 = np.random.randint(2,10,size=[3,4])

## random.randint(a, b),用于生成一个指定范围内的整数。
## 其中参数 a 是下限,参数 b 是上限,生成的随机数 n: a <= n <= b

print("arr4:\n",arr4)

输出结果:

arr4:

[[7 3 2 9]
 [4 8 4 2]
 [4 7 3 6]]



14. The array data type conversion

print(np.float64(42))   ## 整型转换为浮点型

print(np.int8(42.0))    ## 浮点型转换为整型

print(np.bool(42))      ## 整型转换为布尔型

print(np.bool(0))

print(np.float(True))   ## 布尔型转换为浮点型

print(np.int(False))    ## 布尔型转换为整型



输出结果:

42.0
42
True
False
1.0
0

15. A custom data types

import numpy as np

df = np.dtype([("name",np.str_,32),("weight",np.float64),("height",np.float64)])

arr1 = np.array([("zs",180,75),("ls",170,60),("ww",185,80)],dtype=df)

print(arr1)

print(arr1.dtype)



输出结果:

[('zs', 180., 75.) ('ls', 170., 60.) ('ww', 185., 80.)]
[('name', '<U32'), ('weight', '<f8'), ('height', '<f8')]

16. The array index

### 一维数组的索引
##创建一个一维数组

arr = np.arange(10)
print("arr:",arr)

### 一维索引
##取5
print(arr[5])
##取5,6,7
print(arr[5:8])



##多维数组的索引
arr = np.array([[1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7]])
print(arr)
print("取前两行所有列",arr[:2,:])
print("取第三行的4,5",arr[2,1:3])
print("取第一行,第三行的的第一列和最后一列",arr[::2,::3])

17. The array changing shape

arr1 = np.arange(10)     ## 创建一维数组

print(arr1)


输出结果:

[0 1 2 3 4 5 6 7 8 9]

#############################################

##改变数组形状

arr_new = arr1.reshape(2,5)

print(arr_new)


输出结果:


[[0 1 2 3 4]
 [5 6 7 8 9]]

18. Use ravel function f and the function flattened array latten

arr1 = np.arange(9).reshape(3,3)

print(arr1)


输出结果:

[[0 1 2]
 [3 4 5]
 [6 7 8]]



arr_new = arr1.reval()

print("展平的数组结果为:\n"arr_new)


输出结果:

展平的数组结果为:

[0 1 2 3 4 5 6 7 8]

########################################################

### 通过flatten展平数组
### 默认参数为C,按行展开,F为按列展开

arr_new = arr.flatten("F")

print(arr_new)

输出结果:

[0 1 2 3 4 5 6 7 8]

19. The composition Array

import numpy as np

arr1 = np.arange(6).reshape(2,3)

arr2 = np.array([[1,2,3],[4,5,6]])

print('arr1:\n',arr1)
print('arr2:\n',arr2)


输出结果:

arr1: 

[[0 1 2]
 [3 4 5]]

arr2: 

[[1 2 3]
 [4 5 6]]

#######################################

### hstack 数组横向拼接,增加列

arr_new = np.hstack((arr1,arr2))

print('arr_new:\n',arr_new)

输出结果:

arr_new:

[[0 1 2 1 2 3]
 [3 4 5 4 5 6]]


##########################################

###vstack 数组纵向拼接,增加行

arr_new = np.vstack((arr1,arr2))

print("arr_new:\n",arr_new)

输出结果:

arr_new:

[[0 1 2]
 [3 4 5]
 [1 2 3]
 [4 5 6]]

############################################

####concatenate  axis=1  数组横向组合

arr_new = np.concatenate((arr1,arr2),axis=1)


print(arr_new)


输出结果:

[[0 1 2 1 2 3]
 [3 4 5 4 5 6]]

###############################################

### concatenate ,axis=0   数组纵向组合 

# arr_new = np.concatenate((arr1,arr2),axis=0)

# print(arr_new)

输出结果:

[[0 1 2]
 [3 4 5]
 [1 2 3]
 [4 5 6]]


### 拼接过程中:维度
## 横向拼接,行一致一致
## 纵向拼接,列一致

20. Cutting arrays

import numpy as np

arr = np.arange(16).reshape(4,4)

print("arr:\n",arr)

输出结果:

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


#### hsplit 横向拆分

arr_new = np.hsplit(arr,2)

##或者

arr_new = np.split(arr,2,axis=1)

print(arr_new)

输出结果:

[array([[ 0,  1],
       [ 4,  5],
       [ 8,  9],
       [12, 13]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11],
       [14, 15]])]


#### vsplit 纵向拆分

arr_new = np.hsplit(arr,2)

### 或者

arr_new = np.split(arr,2,axis=0)

print(arr_new)

输出结果:

[array([[0, 1, 2, 3],
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]])]

 

Guess you like

Origin blog.csdn.net/weixin_43567965/article/details/91885508