python基础 - Numpy

1 基本操作

1.1数组创建

import numpy as np # Shift + Enter
# 创建可以将Python,中list列表转换成NumPy数组
l = [1,2,3,4,5]

# NumPy数组
nd1 = np.array(l) # 输入一部分arr + tab(命令中自动补全,按键) 代码提示,自动补全
print(nd1)
display(nd1) # 显示
[1 2 3 4 5]

array([1, 2, 3, 4, 5])
nd2 = np.zeros(shape = (3,4),dtype = np.int16) # shift + tab提示方法的属性,使用
nd2
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int16)
nd3 = np.ones(shape = (3,5),dtype=np.float32)
nd3 # juppyter中执行程序,代码,最后一行,默认就是输出
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]], dtype=float32)
# 三维数组
nd4 = np.full(shape = (3,4,5),fill_value=3.1415926) # 生成任意指定的数组
nd4
array([[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],

       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],

       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]]])
nd5 = np.random.randint(0,100,size = 20) # 从0,到100,生成随机数字,int,整数
nd5
array([88, 68, 55, 25, 95, 45, 53, 52, 94, 48, 38, 41, 12, 66, 18, 38, 15,
       53, 15, 86])
nd6 = np.random.rand(3,5) # 生成0~1之间随机数
nd6
array([[0.1805577 , 0.97917756, 0.09591097, 0.07785931, 0.48850737],
       [0.89135262, 0.37393773, 0.30382903, 0.75894887, 0.27816675],
       [0.75702185, 0.22346079, 0.68415817, 0.40806391, 0.01608321]])
nd7 = np.random.randn(3,5) # 正态分布,平均值是0,标准差是1
display(nd7)
array([[ 0.45444319,  0.91608409, -0.32166309, -1.03094546, -0.46488153],
       [-1.31852167,  0.13591764,  0.7220875 ,  1.40014984,  0.18295841],
       [-0.22589557, -0.94451934,  1.25952318, -1.72418753,  0.65093392]])
nd8 = np.random.normal(loc = 175,scale = 10,size = (3,5)) # 正态分布,平均值是175,标准差是10
print(nd8)
[[158.01782738 178.97018664 180.98631405 172.14661692 175.47228488]
 [174.02104608 182.14529853 146.28615046 183.56698055 173.92432236]
 [185.93646469 180.11427498 188.36019552 183.88039966 182.85760538]]
nd9 = np.arange(1,100,step = 10) # 等差数列,左闭右开,100取不到
nd9
array([ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91])
nd10 = np.linspace(1,100,num = 19) # 等差数列,左闭右闭,num表示生成等差数列长度
nd10
array([  1. ,   6.5,  12. ,  17.5,  23. ,  28.5,  34. ,  39.5,  45. ,
        50.5,  56. ,  61.5,  67. ,  72.5,  78. ,  83.5,  89. ,  94.5,
       100. ])

查看数组属性

import numpy as np

nd = np.random.randn(5,3)
nd
array([[ 0.12642702,  0.74807666,  2.42032545],
       [-0.27449595, -0.68078987,  0.27759778],
       [ 1.15223642, -0.63332573,  0.28521052],
       [ 0.39521598,  0.6202027 ,  0.90567908],
       [ 0.31221798, -0.19330004, -0.42587603]])
# 查看数组形状,返回了形状 shape = (5,3)
nd.shape
(5, 3)
nd.dtype # 告诉数组的数据类型 float64 位,一位占一个0或者一个1
dtype('float64')
nd.size # 尺寸,数组可以是多维的,请问,里面共有多少数据 3*5 = 15
15
nd.ndim # 数组维度 
2
nd.itemsize # 条目 尺寸长度 8 字节
# 数据类型是float64 64位 -----> 1个字节8位-----> 64/8 = 8 字节
8

文件读写

nd1 = np.random.randint(0,100,size = (3,5))
nd2 = np.random.randn(3,5)
display(nd1,nd2)
array([[22, 32, 86, 19, 24],
       [58, 80,  2, 38, 96],
       [82, 47, 28, 32, 96]])



array([[-0.04839046,  0.20762731,  1.70801699, -0.41054106,  1.33676572],
       [-1.25124635,  0.59099474, -0.74312281,  1.09247369, -0.29597477],
       [ 0.90854543,  0.96096231, -1.20553854,  0.9696578 , -0.12783873]])
np.save('./data',nd1) # 把一个数据存到文件中
np.load('./data.npy') # 默认添加npy后缀
array([[22, 32, 86, 19, 24],
       [58, 80,  2, 38, 96],
       [82, 47, 28, 32, 96]])
# 多个数据存到一个文件中
np.savez('./data.npz',a = nd1,abc = nd2) # 保存数据是起名:a,abc,称为key,自己命名
data = np.load('./data.npz')
data
<numpy.lib.npyio.NpzFile at 0x7fe3d0869910>
data['a'] # 单引号
array([[22, 32, 86, 19, 24],
       [58, 80,  2, 38, 96],
       [82, 47, 28, 32, 96]])
data['abc']
array([[-0.04839046,  0.20762731,  1.70801699, -0.41054106,  1.33676572],
       [-1.25124635,  0.59099474, -0.74312281,  1.09247369, -0.29597477],
       [ 0.90854543,  0.96096231, -1.20553854,  0.9696578 , -0.12783873]])
data['www'] # 没有保存,无法获取
---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-15-a7937d03f88c> in <module>
----> 1 data['www']


/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numpy/lib/npyio.py in __getitem__(self, key)
    257                 return self.zip.read(key)
    258         else:
--> 259             raise KeyError("%s is not a file in the archive" % key)
    260 
    261 


KeyError: 'www is not a file in the archive'
np.savez_compressed('./data2.npz',x = nd1,y = nd2)
np.load('./data2.npz')['x']
array([[22, 32, 86, 19, 24],
       [58, 80,  2, 38, 96],
       [82, 47, 28, 32, 96]])
np.savetxt(fname = './data.txt',# 文件名
           X = nd1, # 数据
           fmt='%0.2f', # 格式
           delimiter=',')# 分隔符
np.savetxt(fname = './data.cvs',# 文件名
           X = nd1, # 数据
           fmt='%d', # 格式
           delimiter=';')# 分隔符
np.loadtxt('./data.cvs',delimiter=';')
array([[22., 32., 86., 19., 24.],
       [58., 80.,  2., 38., 96.],
       [82., 47., 28., 32., 96.]])
np.loadtxt('./data.txt',delimiter=',')
array([[22., 32., 86., 19., 24.],
       [58., 80.,  2., 38., 96.],
       [82., 47., 28., 32., 96.]])

数据类型

# int8,int16,int32,int64,uint8无符号
# float16,float32,float64
# str字符串类型
# int8 表示 2**8个数字 256个 -128 ~ 127 有符号
# uint8 表示256个数字,无符号,表明只有正数:0 ~ 255

np.array([2,4,7],dtype = np.int8)
array([2, 4, 7], dtype=int8)
np.array([-3,-7,255,108,0,256],dtype = np.uint8)
array([253, 249, 255, 108,   0,   0], dtype=uint8)
np.random.randint(0,100,size = 10,dtype = 'int64')
array([59,  9, 31, 47, 90, 84, 89, 10, 85, 13])
nd = np.random.rand(10,2)
nd
array([[0.08697582, 0.01856716],
       [0.28368212, 0.01316251],
       [0.20035117, 0.6809994 ],
       [0.31315228, 0.40969755],
       [0.79286082, 0.24707183],
       [0.60774558, 0.32297674],
       [0.54115188, 0.89123677],
       [0.99941816, 0.15384657],
       [0.52934283, 0.61635984],
       [0.34174069, 0.30613281]])
nd.dtype
dtype('float64')
np.asarray(nd,dtype = 'float16')
array([[0.087  , 0.01857],
       [0.2837 , 0.01316],
       [0.2003 , 0.681  ],
       [0.3132 , 0.4097 ],
       [0.793  , 0.2471 ],
       [0.608  , 0.323  ],
       [0.541  , 0.891  ],
       [0.9995 , 0.1538 ],
       [0.5293 , 0.616  ],
       [0.3418 , 0.3062 ]], dtype=float16)
nd.astype(dtype = np.float16)
array([[0.087  , 0.01857],
       [0.2837 , 0.01316],
       [0.2003 , 0.681  ],
       [0.3132 , 0.4097 ],
       [0.793  , 0.2471 ],
       [0.608  , 0.323  ],
       [0.541  , 0.891  ],
       [0.9995 , 0.1538 ],
       [0.5293 , 0.616  ],
       [0.3418 , 0.3062 ]], dtype=float16)
nd = np.random.randn(1000,3) # 默认数据类型是float64
np.save('./data1',nd)
np.save('./data2',nd.astype('float16'))
nd2 = np.array(list('abcdefghi'))
nd2
array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], dtype='<U1')
nd2.dtype
dtype('<U1')

数组运算

基本运算

# 加减乘除指数幂运算
nd1 = np.random.randint(0,10,size = 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值