ndarray数据创建及操作方法

--最近自己在B站上 照着学操作的记录,保留下来,方便后期随时复习及使用
--安装anaconda软件
--在spyder上执行程序代码
--pip install numpy 安装库

--在spydedr生成ndarray数据

#在Ipython中用的列表和元组生成ndarray数据
--列表
import numpy as np


a = np.array([1,2,3])

print(type(a))
<class 'numpy.ndarray'>

print(a)
[1 2 3]
#通过函数arange函数生成一个ndarray数组
b=np.arange(5)
print(b)
[0 1 2 3 4]
#生成一个10*10的全1二维列表
c=np.ones((10,10))

print(c)
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
#将dtype类型改为int32
c=np.ones((10,10),dtype=np.int32)

print(c)
[[1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]]
 #np.full
 d=np.full((5,5),'d')#生成一个5*5的数据,值都是‘d’

print(d)
[['d' 'd' 'd' 'd' 'd']
 ['d' 'd' 'd' 'd' 'd']
 ['d' 'd' 'd' 'd' 'd']
 ['d' 'd' 'd' 'd' 'd']
 ['d' 'd' 'd' 'd' 'd']]
 
 #np.eye
 
 
e=np.eye(5)#5*5且对角线为1,其他为0

print(e)
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
 
 
 注意:以上方法生成的类型为浮点数,如果用array生成的为整数类型,如果想要修改需要创建时候指定dtype
 
 ndarray属性
 e.shape#查看尺度,行列信息的
Out[44]: (5, 5)

e.ndim#轴的数量
Out[45]: 2#这个难道是去重的?我看视频理解的是5,现在 得到的结果是2

e.size#数组的个数
Out[46]: 25

e.dtype#数组的类型
Out[47]: dtype('float64')

e.itemsize#元素的大小,单位为字节
Out[48]: 8


#linspace,从1-100,间隔为10,,指定类型为int32,默认类型为浮点
f=np.linspace(1,100,10,dtype=np.int32)

f#直接f显示结果,不使用print
Out[56]: array([  1,  12,  23,  34,  45,  56,  67,  78,  89, 100])


#用于判断最后的数据100是不是,如果不是不输出
g=np.linspace(1,100,10,endpoint=False)

g
Out[58]: array([ 1. , 10.9, 20.8, 30.7, 40.6, 50.5, 60.4, 70.3, 80.2, 90.1])

#将两个数组合并
h=np.concatenate((f,g))

h
Out[60]: 
array([  1. ,  12. ,  23. ,  34. ,  45. ,  56. ,  67. ,  78. ,  89. ,
       100. ,   1. ,  10.9,  20.8,  30.7,  40.6,  50.5,  60.4,  70.3,
        80.2,  90.1])
		
#除了arange函数生成整数外,其他基本都是浮点数


#对数组进行维度转换
i=np.ones((2,3,4))

i
Out[62]: 
array([[[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]],

       [[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]]])
		
i.reshape((3,8))#只是临时转换,i没有真正改变,
Out[65]: 
array([[1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1.]])

i
Out[67]: 
array([[[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]],

       [[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]]])
	   
#改变维度时候,要保证新的维度个数与之前一致	   
i.reshape((2,4))
Traceback (most recent call last):

  File "<ipython-input-68-81b7285f9224>", line 1, in <module>
    i.reshape((2,4))

ValueError: cannot reshape array of size 24 into shape (2,4)
		
		
#将数组维度彻底改变
i.resize((3,8))

i
Out[71]: 
array([[1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1.]])
	   
	   
	 
#对数组进行临时维度变化为1
i.flatten()
Out[76]: 
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1.])
	   
	   
#将数组元素类型进行改变,float改为int
i.astype(np.int)
Out[78]: 
array([[1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1]])
	   
	   
数组转换为list
k=np.full((2,3,4),'mpp')

k
Out[82]: 
array([[['mpp', 'mpp', 'mpp', 'mpp'],
        ['mpp', 'mpp', 'mpp', 'mpp'],
        ['mpp', 'mpp', 'mpp', 'mpp']],

       [['mpp', 'mpp', 'mpp', 'mpp'],
        ['mpp', 'mpp', 'mpp', 'mpp'],
        ['mpp', 'mpp', 'mpp', 'mpp']]], dtype='<U3')

k.tolist()#转换为列表
Out[83]: 
[[['mpp', 'mpp', 'mpp', 'mpp'],
  ['mpp', 'mpp', 'mpp', 'mpp'],
  ['mpp', 'mpp', 'mpp', 'mpp']],
 [['mpp', 'mpp', 'mpp', 'mpp'],
  ['mpp', 'mpp', 'mpp', 'mpp'],
  ['mpp', 'mpp', 'mpp', 'mpp']]]
  
  
  #数组的操作
  #对于一纬数组与pylist操作一样
  #一行代码生成维度为2,3,4的数组
 
a=np.arange(24).reshape((2,3,4))


a
Out[101]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
		
#多维度进行取值
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

a[1,0,0]#取第1个维度,第0个维度的第0个值
Out[102]: 12		
		
#多维度切片
a[:,0:2,3:4]#[:,:,:]
Out[109]: 
array([[[ 3],
        [ 7]],

       [[15],
        [19]]])
		
#以2为步长进行切片
a[:,:,::2]
Out[110]: 
array([[[ 0,  2],
        [ 4,  6],
        [ 8, 10]],

       [[12, 14],
        [16, 18],
        [20, 22]]])
#维度越多,切片越复杂,特别注意


#数组的运算
a.mean()#数组的平均值
Out[113]: 11.5
#数组与一个标量进行运算,实际相当于所有元素都与标量值进行运算
a/2

Out[114]: 
array([[[ 0. ,  0.5,  1. ,  1.5],
        [ 2. ,  2.5,  3. ,  3.5],
        [ 4. ,  4.5,  5. ,  5.5]],

       [[ 6. ,  6.5,  7. ,  7.5],
        [ 8. ,  8.5,  9. ,  9.5],
        [10. , 10.5, 11. , 11.5]]])

#对一维元函数操作
a=np.arange(-24,0).reshape((2,3,4))

a
Out[125]: 
array([[[-24, -23, -22, -21],
        [-20, -19, -18, -17],
        [-16, -15, -14, -13]],

       [[-12, -11, -10,  -9],
        [ -8,  -7,  -6,  -5],
        [ -4,  -3,  -2,  -1]]])

np.abs(a)#求绝对值
Out[126]: 
array([[[24, 23, 22, 21],
        [20, 19, 18, 17],
        [16, 15, 14, 13]],

       [[12, 11, 10,  9],
        [ 8,  7,  6,  5],
        [ 4,  3,  2,  1]]])
		
#二元函数

a=np.arange(24).reshape((2,3,4))

a
Out[128]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

b=np.sqrt(a)#根据a数组开平方生成b数组

b
Out[130]: 
array([[[0.        , 1.        , 1.41421356, 1.73205081],
        [2.        , 2.23606798, 2.44948974, 2.64575131],
        [2.82842712, 3.        , 3.16227766, 3.31662479]],

       [[3.46410162, 3.60555128, 3.74165739, 3.87298335],
        [4.        , 4.12310563, 4.24264069, 4.35889894],
        [4.47213595, 4.58257569, 4.69041576, 4.79583152]]])

#两个数组求最大值
np.maximum(a,b)
Out[131]: 
array([[[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]],

       [[12., 13., 14., 15.],
        [16., 17., 18., 19.],
        [20., 21., 22., 23.]]])
		#返回的是浮点数
		
#两个数组进行比较
a>b
Out[132]: 
array([[[False, False,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]],

       [[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]]])#返回布尔类型

		
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值