python学习笔记10 数组

版权声明: https://blog.csdn.net/qq_40025335/article/details/81607562
#数组操作及深度数据分析
import numpy as np
from math import pi
import cv2

# 用np初始化数组,数组维数、数据类型,长度
'''
ndarray.ndim:数组的维数 
ndarray.shape:数组每一维的大小 
ndarray.size:数组中全部元素的数量 
ndarray.dtype:数组中元素的类型(numpy.int32, numpy.int16, and numpy.float64等) 
ndarray.itemsize:每个元素占几个字节
'''
a = np.arange(15).reshape(3, 5)
print("np多维数组a:")
print("数组元素:", a)
print("数组的维数?:",a.ndim)
print("数组每一维的大小: ", a.shape)
print("数组中元素的类型:", a.dtype.name)
print("每个元素占几个字节:", a.itemsize)
print("数组袁术个数:", a.size)
print("TYpe:", type(a))
print(" ")

b = np.array([2,3,4])
print("np一维数组b:")
print("数组元素:", b)
print("数组的维数: ",   b.ndim)
print("数组每一维的大小: ", b.shape)
print("数组中元素的类型:", b.dtype.name)
print("每个元素占几个字节:", b.itemsize)
print("数组袁术个数:",   b.size)
print("TYpe:", type(b))
print(" ")

c = np.array([(1.5,2,3), (4,5,6)])
print("np多维数组c:")
print("数组元素:", c)
print("数组的维数: ",   c.ndim)
print("数组每一维的大小: ", c.shape)
print("数组中元素的类型:", c.dtype.name)
print("每个元素占几个字节:", c.itemsize)
print("数组袁术个数:",   c.size)
print("TYpe:", type(c))
print(" ")

d = np.array([[1, 2], [3, 4]], dtype=complex)
print("生成数组的同时指定类型d:")
print("数组元素:", d)
print("数组的维数: ", d.ndim)
print("数组每一维的大小: ", d.shape)
print("数组中元素的类型:", d.dtype.name)
print("每个元素占几个字节:", d.itemsize)
print("数组袁术个数:",   d.size)
print("TYpe:", type(d))
print(" ")

"""
生成数组并赋为特殊值: 
ones:全1 
zeros:全0 
empty:随机数,取决于内存情况
"""
print("生成数组并赋为特殊值efg:")
e = np.zeros( (3,4) )
f = np.ones( (2,3,4), dtype=np.int16 )            # dtype can also be specified
g = np.empty( (2,3) )                              # uninitialized, output may vary
print("数组元素e:", e)
print("数组元素f:", f)
print("数组元素g:", g)

print("数组的维数g: ", g.ndim)
print("数组每一维的大小g: ", g.shape)
print("数组中元素的类型g:", g.dtype.name)
print("每个元素占几个字节g:", g.itemsize)
print("数组袁术个数g:",  g.size)
print("TYpe_g:", type(g))
print(" ")
"""
生成均匀分布的array: 
arange(最小值,最大值,步长)(左闭右开) 
linspace(最小值,最大值,元素数量)
"""
print("生成均匀分布的array_h:")
h = np.arange( 10, 30, 5 )
print("h:", h)
k = np.arange( 0, 2, 0.3 )                 # it accepts float arguments
l =  np.linspace( 0, 2, 9 )                 # 9 numbers from 0 to 2
m = np.linspace( 0, 2*pi, 100 )        # useful to evaluate function at lots of points

print("k:", k)
print("l:", l)
print("m:", m)
print(" ")

"""
整个array按顺序参与运算:
"""
print("整个array按顺序参与运算:")
n = np.array( [20,30,40,50] )
o = np.arange( 4 )
n1 = n - o
# n2 = n1 *2
n2 = n1 **2
n3 = 10*np.sin(n)
n4 = n < 35
print("n:", n)
print("o:", o)
print("n1:", n1)
print("n2:", n2)
print("n3:", n3)
print("n4:", n4)
print(" ")

print("两个二维使用*符号仍然是按位置一对一相乘,如果想表示矩阵乘法,使用dot:")
A = np.array( [[1,1], [0,1]] )
B = np.array([[2, 0], [3,4]] )
C =  A*B                         # elementwise product
A.dot(B)
np.dot(A, B)
print("A:", A)
print("B:", B)
print("C:", C)
print("A.dot(B):", A.dot(B))   #???
print("np.dot(A, B) :", np.dot(A, B))   #??
print(" ")

print("内置函数(min,max,sum),同时可以使用axis指定对哪一维进行操作:")
D= np.arange(12).reshape(3,4)
# B = np.array([[2, 0], [3,4]] )
# C =  A*B                         # elementwise product
# A.dot(B)
# np.dot(A, B)
# D0 = D[0:2:1, 0:2:1, 0:2]
# print("D0:", D0)
print("D:", D)
print(" D.sum(axis=0) :",  D.sum(axis=0) )#按行累和
print("D.min(axis=1):", D.min(axis=1))  #去第一列
print("D.cumsum(axis=1) :", D.cumsum(axis=1))  #按列雷和
# print("D:", D)
# print("np.dot(A, B) :", np.dot(A, B))   #??
print(" ")

print("Numpy同时提供很多全局函数:")
D1 = np.arange(3)
print("D1:", D1)
print("np.exp(D1):", np.exp(D1))
print("np.sqrt(D1):", np.sqrt(D1))
D2 = np.array([2., -1., 4.])
print("np.add(D1, D2):", np.add(D1, D2))
print(" ")
data = np.array([255, 256, 1500, 888, 999, 256, 256])
print(data.shape)
print(data.dtype)
data = data /5.88
data1 = data.astype(np.uint8)
print(data1)
print(data1.dtype)
# cv2.imshow("data1", data1)
# cv2.waitKey()
data2=np.array([
    [1500,1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500,1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500,1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500],
 [1500, 1500, 1500,  366, 889, 1500, 1500, 1500,  584, 1500, 1500, 1500, 1500, 1500,627, 1500,  274, 1500,  271,  274,  280, 1500, 1500,  622,  535, 1500, 1500, 1500,1500,  728,  677, 1500,  715, 1500,  407,  373,  338,  343, 1500,  311, 1500, 1500],
])
print(data2.shape)
print(data2.dtype)
data2 = data2 /5.88
data2 = data2.astype(np.uint8)
print(data2)
data3 = data2[:, 4] #将第五列的值输出
print("data3:",data3)
data4 =data2[:, 0:4]
print("data4:",data4)
# cv2.imshow("data2", data2)
# cv2.waitKey()

-----------------------------------------------------------------------------------------------------

G:\Python\testproject1\venv1\Scripts\python.exe G:/Python/testproject1/venv1/pyMutilProcess/数组操作.py
np多维数组a:
数组元素: [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
数组的维数?: 2
数组每一维的大小:  (3, 5)
数组中元素的类型: int32
每个元素占几个字节: 4
数组袁术个数: 15
TYpe: <class 'numpy.ndarray'>
 
np一维数组b:
数组元素: [2 3 4]
数组的维数:  1
数组每一维的大小:  (3,)
数组中元素的类型: int32
每个元素占几个字节: 4
数组袁术个数: 3
TYpe: <class 'numpy.ndarray'>
 
np多维数组c:
数组元素: [[1.5 2.  3. ]
 [4.  5.  6. ]]
数组的维数:  2
数组每一维的大小:  (2, 3)
数组中元素的类型: float64
每个元素占几个字节: 8
数组袁术个数: 6
TYpe: <class 'numpy.ndarray'>
 
生成数组的同时指定类型d:
数组元素: [[1.+0.j 2.+0.j]
 [3.+0.j 4.+0.j]]
数组的维数:  2
数组每一维的大小:  (2, 2)
数组中元素的类型: complex128
每个元素占几个字节: 16
数组袁术个数: 4
TYpe: <class 'numpy.ndarray'>
 
生成数组并赋为特殊值efg:
数组元素e: [[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
数组元素f: [[[1 1 1 1]
  [1 1 1 1]
  [1 1 1 1]]

 [[1 1 1 1]
  [1 1 1 1]
  [1 1 1 1]]]
数组元素g: [[1.5 2.  3. ]
 [4.  5.  6. ]]
数组的维数g:  2
数组每一维的大小g:  (2, 3)
数组中元素的类型g: float64
每个元素占几个字节g: 8
数组袁术个数g: 6
TYpe_g: <class 'numpy.ndarray'>
 
生成均匀分布的array_h:
h: [10 15 20 25]
k: [0.  0.3 0.6 0.9 1.2 1.5 1.8]
l: [0.   0.25 0.5  0.75 1.   1.25 1.5  1.75 2.  ]
m: [0.         0.06346652 0.12693304 0.19039955 0.25386607 0.31733259
 0.38079911 0.44426563 0.50773215 0.57119866 0.63466518 0.6981317
 0.76159822 0.82506474 0.88853126 0.95199777 1.01546429 1.07893081
 1.14239733 1.20586385 1.26933037 1.33279688 1.3962634  1.45972992
 1.52319644 1.58666296 1.65012947 1.71359599 1.77706251 1.84052903
 1.90399555 1.96746207 2.03092858 2.0943951  2.15786162 2.22132814
 2.28479466 2.34826118 2.41172769 2.47519421 2.53866073 2.60212725
 2.66559377 2.72906028 2.7925268  2.85599332 2.91945984 2.98292636
 3.04639288 3.10985939 3.17332591 3.23679243 3.30025895 3.36372547
 3.42719199 3.4906585  3.55412502 3.61759154 3.68105806 3.74452458
 3.8079911  3.87145761 3.93492413 3.99839065 4.06185717 4.12532369
 4.1887902  4.25225672 4.31572324 4.37918976 4.44265628 4.5061228
 4.56958931 4.63305583 4.69652235 4.75998887 4.82345539 4.88692191
 4.95038842 5.01385494 5.07732146 5.14078798 5.2042545  5.26772102
 5.33118753 5.39465405 5.45812057 5.52158709 5.58505361 5.64852012
 5.71198664 5.77545316 5.83891968 5.9023862  5.96585272 6.02931923
 6.09278575 6.15625227 6.21971879 6.28318531]
 
整个array按顺序参与运算:
n: [20 30 40 50]
o: [0 1 2 3]
n1: [20 29 38 47]
n2: [ 400  841 1444 2209]
n3: [ 9.12945251 -9.88031624  7.4511316  -2.62374854]
n4: [ True  True False False]
 
两个二维使用*符号仍然是按位置一对一相乘,如果想表示矩阵乘法,使用dot:
A: [[1 1]
 [0 1]]
B: [[2 0]
 [3 4]]
C: [[2 0]
 [0 4]]
A.dot(B): [[5 4]
 [3 4]]
np.dot(A, B) : [[5 4]
 [3 4]]
 
内置函数(min,max,sum),同时可以使用axis指定对哪一维进行操作:
D: [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
 D.sum(axis=0) : [12 15 18 21]
D.min(axis=1): [0 4 8]
D.cumsum(axis=1) : [[ 0  1  3  6]
 [ 4  9 15 22]
 [ 8 17 27 38]]
 
Numpy同时提供很多全局函数:
D1: [0 1 2]
np.exp(D1): [1.         2.71828183 7.3890561 ]
np.sqrt(D1): [0.         1.         1.41421356]
np.add(D1, D2): [2. 0. 6.]
 
(7,)
int32
[ 43  43 255 151 169  43  43]
uint8
(2, 42)
int32
[[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
  255 255 255 255 255 255]
 [255 255 255  62 151 255 255 255  99 255 255 255 255 255 106 255  46 255
   46  46  47 255 255 105  90 255 255 255 255 123 115 255 121 255  69  63
   57  58 255  52 255 255]]
data3: [255 151]
data4: [[255 255 255 255]
 [255 255 255  62]]

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/qq_40025335/article/details/81607562