numpy学习专题
二、ndarray各种数组
ndarray数组
import numpy as np
dt = np.array([1,2,3,4,5])
print(dt)
print(type(dt))
[1 2 3 4 5]
元素类型要相同
import numpy as np
arr =np.array([1,2,3,4,5.5,6,7,8])
print(arr)
print(type(arr))
[1. 2. 3. 4. 5.5 6. 7. 8. ]
多维数组
import numpy as np
arr = np.array([[1,2,3],[1,2,3]])
print(arr)
[[1 2 3]
[1 2 3]]
import numpy as np
arr = np.array([1,2,3,4,5,6,7,8,0],ndmin=5)
print(arr)
[[[[[1 2 3 4 5 6 7 8 0]]]]]
dtype 参数
import numpy as np
arr = np.array([1,2,3,4,5,6] , dtype = float)
print(arr)
[1. 2. 3. 4. 5. 6.]
结构化数据类型
import numpy as np
student = np.dtype([("names","S20"),("age","i4"),("marks","f")])
arr = np.array([("ZJM",19,2.0),("ZLJ",43,1.0)],dtype = student)
print(arr)
[(b'ZJM', 19, 2.) (b'ZLJ', 43, 1.)]
asarray函数 – 与array功能类似
import numpy as np
arr = np.asarray(([1,2,3],[4,5,6]))
print(arr)
[[1 2 3]
[4 5 6]]
import numpy as np
arr = np.asarray([(1,2,3),(4,5)])
print(arr)
[(1, 2, 3) (4, 5)]
import numpy as np
arr = np.asarray([(1,2,3,2,3,5)],dtype = float)
arr
array([[1., 2., 3., 2., 3., 5.]])
empty函数 – 所有元素随机数来填充
import numpy as np
arr = np.empty([3,2],dtype = int)
print(arr)
[[1 2]
[3 4]
[5 6]]
zeros函数 – 所有元素用0来填充
import numpy as np
arr = np.zeros([2,3],dtype = int)
print(arr)
[[0 0 0]
[0 0 0]]
import numpy as np
arr = np.zeros([3,4],dtype = [("x",float),("y","S1")])
print(arr)
[[(0., b'') (0., b'') (0., b'') (0., b'')]
[(0., b'') (0., b'') (0., b'') (0., b'')]
[(0., b'') (0., b'') (0., b'') (0., b'')]]
ones 函数 – 所有元素用1来填充
import numpy as np
arr = np.ones([3,4],dtype = [("X",float),("Y","S20")])
print(arr)
[[(1., b'1') (1., b'1') (1., b'1') (1., b'1')]
[(1., b'1') (1., b'1') (1., b'1') (1., b'1')]
[(1., b'1') (1., b'1') (1., b'1') (1., b'1')]]
full函数 – 指定value来填充
import numpy as np
arr = np.full([5,6],fill_value = 3.14,dtype = float)
print(arr)
[[3.14 3.14 3.14 3.14 3.14 3.14]
[3.14 3.14 3.14 3.14 3.14 3.14]
[3.14 3.14 3.14 3.14 3.14 3.14]
[3.14 3.14 3.14 3.14 3.14 3.14]
[3.14 3.14 3.14 3.14 3.14 3.14]]
eye函数 – 对角线全部为1,其他地方全部为0
import numpy as np
arr = np.eye(10,dtype="i4")
print(arr)
[[1 0 0 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0 0 0]
[0 0 0 1 0 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0 0]
[0 0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 0 1 0 0 0]
[0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 0 0 1]]
arange函数 – 在一个范围内,按照规定的步长进行输出
import numpy as np
arr = np.arange(10,20,3,dtype = float)
print(arr)
[10. 13. 16. 19.]
frombuffer函数 – 依次输出字符串
import numpy as np
arr = b"ZJM IS A HANDSOME BOYS"
str =np.frombuffer(arr,dtype = "i1")
print(str)
[90 74 77 32 73 83 32 65 32 72 65 78 68 83 79 77 69 32 66 79 89 83]
import numpy as np
arr = b"YOU ARE SO EXCELLENT"
str = np.frombuffer(arr,dtype = "S4")
print(str)
[b'YOU ' b'ARE ' b'SO E' b'XCEL' b'LENT']
import numpy as np
arr = b"ZJM IS A HANDSOME BOY"
str = np.frombuffer(arr,dtype = "S1",count = len(arr)-2 , offset = 0)
print(str)
[b'Z' b'J' b'M' b' ' b'I' b'S' b' ' b'A' b' ' b'H' b'A' b'N' b'D' b'S'
b'O' b'M' b'E' b' ' b'B']
fromiter函数
import numpy as np
x = [1,2,3,4,5]
z = iter(x)
print(z)
print(type(z))
arr = np.fromiter (z,dtype='f')
print(arr)
[1. 2. 3. 4. 5.]
linspace函数 – 等差输出数据
import numpy as np
arr = np.linspace(1,40,20,True,True,int)
print(arr)
(array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33,
35, 37, 40]), 2.0526315789473686)
logspace函数 – 等比输出数据
import numpy as np
arr = np.logspace(1,4,20,float)
print(arr)
[ 10. 14.38449888 20.69138081 29.76351442
42.81332399 61.58482111 88.58667904 127.42749857
183.29807108 263.66508987 379.26901907 545.55947812
784.75997035 1128.83789168 1623.77673919 2335.72146909
3359.81828628 4832.93023857 6951.92796178 10000. ]
一定要支持作者哦