ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python numpy的一些基本使用

2022-01-24 17:01:04  阅读:178  来源: 互联网

标签:基本 python array1 array4 array3 np array numpy 0.000


import numpy as np

'''the 1st part: the build of array'''

array1_1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
'''[[1 2 3]
 [4 5 6]
 [7 8 9]]
 '''
array1_2 = np.zeros([3, 3])
'''[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
 '''
array1_3 = np.ones([3, 3])
'''[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
 '''
array1_4 = np.empty([3, 3])
'''[[0.000e+000 0.000e+000 0.000e+000]
 [0.000e+000 0.000e+000 3.617e-321]
 [0.000e+000 0.000e+000 0.000e+000]]
 it is random
 '''
array1_5 = np.full([3, 3], 6)
'''[[6 6 6]
 [6 6 6]
 [6 6 6]]
'''
array1_6 = np.eye(3)
'''[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
 '''
array1_7 = np.arange(10, 20, 2)
'''[10 12 14 16 18]'''
array1_8 = np.linspace(10, 20, 5)
'''[10.  12.5 15.  17.5 20. ]'''
array1_9 = np.logspace(10, 20, 5)
'''[1.00000000e+10 3.16227766e+12 1.00000000e+15 3.16227766e+17
 1.00000000e+20]
'''
array1_10 = np.diag([3, 4, 5])
'''[[3 0 0]
 [0 4 0]
 [0 0 5]]
'''
array1_11 = np.tri(3)
'''[[1. 0. 0.]
 [1. 1. 0.]
 [1. 1. 1.]]
'''
array1_12 = np.vander([1, 2, 3])
'''[[1 1 1]
 [4 2 1]
 [9 3 1]]
'''


'''the 2nd part: the property of array'''
array2_1 = np.vander([1, 2, 3])
'''
[[1 1 1]
 [4 2 1]
 [9 3 1]]
'''
s1 = array2_1.shape
'''(3, 3)'''
s2 = array2_1.size
'''9'''
s3 = array2_1.T
'''
[[1 4 9]
 [1 2 3]
 [1 1 1]]
'''
s4 = array2_1.real
'''[[1 1 1]
 [4 2 1]
 [9 3 1]]
 the real part of array
'''
s5 = array2_1.imag
'''[[0 0 0]
 [0 0 0]
 [0 0 0]]
the imaginary part of array
'''


'''the 3rd part:the operation of array'''
'''array.copy'''
array3_1 = np.vander([1, 2, 3])
cc = array3_1
#cc[0, 0] = 0
'''
array3_1:
[[0 1 1]
 [4 2 1]
 [9 3 1]] 
 cc:
 [[0 1 1]
 [4 2 1]
 [9 3 1]]
 It means that the content of the original array will be changed with the change in cc while you use "=".
 We can use another method if you won't want to change the original array
 '''
cc1 = array3_1.copy()
cc1[0, 0] = 0
'''
array:
[[1 1 1]
 [4 2 1]
 [9 3 1]] 
 cc1:
 [[0 1 1]
 [4 2 1]
 [9 3 1]]'''

cc2 = array3_1.reshape(1, 9)
'''array:
[[1 1 1]
 [4 2 1]
 [9 3 1]] 
cc2:
[[0 1 1 4 2 1 9 3 1]]'''
cc2.resize(3, 3)
'''
[[1 1 1]
 [4 2 1]
 [9 3 1]]'''
d_1 = cc.flatten()
''' [1 1 1 4 2 1 9 3 1]'''
d_2 = cc2.max()
'''9'''


'''the fourth part: indexes of array'''
f_1 = array3_1[1:3, 1:3]
'''
 [[2 1]
 [3 1]]
'''
f_2 = array3_1[1:3, 1:3][0, 0]
'''2'''
f_3 = array3_1[[1, 2], [0, 1]]
'''
 [4 3]
 [1,2] is the row
 [0,1] is the list
 So (1,0) is 4 and (2,1) is 3
'''
f_4 = array3_1[np.ix_([1, 2], [0, 1])]
'''
[[4 2]
 [9 3]]
'''
for i in array3_1:
    print(i)
'''
[1 1 1]
[4 2 1]
[9 3 1]
'''
for i in np.nditer(array3_1):
    print(i)
'''
1
1
1
4
2
1
9
3
1
'''

'''the fifth part: the separation and combination'''
array4_1 = np.vander([1, 2, 3])
array4_2 = np.eye(3)
gg_1 = np.vstack([array4_1, array4_2])
'''
 [[1. 1. 1.]
 [4. 2. 1.]
 [9. 3. 1.]
 [1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
'''
gg_2 = np.hstack([array4_1, array4_2])
'''
 [[1. 1. 1. 1. 0. 0.]
 [4. 2. 1. 0. 1. 0.]
 [9. 3. 1. 0. 0. 1.]]

'''
gg_3 = np.stack([array4_1, array4_2])
'''
 [[[1. 1. 1.]
  [4. 2. 1.]
  [9. 3. 1.]]

 [[1. 0. 0.]
  [0. 1. 0.]
  [0. 0. 1.]]]
'''
array4_3 = np.vander([1, 2, 3, 4])

np.vsplit(array4_3, 2)
np.hsplit(array4_3, 2)
np.split(array4_3, 2)
print(array4_3)


标签:基本,python,array1,array4,array3,np,array,numpy,0.000
来源: https://blog.csdn.net/weixin_45696825/article/details/122670789

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有