day01-numpy

numpy get started

导入numpy库,并查看numpy版本

%lsmagic
Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %colors  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %matplotlib  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.
import numpy as np

# 绘图的工具
import matplotlib.pyplot as plt
# 将绘图工具嵌入到代码行中
%matplotlib inline  #方便展示图片
nd = np.random.randint(0,150,size = 10)
#输出的两种方式
#print(nd)
#输出[十个随机数]
nd  #一维数据
array([ 41,  70,  18,  98,  25,   4, 108, 147,  83,  16])
# n个数据,dimession维度
type(nd)
numpy.ndarray
nd2 = np.random.randint(0,150,size = (10,2,3))
nd2  #二维数据
array([[[ 14,  67, 148],
        [110, 143,  97]],

       [[138,  81,  58],
        [132,  51,   4]],

       [[ 94,  20,  41],
        [ 78,  96,  66]],

       [[ 75, 134, 113],
        [113,  10,  51]],

       [[ 96,  14,  79],
        [  1,  91, 121]],

       [[ 14, 123,  58],
        [ 72,  59,  91]],

       [[ 12,  64,   8],
        [ 40,  36,  23]],

       [[ 32,  10, 144],
        [127, 133,  68]],

       [[ 52,  98,  96],
        [ 28,  19,  11]],

       [[140,  17,   9],
        [ 11, 146, 112]]])
cat = plt.imread('./cat.jpg') 
#导入当前目录下的cat.jpg图片 结果显示三维数组  计算机识别图片?
#[199, 119,  82]三原色   红绿蓝
cat
array([[[231, 186, 131],
        [232, 187, 132],
        [233, 188, 133],
        ...,
        [100,  54,  54],
        [ 92,  48,  47],
        [ 85,  43,  44]],

       [[232, 187, 132],
        [232, 187, 132],
        [233, 188, 133],
        ...,
        [100,  54,  54],
        [ 92,  48,  47],
        [ 84,  42,  43]],

       [[232, 187, 132],
        [233, 188, 133],
        [233, 188, 133],
        ...,
        [ 99,  53,  53],
        [ 91,  47,  46],
        [ 83,  41,  42]],

       ...,

       [[199, 119,  82],
        [199, 119,  82],
        [200, 120,  83],
        ...,
        [189,  99,  65],
        [187,  97,  63],
        [187,  97,  63]],

       [[199, 119,  82],
        [199, 119,  82],
        [199, 119,  82],
        ...,
        [188,  98,  64],
        [186,  96,  62],
        [188,  95,  62]],

       [[199, 119,  82],
        [199, 119,  82],
        [199, 119,  82],
        ...,
        [188,  98,  64],
        [188,  95,  62],
        [188,  95,  62]]], dtype=uint8)
type(cat)
numpy.ndarray
# 显示数组cat

plt.imshow(cat) #搜索引擎如何识别猫?
<matplotlib.image.AxesImage at 0x1807dab4f98>

这里写图片描述

一、创建ndarray

1. 使用np.array()由python list创建

参数为列表:
[1, 4, 2, 5, 3]

注意:
- numpy默认ndarray的所有元素的类型是相同的
- 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int

l = [-1,0,0,0,0]

nd = np.array(l)#为何转换?

# 展示 display==print
display(l,nd,type(l),type(nd))
[-1, 0, 0, 0, 0]



array([-1,  0,  0,  0,  0])



list



numpy.ndarray
sum(l)
25
l.sum()     #列表当中没方法而 ndarry提供了很多方法  转化后方便
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-15-85f1a040cf5e> in <module>()
----> 1 l.sum()


AttributeError: 'list' object has no attribute 'sum'
nd.sum()  #求和
25
nd.any()  #l = [0,0,0,0,0]时为False
True
nd = np.array([2,4,6,8,3])
nd
array([2, 4, 6, 8, 3])
# 最大值的索引
nd.argmax()
3
nd.argmin()  #最小值对应索引
0
sort = nd.argsort() #从小到大排序,输出对应索引 索引的排序
sort
array([0, 4, 1, 2, 3], dtype=int64)
nd[[0,4,1]]    #取出索引为041对应的值
array([2, 3, 4])
# sort作为参数      #按照上面索引的排序输出对应的值
nd[sort]
array([2, 3, 4, 6, 8])
nd
array([2, 4, 6, 8, 3])
cond1 = nd < 6
cond2 = nd >=4
display(cond1,cond2)
array([ True,  True, False, False,  True])



array([False,  True,  True,  True, False])
np.logical_and(cond1,cond2) #True & True合并 逻辑运算符and
array([False,  True, False, False, False])
# 把所有大于4的数据取出来
np.argwhere(np.logical_and(nd < 6,nd >=4)) #所有地方符合要求的值都取出来
array([[1]], dtype=int64)
# RGB 透明度 ARGB  RGBA      导入图片 输出为三维数组 
#0 纯黑 255 纯白 判断哪一个是透明度?
captcha = plt.imread('./captcha.jpg')
captcha
array([[[204, 213, 204, 255],
        [204, 170, 204, 255],
        [204, 213, 204, 255],
        ...,
        [204, 213, 204, 255],
        [204, 170, 204, 255],
        [204, 213, 204, 255]],

       [[204, 170, 204, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [204, 170, 204, 255]],

       [[153, 213, 153, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [204, 213, 204, 255]],

       ...,

       [[204, 213, 204, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [204, 170, 204, 255]],

       [[204, 170, 153, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [204, 213, 204, 255]],

       [[204, 213, 204, 255],
        [153, 170, 204, 255],
        [204, 170, 153, 255],
        ...,
        [204, 213, 204, 255],
        [204, 170, 153, 255],
        [153, 170, 204, 255]]], dtype=uint8)
#三维数组 前两维不切,第三维切最后一位  发现透明度位
plt.imshow(captcha[:,:,:3]) 
<matplotlib.image.AxesImage at 0x1807dc6c5c0>

png

captcha_alpha = captcha.copy()

captcha_alpha[:,:,3] = 10  #改变透明度
captcha_alpha
array([[[204, 213, 204,  10],
        [204, 170, 204,  10],
        [204, 213, 204,  10],
        ...,
        [204, 213, 204,  10],
        [204, 170, 204,  10],
        [204, 213, 204,  10]],

       [[204, 170, 204,  10],
        [255, 255, 255,  10],
        [255, 255, 255,  10],
        ...,
        [255, 255, 255,  10],
        [255, 255, 255,  10],
        [204, 170, 204,  10]],

       [[153, 213, 153,  10],
        [255, 255, 255,  10],
        [255, 255, 255,  10],
        ...,
        [255, 255, 255,  10],
        [255, 255, 255,  10],
        [204, 213, 204,  10]],

       ...,

       [[204, 213, 204,  10],
        [255, 255, 255,  10],
        [255, 255, 255,  10],
        ...,
        [255, 255, 255,  10],
        [255, 255, 255,  10],
        [204, 170, 204,  10]],

       [[204, 170, 153,  10],
        [255, 255, 255,  10],
        [255, 255, 255,  10],
        ...,
        [255, 255, 255,  10],
        [255, 255, 255,  10],
        [204, 213, 204,  10]],

       [[204, 213, 204,  10],
        [153, 170, 204,  10],
        [204, 170, 153,  10],
        ...,
        [204, 213, 204,  10],
        [204, 170, 153,  10],
        [153, 170, 204,  10]]], dtype=uint8)
plt.imshow(captcha_alpha)  #展示图片 验证透明度作用
<matplotlib.image.AxesImage at 0x1807dd10c18>

png

captcha = captcha[:,:,:3]  #四维数组变成三维 红绿蓝
captcha
array([[[204, 213, 204],
        [204, 170, 204],
        [204, 213, 204],
        ...,
        [204, 213, 204],
        [204, 170, 204],
        [204, 213, 204]],

       [[204, 170, 204],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [204, 170, 204]],

       [[153, 213, 153],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [204, 213, 204]],

       ...,

       [[204, 213, 204],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [204, 170, 204]],

       [[204, 170, 153],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [204, 213, 204]],

       [[204, 213, 204],
        [153, 170, 204],
        [204, 170, 153],
        ...,
        [204, 213, 204],
        [204, 170, 153],
        [153, 170, 204]]], dtype=uint8)
#将图片变成黑白 
captcha = captcha[:,:,:3]
 #mean平均 min 最小值
#最后一维min方法axis参数  轴 2是最后的那一维
#shift+tab 显示说明
captcha_gray = captcha.min(axis =2 )
# cmap=color map 灰色
plt.imshow(captcha_gray,cmap = 'gray')
<matplotlib.image.AxesImage at 0x1807dd67e80>

png

captcha_gray  #里面较大的数对应白 较小的数对应黑
array([[204, 170, 204, ..., 204, 170, 204],
       [170, 255, 255, ..., 255, 255, 170],
       [153, 255, 255, ..., 255, 255, 204],
       ...,
       [204, 255, 255, ..., 255, 255, 170],
       [153, 255, 255, ..., 255, 255, 204],
       [204, 153, 153, ..., 204, 153, 153]], dtype=uint8)
# captcha_gray这个数据是二维
captcha_gray = captcha_gray.reshape(-1)#将数据变为一维的
# 150数据比较白,大于150的全部设置为255 全白的
cond = np.argwhere(captcha_gray > 150)

captcha_gray[cond] = 255
#以上将数据变成一维的了用reshape(分辨率55X22)变成二维的  
#数据没有变只是形状发生了改变
#设为黑白
#显示可知,去除显示不清的杂质
plt.imshow(captcha_gray.reshape(22,55),cmap = 'gray')
<matplotlib.image.AxesImage at 0x1807ded5f98>

png

#argwhere 用于去除声音中的杂质 图片中的杂质......

2. 使用np的routines函数创建

包含以下常见创建方法:

1) np.ones(shape, dtype=None, order=’C’)

np.ones(shape = (5,5),dtype=float)
#shape 形状 dtype 数据类型 order排序看说明 #ones值为1
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.]])

2) np.zeros(shape, dtype=float, order=’C’)

# 维度 axis(轴) 0,1,2,3
np.zeros((5,7,6,8))
#三维 5行7列高6 #四维  5代表0 6代表1 7代表2  zeros值为0
array([[[[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        ...,

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]]],


       [[[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        ...,

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]]],


       [[[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        ...,

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]]],


       [[[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        ...,

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]]],


       [[[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        ...,

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]]]])

3) np.full(shape, fill_value, dtype=None, order=’C’)

np.full((3,4,5),fill_value=3.14) 
#3为数组 3行4列 高5
#值自定义
array([[[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],
        [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]]])

4) np.eye(N, M=None, k=0, dtype=float)
对角线为1其他的位置为0

# 单位矩阵:斜对角线是1其他都是0     np.eye(N = 5)
# 满秩矩阵

eye = np.eye(N = 5)
# 求解矩阵的秩
np.linalg.matrix_rank(eye)
# 如果一个矩阵是满秩矩阵----> 有唯一解————预测准确
# 机器学习,本质上也是在解方程,通过矩阵 非满秩矩阵有无数解 预测不准
5

5) np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

np.linspace(0,100,101) #0-100之间 分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.,  24.,  25.,  26.,  27.,  28.,  29.,  30.,  31.,  32.,
        33.,  34.,  35.,  36.,  37.,  38.,  39.,  40.,  41.,  42.,  43.,
        44.,  45.,  46.,  47.,  48.,  49.,  50.,  51.,  52.,  53.,  54.,
        55.,  56.,  57.,  58.,  59.,  60.,  61.,  62.,  63.,  64.,  65.,
        66.,  67.,  68.,  69.,  70.,  71.,  72.,  73.,  74.,  75.,  76.,
        77.,  78.,  79.,  80.,  81.,  82.,  83.,  84.,  85.,  86.,  87.,
        88.,  89.,  90.,  91.,  92.,  93.,  94.,  95.,  96.,  97.,  98.,
        99., 100.])
np.log10(0.001)   #默认自然底数e
-3.0
1.00000000e-03
0.001
 np.logspace(-3,2,5) #10的-3次方 到10的2次方之间 按log的形式划分5份
    #10的-3 -2 -1 0 1 2 次方
array([1.00000000e-03, 1.77827941e-02, 3.16227766e-01, 5.62341325e+00,
       1.00000000e+02])

6) np.arange([start, ]stop, [step, ]dtype=None)

np.arange(0,100,step = 3) #0-100 步进为3
array([ 0,  3,  6,  9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48,
       51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99])

7) np.random.randint(low, high=None, size=None, dtype=’l’)

c = np.random.randint(0,255,size = (22,55,3))
#随机生成一张图片 高度25 宽度55  3是红绿蓝
c
array([[[172, 112, 216],
        [ 99,  79,  41],
        [ 11, 176, 174],
        ...,
        [182, 147, 142],
        [ 62, 107,  88],
        [220, 120, 100]],

       [[231, 121,  93],
        [128,  49, 183],
        [ 40,  94, 145],
        ...,
        [  6, 217, 174],
        [ 99,  30,  40],
        [ 60,  30, 232]],

       [[149, 183, 173],
        [157, 211,  49],
        [160, 173, 139],
        ...,
        [191,  22,  10],
        [127,  17, 218],
        [  7, 121, 183]],

       ...,

       [[116, 239,   1],
        [141,   4,  14],
        [ 67,  36,  12],
        ...,
        [ 26, 159,  15],
        [157, 254,  47],
        [246, 192, 182]],

       [[247, 220, 184],
        [ 81, 138, 254],
        [ 90, 183, 125],
        ...,
        [234, 223,  36],
        [117, 178, 244],
        [218, 211,  80]],

       [[176,  58,  59],
        [187,  59,  31],
        [137, 234,  13],
        ...,
        [105, 179, 247],
        [ 89,  28, 148],
        [129, 214, 133]]])
plt.imshow(c)
<matplotlib.image.AxesImage at 0x1807eef79b0>

png

8) np.random.randn(d0, d1, …, dn)

标准正太分布

# Return a sample (or samples) from the "standard normal" distribution.
#np.random.randn(100) #一维正太分布  标准值为0 正的负的
#np.random.randn(100,2)#二维正太分布
np.random.randn(100,2,3)#三维正态分布
array([[[ 6.56809149e-01,  7.02077088e-01, -6.43966386e-01],
        [-1.37266623e+00,  4.14117057e-01,  7.16186222e-02]],

       [[ 6.12062996e-01,  6.97162547e-01, -2.30144804e-01],
        [-1.50394291e+00,  9.09970926e-01, -8.80496051e-01]],

       [[-8.51222832e-01,  4.21077487e-01,  2.48395960e+00],
        [ 9.62761562e-02, -1.42612567e+00,  1.08803844e-01]],

       [[ 1.68135665e+00, -1.62911164e+00, -9.03367232e-01],
        [-9.97181726e-01,  1.28426010e+00, -1.17073658e+00]],

       [[ 1.06997225e-01, -2.46102880e-01,  2.74754228e-01],
        [ 1.21885306e+00, -6.15925293e-01, -1.95508292e-01]],

       [[-1.06354874e-01, -7.60015868e-01, -2.44766679e-01],
        [-1.16619876e+00, -4.03723964e-01, -1.13193655e+00]],

       [[ 7.37778678e-01, -1.79986497e+00, -1.64768208e+00],
        [ 1.45657313e+00, -9.28204217e-01,  1.30840482e+00]],

       [[ 1.97503412e-01,  2.03036275e+00, -8.52872556e-02],
        [ 1.45140252e-03, -2.07150140e-02, -1.98472523e-01]],

       [[-8.11094101e-01, -8.91255421e-01,  1.48648839e+00],
        [ 2.57515432e-01,  1.14407316e-01,  1.51593800e+00]],

       [[ 1.49918942e+00,  1.29045987e+00,  1.73953893e+00],
        [-5.24601325e-01, -1.74646487e+00,  1.47754526e+00]],

       [[-1.15448414e-01, -1.23868030e+00, -1.31656300e-01],
        [-1.46364511e+00, -2.28399571e+00, -1.29718951e+00]],

       [[ 9.43753481e-01,  7.81204623e-01,  7.54279199e-01],
        [ 1.55217817e+00, -1.13086364e+00,  1.92313618e+00]],

       [[-9.40388253e-01, -4.28186669e-01, -1.11190779e-01],
        [ 4.29122075e-01, -1.43484438e+00, -2.06456443e+00]],

       [[ 8.66836601e-01,  7.19867525e-01, -9.05173650e-01],
        [ 2.19742079e-01,  6.16358623e-01,  1.13436250e+00]],

       [[-2.09731090e-01,  2.46172497e-01,  5.54125382e-01],
        [ 1.96048931e+00,  7.66958620e-02, -1.07506977e+00]],

       [[ 7.30938985e-01, -6.02845957e-01, -1.54686224e+00],
        [ 5.88492404e-02, -5.87277865e-01,  1.25137767e+00]],

       [[-1.57551271e+00,  2.00302387e-01, -9.05860234e-01],
        [-2.05678987e-01,  3.79643763e-01, -2.87441841e-01]],

       [[ 6.89598429e-01,  6.90356501e-01, -1.51389377e+00],
        [ 1.14978814e+00,  1.36574079e+00, -1.72003392e+00]],

       [[-3.02595517e+00, -1.92523960e-01, -6.48006729e-02],
        [-4.27428637e-01,  5.14801985e-01,  5.76667039e-01]],

       [[ 2.48818635e-01, -1.50578496e+00,  1.60662015e+00],
        [ 3.55511630e-01, -4.99367623e-01, -9.27712249e-03]],

       [[-3.51494885e-01, -6.88560784e-01, -1.93059292e+00],
        [ 1.24766012e+00, -3.54013719e-01, -2.06950195e+00]],

       [[ 1.14122340e+00,  2.49766690e-01,  6.04948753e-02],
        [ 8.68903046e-01, -1.35094329e+00, -3.11667017e-01]],

       [[ 9.94905490e-01, -4.21378244e-01,  4.19511534e-01],
        [ 3.57671113e-01,  1.85956419e+00, -8.44496255e-01]],

       [[ 5.02812164e-01, -1.59582847e+00, -2.18422431e+00],
        [ 2.33080263e+00, -6.65037601e-02, -4.04555348e-01]],

       [[-6.76538363e-01,  3.51922146e-01,  5.61567028e-01],
        [ 1.24349741e+00,  5.12252168e-01, -1.36213694e+00]],

       [[-1.10834008e+00,  1.42555196e+00, -2.74288555e-01],
        [ 9.94076568e-01, -3.30236619e-01,  1.88605248e-01]],

       [[ 1.13572047e+00, -3.50532690e-01, -1.75848988e+00],
        [ 5.94094491e-02,  1.01767434e+00, -2.69948273e-01]],

       [[-8.58880061e-01,  9.20040038e-01, -3.24097782e-01],
        [-1.69798455e+00,  6.07696313e-01,  1.56691068e+00]],

       [[ 9.77195383e-01,  2.74840652e-01,  1.52139657e+00],
        [-1.75941870e-01,  8.30134159e-01,  1.17554221e+00]],

       [[-8.93513632e-02,  3.00705342e-01,  6.13881910e-01],
        [-1.56179409e+00,  4.74127651e-01,  7.95935367e-01]],

       [[ 1.36266663e+00,  3.35528850e-01, -1.81631488e+00],
        [ 9.48643755e-01,  7.18599711e-01, -8.24127675e-01]],

       [[ 2.09046132e-01, -6.23508577e-01, -8.28459078e-01],
        [-6.20298927e-01, -9.03403435e-01,  9.30711735e-01]],

       [[ 1.35696970e+00,  6.13368755e-01, -6.17010749e-01],
        [-5.43208556e-01, -9.47188344e-01, -9.30530773e-03]],

       [[ 6.81943924e-01, -5.11136301e-01,  3.32196302e-01],
        [ 8.77751551e-01,  8.75868489e-02,  7.61402852e-01]],

       [[ 1.09583950e+00, -1.38929379e+00,  9.65830930e-01],
        [-7.04597879e-01, -6.88842771e-01,  6.64374199e-01]],

       [[ 6.44149986e-01,  2.09153124e+00, -1.31259825e+00],
        [-1.05262788e+00,  1.92903725e-01,  8.53176124e-01]],

       [[-9.52745289e-01,  1.54413990e+00, -8.83020989e-02],
        [-4.52931838e-01,  1.46013027e-01,  6.80081981e-01]],

       [[-6.14436277e-01, -1.25881277e+00, -7.64476935e-01],
        [ 1.87755528e+00,  2.39406740e-03,  3.87579489e-01]],

       [[ 8.88809779e-01, -6.83587391e-01,  2.63757669e+00],
        [ 6.69524381e-02, -4.81232994e-01, -3.60392857e-01]],

       [[ 2.10737634e+00, -1.72186258e+00,  5.81396087e-01],
        [-2.66669640e-01, -8.20988045e-01,  1.63821409e+00]],

       [[ 5.44695041e-02,  3.07931968e-01,  2.92339638e-01],
        [-1.08433374e+00,  8.73756101e-01,  6.06200630e-01]],

       [[ 7.28305747e-02,  3.59898114e-01, -1.13088745e+00],
        [ 5.86316433e-01,  6.96976040e-01,  4.16070338e-01]],

       [[-7.83515405e-02,  1.44605928e+00,  1.22113425e+00],
        [ 5.15941911e-01, -1.02108354e+00, -1.02605112e+00]],

       [[-9.83189905e-01,  8.14806085e-01, -9.84549883e-01],
        [ 3.88084330e-01,  5.87324530e-02,  9.19775561e-01]],

       [[-3.77996112e-01,  2.78923601e-01,  3.49470036e-01],
        [ 1.42977682e-01, -1.06888528e+00,  1.16852252e-01]],

       [[-7.14900697e-01,  3.68904447e-01,  1.30576065e+00],
        [ 8.45086422e-01,  1.44571096e+00,  1.40314198e-01]],

       [[-9.85294605e-02, -7.41635633e-01,  1.62724707e+00],
        [-1.28581404e+00,  1.19187265e-01,  2.65522899e-01]],

       [[-1.25242486e+00, -1.80539974e+00,  1.09725817e+00],
        [-4.71661865e-02,  8.53667646e-02, -6.26025078e-01]],

       [[-4.93999688e-01, -2.35921420e-01,  1.97829777e-01],
        [ 2.28772006e+00, -2.77260023e-01, -3.73639037e-01]],

       [[-6.91202874e-01,  1.64125049e+00, -1.44133282e+00],
        [ 5.23801422e-01,  1.90423000e+00, -1.08537375e+00]],

       [[ 3.16136657e-01, -1.57631864e-01, -3.80007313e-01],
        [-1.11395678e+00,  2.49938895e-01,  8.53874688e-01]],

       [[ 8.91848566e-01, -2.95212194e+00,  4.03382776e-01],
        [ 1.55117745e+00,  4.67020820e-01, -2.44691571e-02]],

       [[-6.85031385e-01, -7.63468923e-01, -6.94477499e-02],
        [ 1.49310192e+00, -1.42547098e+00, -1.39224549e-01]],

       [[-4.37661762e-01,  2.12225692e+00,  2.10242575e-01],
        [-1.16128134e+00, -2.74139631e-01, -1.29933592e+00]],

       [[ 6.56750013e-01,  5.45411379e-01,  1.86826578e+00],
        [ 4.32236784e-01, -4.92220729e-01, -4.19502601e-02]],

       [[ 8.05549507e-01,  1.07854034e+00, -7.93635440e-01],
        [ 2.91039333e-01, -1.22502974e+00,  4.52726864e-01]],

       [[ 9.53078647e-02,  4.51723966e-01,  2.22600836e+00],
        [ 1.09316916e-02, -2.37204174e-01, -4.49538065e-02]],

       [[ 2.22132699e+00, -1.88507268e+00, -4.58498235e-01],
        [ 1.50903089e+00,  1.21131833e+00,  1.40139067e+00]],

       [[ 2.09860202e+00,  1.09645543e+00, -1.40215994e+00],
        [ 1.10293673e+00,  1.29221409e+00, -4.30130454e-01]],

       [[ 1.05364711e+00,  1.59476999e-01,  1.37261081e+00],
        [-4.67119437e-01, -1.08071461e+00,  5.03272074e-01]],

       [[ 4.57629806e-01,  1.13484259e+00,  1.00077836e+00],
        [ 3.31205802e-01, -3.24091544e-01,  8.81774514e-01]],

       [[-7.20776915e-01,  1.41487772e+00, -4.67362615e-01],
        [ 5.25892564e-01,  2.03351936e+00,  9.49327370e-01]],

       [[-8.20493059e-01, -4.28617633e-02,  2.27294906e-01],
        [-3.30980320e-01,  7.20586745e-01, -6.03909896e-01]],

       [[-6.27540746e-01,  1.18422148e+00,  1.90859002e+00],
        [ 4.58575605e-01, -1.03723868e+00, -3.84191954e-01]],

       [[-3.14273487e-01,  4.85226530e-01,  3.28482977e-01],
        [ 1.09148142e+00, -9.18530965e-01,  4.99094469e-01]],

       [[-7.30583722e-01, -3.66276911e-01, -1.30038961e+00],
        [-1.27149126e+00, -2.22104595e-02,  6.79220485e-01]],

       [[-8.70813107e-01, -1.20720205e+00, -1.13835517e+00],
        [-1.97972973e-01, -3.37641138e-01,  1.40228319e+00]],

       [[ 1.72542420e+00, -2.04121658e+00, -1.85561152e-01],
        [ 1.13327621e+00, -2.68546718e-01,  2.42717239e-01]],

       [[-6.99643303e-01, -7.27148568e-02, -1.33756338e+00],
        [ 7.42463537e-01, -1.25014683e-01, -7.80078431e-01]],

       [[-1.73131757e+00,  1.20078515e+00,  1.24482664e-02],
        [-4.19374493e-01,  3.06912695e-01, -6.58806720e-01]],

       [[ 1.22776232e+00, -3.45820427e-01,  1.65258379e+00],
        [ 1.01792437e+00, -7.47813443e-02,  5.83356126e-01]],

       [[-5.27201468e-02,  8.58906914e-01, -6.81847919e-02],
        [ 9.05600224e-01, -3.32551738e-01, -2.20004006e+00]],

       [[ 1.64693795e+00, -7.53433420e-01, -3.90266181e-01],
        [ 1.31454637e-01,  3.88150825e-01, -1.65526832e+00]],

       [[ 6.03629926e-01, -6.97431745e-01, -4.54984250e-01],
        [ 9.79344087e-01, -7.79310951e-03,  8.81547187e-01]],

       [[ 1.29058909e-01, -6.75384173e-03,  8.92345119e-01],
        [ 4.64062085e-01,  1.01400367e+00, -7.74974094e-01]],

       [[ 1.32678701e+00,  1.06545982e+00,  4.43835033e-01],
        [-1.32631048e+00,  4.45716286e-01,  7.00203834e-01]],

       [[-8.79970388e-01,  1.32406456e+00,  4.73943388e-01],
        [ 1.40333836e+00, -1.21553154e+00,  5.24766956e-01]],

       [[ 1.07091682e+00,  4.79499589e-01, -6.08621662e-01],
        [-2.09118810e-01, -2.20877965e-02,  2.10463624e-01]],

       [[-2.34444538e-01, -1.89629819e-01, -4.62619679e-01],
        [-2.81362439e-01,  6.69785180e-03, -4.30421712e-01]],

       [[-2.03752165e-01, -2.99900723e-01,  4.13670207e-01],
        [-9.99885269e-01, -1.41011635e+00, -1.39632447e+00]],

       [[ 1.49381476e+00,  4.74150318e-01,  6.96813613e-01],
        [-8.24664499e-01, -2.03168426e+00,  1.65074285e-01]],

       [[ 8.18904291e-01, -9.74270835e-01, -7.72718278e-01],
        [-1.08841167e+00,  1.32861724e+00,  8.40361300e-01]],

       [[-4.43134408e-01, -9.53772891e-02,  5.25695656e-02],
        [ 2.07948076e-01,  1.70655263e-01, -1.48913878e+00]],

       [[ 2.76119170e-01, -2.60237978e-01,  6.54404100e-01],
        [ 5.50091269e-01,  1.46619159e+00, -2.36859848e-02]],

       [[-6.94653693e-01, -7.02162258e-01,  3.52878761e-01],
        [-8.37409497e-01,  5.45176475e-01, -5.10140024e-01]],

       [[-6.60768589e-01,  1.06910053e+00,  2.13874227e+00],
        [ 2.00341778e-01, -5.26191051e-01,  2.34671482e-01]],

       [[-3.90149275e-01,  1.27607186e+00,  1.09128116e-01],
        [-8.67662143e-01, -2.36951273e+00,  6.07038075e-01]],

       [[ 1.58305019e-01,  2.89324682e-01,  3.95307870e-02],
        [-2.15078591e+00, -1.86541896e+00, -1.11594655e+00]],

       [[ 2.98266806e-01, -3.89326023e-01, -2.55864360e-01],
        [-6.22478986e-01, -6.96726369e-01,  3.20439534e+00]],

       [[ 1.58826589e-01, -3.83982387e-01, -1.41677194e+00],
        [-4.02453928e-01,  3.82136256e-01,  2.95350507e+00]],

       [[-1.17355165e+00,  1.21884481e-01, -7.87730766e-01],
        [-9.02199035e-01,  1.59821964e-01,  9.90698526e-01]],

       [[-7.91786733e-02, -1.06972586e+00,  3.10250600e-01],
        [ 5.01790222e-01,  9.98189042e-01,  8.64492777e-01]],

       [[-5.55200955e-01, -6.36964226e-01,  8.94967813e-02],
        [ 1.00769289e+00, -1.54107888e+00,  6.19664967e-01]],

       [[ 4.93578706e-01,  1.50483603e+00, -1.65074712e+00],
        [ 9.95142328e-01,  1.55107528e-01, -2.35631895e+00]],

       [[-4.00199183e-01, -1.53415966e+00, -4.77114408e-02],
        [ 5.24263355e-01, -2.96598057e-01, -1.68032886e+00]],

       [[-8.00977509e-01, -2.21839376e-01, -1.78958968e-01],
        [-2.04464024e+00, -1.72553140e-01, -1.16328132e+00]],

       [[ 1.59949165e+00, -4.36991598e-01, -4.60627908e-01],
        [-3.18398222e-01, -9.31119085e-01, -1.19746035e+00]],

       [[ 1.23132600e+00,  1.21433351e+00,  1.15788529e+00],
        [ 5.02044630e-01,  4.29438529e-01,  8.43417844e-01]],

       [[ 2.39130990e-01,  4.22491882e-01,  5.47996052e-02],
        [-2.95844617e-01, -6.72663035e-01,  4.36920008e-01]],

       [[ 1.04766994e+00, -6.67786696e-01,  1.40003089e+00],
        [ 1.14625416e-01, -1.17226063e+00, -3.98454343e-01]]])

9)np.random.normal(loc=0.0, scale=1.0, size=None)

np.random.normal(loc=10,scale=50,size = 35)
#生成正态分布location 标准值是10  
#scale 方差==离散程度 
#50 size 方差为0时 35个数 值为10
array([  30.49673155,    3.12950375,  -48.31282599,  -78.72186185,
         -1.6709422 ,   13.83390572,   65.39487771,  -28.77989387,
        -32.45101314,    9.655045  ,   41.62676427,  -23.24526531,
        -43.74501032,  133.97838634,   21.85240352,   39.5187366 ,
          4.05378682,   67.5030592 ,  106.57110333,   78.67335643,
         40.74672661,   35.63201155,   76.98872486, -134.20778361,
          5.59244002,  -52.81459887,  -11.97689668,  -71.16151954,
          9.93244983, -160.24147064,   71.60968027,  -48.64479858,
        -54.10447494,  101.61678873,   29.58856948])

10) np.random.random(size=None)

生成0到1的随机数,左闭右开

# 图片jpg(0~255),png(0~1)
#随机生成一张图片高度22 宽度55 3是红绿蓝
c = np.random.random(size = (22,55,3))
c
array([[[0.26652578, 0.00551516, 0.2314738 ],
        [0.85297236, 0.07768478, 0.31138069],
        [0.8974675 , 0.50770698, 0.74183405],
        ...,
        [0.40443339, 0.59760571, 0.20388184],
        [0.90862254, 0.55003514, 0.28354203],
        [0.48415181, 0.64538967, 0.53646361]],

       [[0.14362374, 0.08718555, 0.13795558],
        [0.32333512, 0.84079871, 0.9900884 ],
        [0.99519648, 0.42411101, 0.37531979],
        ...,
        [0.00908093, 0.55294791, 0.80156801],
        [0.1360507 , 0.02921999, 0.93333581],
        [0.62700079, 0.48675083, 0.2435331 ]],

       [[0.55270683, 0.471813  , 0.23505056],
        [0.2753174 , 0.77581417, 0.91183962],
        [0.06221068, 0.44083986, 0.99434142],
        ...,
        [0.18303738, 0.2182309 , 0.05338125],
        [0.49683878, 0.77209044, 0.7087025 ],
        [0.01125538, 0.10171562, 0.66659258]],

       ...,

       [[0.95702811, 0.76045914, 0.16419169],
        [0.82431889, 0.85310792, 0.84393974],
        [0.83107594, 0.26073854, 0.04590622],
        ...,
        [0.04070574, 0.91568322, 0.42715057],
        [0.78478417, 0.75466195, 0.18215913],
        [0.63905949, 0.17341542, 0.08493473]],

       [[0.9987377 , 0.06479248, 0.97591728],
        [0.23095179, 0.56044149, 0.77540695],
        [0.64960258, 0.07245667, 0.17028622],
        ...,
        [0.86115224, 0.83007224, 0.95225155],
        [0.78749374, 0.10907472, 0.99863723],
        [0.30567312, 0.05869898, 0.28209614]],

       [[0.68350372, 0.74603692, 0.99631753],
        [0.71010067, 0.65939612, 0.16057702],
        [0.03838214, 0.08058468, 0.79675577],
        ...,
        [0.54542204, 0.20283503, 0.99685662],
        [0.84760123, 0.42360829, 0.27976175],
        [0.22119621, 0.88179437, 0.66029038]]])

使用随机数成成一张图片

plt.imshow(c)
<matplotlib.image.AxesImage at 0x1807ef5f8d0>

png

二、ndarray的属性

4个必记参数:
ndim:维度
shape:形状(各维度的长度)
size:总长度

dtype:元素类型

cat.ndim #图片属性
3
cat
array([[[231, 186, 131],
        [232, 187, 132],
        [233, 188, 133],
        ...,
        [100,  54,  54],
        [ 92,  48,  47],
        [ 85,  43,  44]],

       [[232, 187, 132],
        [232, 187, 132],
        [233, 188, 133],
        ...,
        [100,  54,  54],
        [ 92,  48,  47],
        [ 84,  42,  43]],

       [[232, 187, 132],
        [233, 188, 133],
        [233, 188, 133],
        ...,
        [ 99,  53,  53],
        [ 91,  47,  46],
        [ 83,  41,  42]],

       ...,

       [[199, 119,  82],
        [199, 119,  82],
        [200, 120,  83],
        ...,
        [189,  99,  65],
        [187,  97,  63],
        [187,  97,  63]],

       [[199, 119,  82],
        [199, 119,  82],
        [199, 119,  82],
        ...,
        [188,  98,  64],
        [186,  96,  62],
        [188,  95,  62]],

       [[199, 119,  82],
        [199, 119,  82],
        [199, 119,  82],
        ...,
        [188,  98,  64],
        [188,  95,  62],
        [188,  95,  62]]], dtype=uint8)
nd = np.random.randint(0,150,size = (10,5))#10行5列
nd
array([[107,  74,  50,  80,  82],
       [  6,  96, 103,   0,  51],
       [ 75,  17,  50, 122,   7],
       [130,   2,  44,  74,  68],
       [119, 143, 110,  43,  57],
       [  3,  64, 102,  12, 133],
       [ 35,  81,  24, 104,  27],
       [ 48,  48,  72,  21,  41],
       [ 87,  26,  68,  69,  44],
       [ 40,  85,   2,  92, 132]])
nd.shape
(10, 5)
cat.shape   #shape:形状(各维度的长度)456行 730列
(456, 730, 3)
# 456高度 行数量决定高度
# 730宽度 列数量决定快度
# 3 红禄蓝 颜色决定像素
cat.shape
plt.imshow(cat)
<matplotlib.image.AxesImage at 0x1807ef5a160>

png

456*730*3
998640
cat.size  #456*730*3 
998640
cat.dtype #图片数据类型 
dtype('uint8')
# 数字0 二进制表示 8位:000000000 00000000 00000000 00000000
# 数字1 二进制表示 8位:000000000 00000000 00000000 00000001
#int8 8位 第一位表示正负 
# int8 -128 ~ 127
# 2**8 - 1

# -128 11111111  01111111 127
nd = np.array([-100,-128,-130,20,127,150],dtype=np.int8)
nd
array([-100, -128,  126,   20,  127, -106], dtype=int8)
# int8 范围-128 ~ 127  256
# unit8 范围 0~ 255    256
nd1 = np.random.randint(-128,127,size = 10000,dtype=np.int8)
nd1
array([-84,  84, -20, ...,  32, -53,  25], dtype=int8)
np.save('./nd1',nd1)#将变量nd1保存到当前目录下nd1.npy
nd2 = nd1.copy()
nd2.dtype
nd2 = nd2.astype(np.int16)#转换类型
nd2.dtype

np.save('./nd2',nd2)#保存  内容一样但是文本大小不同int16大
nd2
array([-84,  84, -20, ...,  32, -53,  25], dtype=int16)

三、ndarray的基本操作

1. 索引

一维与列表完全一致
多维时同理

nd = np.random.randint(0,150,size = (3,4,5)) #随机生成三维数组
nd
array([[[ 20, 111,  12,  52,  31],
        [ 54,  50,  56,   9,  83],
        [ 80,  33,  30,  16, 127],
        [ 90, 137,  27,  24,  48]],

       [[ 46,  55,  44,  16, 118],
        [ 99,  56, 135,  42,  78],
        [ 83,  35,  92,  30,  28],
        [ 60, 142,  23, 130,  81]],

       [[ 30,  21,  49,  21,  28],
        [ 52,   9,  52, 140, 122],
        [ 95,  44,   7, 135, 133],
        [ 43,  98,  26,  15, 111]]])
nd[1,-1,-2] #下表索引都是从0 1.....-1  先大 看作整体 再小
130

根据索引修改数据

2. 切片

一维与列表完全一致
多维时同理

nd
array([[[ 20, 111,  12,  52,  31],
        [ 54,  50,  56,   9,  83],
        [ 80,  33,  30,  16, 127],
        [ 90, 137,  27,  24,  48]],

       [[ 46,  55,  44,  16, 118],
        [ 99,  56, 135,  42,  78],
        [ 83,  35,  92,  30,  28],
        [ 60, 142,  23, 130,  81]],

       [[ 30,  21,  49,  21,  28],
        [ 52,   9,  52, 140, 122],
        [ 95,  44,   7, 135, 133],
        [ 43,  98,  26,  15, 111]]])
nd[:,0,:]  #第二维选0
array([[ 20, 111,  12,  52,  31],
       [ 46,  55,  44,  16, 118],
       [ 30,  21,  49,  21,  28]])
nd[:,:,0]  #第三维选0
array([[20, 54, 80, 90],
       [46, 99, 83, 60],
       [30, 52, 95, 43]])

将数据反转,例如[1,2,3]—->[3,2,1]

两个::进行切片

nd
array([[[ 20, 111,  12,  52,  31],
        [ 54,  50,  56,   9,  83],
        [ 80,  33,  30,  16, 127],
        [ 90, 137,  27,  24,  48]],

       [[ 46,  55,  44,  16, 118],
        [ 99,  56, 135,  42,  78],
        [ 83,  35,  92,  30,  28],
        [ 60, 142,  23, 130,  81]],

       [[ 30,  21,  49,  21,  28],
        [ 52,   9,  52, 140, 122],
        [ 95,  44,   7, 135, 133],
        [ 43,  98,  26,  15, 111]]])
nd[:,:,::-2] # 第三维隔一个取他的值 -号反转
array([[[ 31,  12,  20],
        [ 83,  56,  54],
        [127,  30,  80],
        [ 48,  27,  90]],

       [[118,  44,  46],
        [ 78, 135,  99],
        [ 28,  92,  83],
        [ 81,  23,  60]],

       [[ 28,  49,  30],
        [122,  52,  52],
        [133,   7,  95],
        [111,  26,  43]]])
plt.imshow(cat)
<matplotlib.image.AxesImage at 0x1807f034e48>

png

cat1=cat[::-1,:,:]#原图颠倒
plt.imshow(cat1)
cat1=cat[::-5,:,:]#原图颠倒,高度压缩
plt.imshow(cat1)
cat2 = cat[::-10,::-10,:]#每10个切一个
#高度压缩到73宽度压缩到40多 像素减少会变模糊
plt.imshow(cat2)
<matplotlib.image.AxesImage at 0x1807f1e6c88>

png

# 红禄蓝-----> 蓝绿红
cat3 = cat[:,:,::-1] #第一第二维不变 第三维颜色反转
plt.imshow(cat3)
<matplotlib.image.AxesImage at 0x1807f244160>

png

cat4 = cat[:,:,[1,0,2]]#第一第二维度不变 第三维 1 绿色 
plt.imshow(cat4)
<matplotlib.image.AxesImage at 0x1807f299780>

png

plt.imshow(cat)
<matplotlib.image.AxesImage at 0x1807f2f09e8>

png

cat
array([[[231, 186, 131],
        [232, 187, 132],
        [233, 188, 133],
        ...,
        [100,  54,  54],
        [ 92,  48,  47],
        [ 85,  43,  44]],

       [[232, 187, 132],
        [232, 187, 132],
        [233, 188, 133],
        ...,
        [100,  54,  54],
        [ 92,  48,  47],
        [ 84,  42,  43]],

       [[232, 187, 132],
        [233, 188, 133],
        [233, 188, 133],
        ...,
        [ 99,  53,  53],
        [ 91,  47,  46],
        [ 83,  41,  42]],

       ...,

       [[199, 119,  82],
        [199, 119,  82],
        [200, 120,  83],
        ...,
        [189,  99,  65],
        [187,  97,  63],
        [187,  97,  63]],

       [[199, 119,  82],
        [199, 119,  82],
        [199, 119,  82],
        ...,
        [188,  98,  64],
        [186,  96,  62],
        [188,  95,  62]],

       [[199, 119,  82],
        [199, 119,  82],
        [199, 119,  82],
        ...,
        [188,  98,  64],
        [188,  95,  62],
        [188,  95,  62]]], dtype=uint8)
cat.max()
255
cat.min()
0
cat5.min()
0
#a = -10
a = np.array([-10]) #a进行类型转换
a
array([-10])
a.astype(np.uint8)#类型转换为uint8 值的范围0-255 -10 会变成246
array([246], dtype=uint8)
cat5 = cat - 10 
#值0-255 值越小越黑
#原值减少10 最小值减少10 超了,变成0-255正数
plt.imshow(cat5)
<matplotlib.image.AxesImage at 0x1807f437e48>

png

3. 变形

使用reshape函数,注意参数是一个tuple!

nd.shape
(3, 4, 5)
nd.reshape(-1)#变成一维数据
array([ 20, 111,  12,  52,  31,  54,  50,  56,   9,  83,  80,  33,  30,
        16, 127,  90, 137,  27,  24,  48,  46,  55,  44,  16, 118,  99,
        56, 135,  42,  78,  83,  35,  92,  30,  28,  60, 142,  23, 130,
        81,  30,  21,  49,  21,  28,  52,   9,  52, 140, 122,  95,  44,
         7, 135, 133,  43,  98,  26,  15, 111])
nd.reshape(3,21)#元来的数据是60个 现在想变成63个   报错
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-164-77a46828f29d> in <module>()
----> 1 nd.reshape(3,21)


ValueError: cannot reshape array of size 60 into shape (3,21)
#nd.reshape(3,20)  #nd.reshape(6,10) #nd.reshape(4,15) #都可以还是60个

4. 级联

  1. np.concatenate()
    级联需要注意的点:
    • 级联的参数是列表:一定要加中括号或小括号
    • 维度必须相同
    • 形状相符
    • 【重点】级联的方向默认是shape这个tuple的第一个值所代表的维度方向
    • 可通过axis参数改变级联的方向
cat.shape
(456, 730, 3)
cat2.shape #此时cat1 cat2 不能级联
(46, 73, 3)
cat2 = cat[::-1,:,:]#cat2 倒转 可以级联
cat6 = np.concatenate((cat,cat2))#两张图级联成一张
plt.imshow(cat6)
<matplotlib.image.AxesImage at 0x1807fb4eb70>

png

nd1 = np.random.randint(0,150,size = (4,5))#4行5列
nd2 = np.random.randint(0,150,size = (3,5))#3行5列

display(nd1,nd2)#列数相同可以级联
array([[123,  21,  71, 102,  31],
       [ 45,  51, 126, 107, 129],
       [ 87,  95,  17,  26, 129],
       [103,  66,  90,  97,  16]])



array([[137,  34, 110,   4,  42],
       [110,  58,  82,  24,  17],
       [ 85,  10,  26,  54, 148]])
np.concatenate((nd1,nd2))#级联效果
array([[123,  21,  71, 102,  31],
       [ 45,  51, 126, 107, 129],
       [ 87,  95,  17,  26, 129],
       [103,  66,  90,  97,  16],
       [137,  34, 110,   4,  42],
       [110,  58,  82,  24,  17],
       [ 85,  10,  26,  54, 148]])
nd3 = np.random.randint(0,150,size = (4,6))#4行6列

display(nd1,nd3)
array([[123,  21,  71, 102,  31],
       [ 45,  51, 126, 107, 129],
       [ 87,  95,  17,  26, 129],
       [103,  66,  90,  97,  16]])



array([[ 74,  79,  65,  59,  10,  66],
       [129, 140, 105, 111, 141, 141],
       [ 43, 120, 136,  69,  81,   9],
       [116, 134,  52,  12,  81,  20]])
# axis这个参数,0 行; 1 列
np.concatenate((nd1,nd3),axis = 1)
#nd1 nd3默认axis=0 按行此时不能级联 
#指定axis=1 此时可以按列级联 nd1 nd3列数相同可以级联
array([[123,  21,  71, 102,  31,  74,  79,  65,  59,  10,  66],
       [ 45,  51, 126, 107, 129, 129, 140, 105, 111, 141, 141],
       [ 87,  95,  17,  26, 129,  43, 120, 136,  69,  81,   9],
       [103,  66,  90,  97,  16, 116, 134,  52,  12,  81,  20]])
  1. np.hstack与np.vstack
    水平级联与垂直级联,处理自己,进行维度的变更
# 相当于axis = 1
np.hstack((nd1,nd3))#水平级联
array([[123,  21,  71, 102,  31,  74,  79,  65,  59,  10,  66],
       [ 45,  51, 126, 107, 129, 129, 140, 105, 111, 141, 141],
       [ 87,  95,  17,  26, 129,  43, 120, 136,  69,  81,   9],
       [103,  66,  90,  97,  16, 116, 134,  52,  12,  81,  20]])
# 竖直方向上,进行级联,行数变多
# axis = 0
np.vstack((nd1,nd2))#竖直级联
array([[123,  21,  71, 102,  31],
       [ 45,  51, 126, 107, 129],
       [ 87,  95,  17,  26, 129],
       [103,  66,  90,  97,  16],
       [137,  34, 110,   4,  42],
       [110,  58,  82,  24,  17],
       [ 85,  10,  26,  54, 148]])

5. 切分

与级联类似,三个函数完成切分工作:
- np.split
- np.vsplit
- np.hsplit

nd1
array([[123,  21,  71, 102,  31],
       [ 45,  51, 126, 107, 129],
       [ 87,  95,  17,  26, 129],
       [103,  66,  90,  97,  16]])
# indices_or_sections : int or 1-D array
np.split(nd1,indices_or_sections=4)
#对行划分 均分四份 不能平均报错
[array([[123,  21,  71, 102,  31]]),
 array([[ 45,  51, 126, 107, 129]]),
 array([[ 87,  95,  17,  26, 129]]),
 array([[103,  66,  90,  97,  16]])]
np.split(nd1,indices_or_sections=[1,4],axis = 1)
#对列划分  [1,4]左闭右开  [ 21,  71, 102]前三个   [ 31]最后一个
[array([[123],
        [ 45],
        [ 87],
        [103]]), array([[ 21,  71, 102],
        [ 51, 126, 107],
        [ 95,  17,  26],
        [ 66,  90,  97]]), array([[ 31],
        [129],
        [129],
        [ 16]])]
np.vsplit(nd1,indices_or_sections=[1,3])
#竖直方向划分
#0 一份 
#[1,3] 3取不到一份
#3 一份
[array([[123,  21,  71, 102,  31]]), array([[ 45,  51, 126, 107, 129],
        [ 87,  95,  17,  26, 129]]), array([[103,  66,  90,  97,  16]])]
np.hsplit(nd1,indices_or_sections=[1,3])
#在竖直方向上切割
[array([[123],
        [ 45],
        [ 87],
        [103]]), array([[ 21,  71],
        [ 51, 126],
        [ 95,  17],
        [ 66,  90]]), array([[102,  31],
        [107, 129],
        [ 26, 129],
        [ 97,  16]])]

6. 副本

所有赋值运算不会为ndarray的任何元素创建副本。对赋值后的对象的操作也对原来的对象生效。

可使用copy()函数创建副本

cat7 = cat.copy() #复制,重新生成一份,深拷贝
cat8 = cat#浅拷贝 内存地址和cat一样
display(id(cat8),id(cat7),id(cat))
1651372659536



1649284137488



1651372659536

四、ndarray的聚合操作

1. 求和np.sum

2. 最大最小值:np.max/ np.min

同理

3. 其他聚合操作

Function Name   NaN-safe Version    Description
np.sum  np.nansum   Compute sum of elements
np.prod np.nanprod  Compute product of elements
np.mean np.nanmean  Compute mean of elements
np.std  np.nanstd   Compute standard deviation
np.var  np.nanvar   Compute variance
np.min  np.nanmin   Find minimum value
np.max  np.nanmax   Find maximum value
np.argmin   np.nanargmin    Find index of minimum value
np.argmax   np.nanargmax    Find index of maximum value
np.median   np.nanmedian    Compute median of elements
np.percentile   np.nanpercentile    Compute rank-based statistics of elements
np.any  N/A Evaluate whether any elements are true
np.all  N/A Evaluate whether all elements are true
np.power 幂运算

nd = np.arange(0,101,step = 1)
nd

# 累加和
nd.cumsum()
array([   0,    1,    3,    6,   10,   15,   21,   28,   36,   45,   55,
         66,   78,   91,  105,  120,  136,  153,  171,  190,  210,  231,
        253,  276,  300,  325,  351,  378,  406,  435,  465,  496,  528,
        561,  595,  630,  666,  703,  741,  780,  820,  861,  903,  946,
        990, 1035, 1081, 1128, 1176, 1225, 1275, 1326, 1378, 1431, 1485,
       1540, 1596, 1653, 1711, 1770, 1830, 1891, 1953, 2016, 2080, 2145,
       2211, 2278, 2346, 2415, 2485, 2556, 2628, 2701, 2775, 2850, 2926,
       3003, 3081, 3160, 3240, 3321, 3403, 3486, 3570, 3655, 3741, 3828,
       3916, 4005, 4095, 4186, 4278, 4371, 4465, 4560, 4656, 4753, 4851,
       4950, 5050], dtype=int32)
cat.std() #求标准方差
57.42300511189054
cat9 = cat.mean(axis =  2) #求平均值
plt.imshow(cat9,cmap = 'gray')
<matplotlib.image.AxesImage at 0x18001552080>

png

cat9 = cat.max(axis =  2)#求最大值  
plt.imshow(cat9,cmap = 'gray')
<matplotlib.image.AxesImage at 0x180012c6550>

png

cat9 = cat.min(axis =  2)#求最小值
plt.imshow(cat9,cmap = 'gray')
<matplotlib.image.AxesImage at 0x1800131cac8>

png

cat.shape
(456, 730, 3)
cat9.shape
(456, 730)
#聚合之前三维 聚合之后就成两维的了  聚合操作 聚集合并到一起

np.sum 和 np.nansum 的区别
nan not a number

nd1 = np.array([1,3,5,7,9])
nd1.sum()
25
# nan == not a number
nd2 = np.array([1,3,5,np.nan])
nd2
array([ 1.,  3.,  5., nan])
nd2.sum()
nan
# 使用模块进行调用
np.nansum(nd2)
9.0

操作文件

使用pandas打开文件president_heights.csv
获取文件中的数据

五、ndarray的矩阵操作

1. 基本矩阵操作

1) 算术运算符:
- 加减乘除

2) 矩阵积np.dot()

nd1 = np.random.randint(0,10,size = (3,2))#0-10 3行2列
nd2 = np.random.randint(0,10,size = (2,3))#0-10 2行3列

nd3 = np.random.randint(0,10,size = (3,2))
display(nd1,nd2,nd3)
nd1*nd3#矩阵乘法行乘以列 对应位置相乘  数学上乘法*
array([[1, 1],
       [2, 5],
       [8, 2]])



array([[5, 7, 4],
       [9, 2, 4]])



array([[1, 4],
       [3, 0],
       [7, 3]])





array([[ 1,  4],
       [ 6,  0],
       [56,  6]])
np.dot(nd1,nd2)#矩阵乘法
array([[14,  9,  8],
       [55, 24, 28],
       [58, 60, 40]])
# 矩阵的乘法,不满足交换律  dot 点乘
np.dot(nd2,nd1)
array([[51, 48],
       [45, 27]])
nd1 = np.random.randint(0,10,size = (3,2))
nd2 = np.random.randint(0,10,size = (2,5))
display(nd1,nd2)
array([[5, 9],
       [1, 9],
       [7, 3]])



array([[2, 9, 2, 1, 5],
       [9, 3, 6, 6, 8]])
np.dot(nd1,nd2)#可以点乘
array([[91, 72, 64, 59, 97],
       [83, 36, 56, 55, 77],
       [41, 72, 32, 25, 59]])
nd.dot(nd2,nd1)#形状不匹配无法点乘
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-215-18f372a132d0> in <module>()
----> 1 nd.dot(nd2,nd1)


ValueError: shapes (101,) and (2,5) not aligned: 101 (dim 0) != 2 (dim 0)
# 马赛克
plt.imshow(cat)
<matplotlib.image.AxesImage at 0x18001381e48>

png

cat_nose = cat[240:340,260:360] 
#猫鼻子切片 
#第一维高度方向 第二维宽度方向上

cat2 = cat.copy()

cat2[230:330,260:360] = 255
plt.imshow(cat2)
<matplotlib.image.AxesImage at 0x18001494358>

png

plt.imshow(cat[::20,::20])#每搁20个取一个数据 
<matplotlib.image.AxesImage at 0x180014ee390>

png

# 对cat_nose区域进行每隔10个进行一次切片
cat_nose = cat[230:330,260:360]

plt.imshow(cat_nose[::10,::10])#展示马赛克的猫鼻子

cat_nose2 = cat_nose[::20,::20]#打了马赛克的猫鼻子
#现在要将打了马赛克的猫鼻子放在白色区域 #高度宽度不匹配 ,需要将(10,10,3)这个数据放到(100,100,3)区域
# cat_nose[::10,::10].shape  
#(10,10,3)
# cat_nose.shape
#(100,100,3)

cat3 = cat.copy()

# i高度
for i in range(5):
# j  宽度
    for j in range(5):
        cat3[230+i*20:250+i*20,260+j*20:280 + j*20] = cat_nose2[i,j]

plt.imshow(cat3)
<matplotlib.image.AxesImage at 0x180029c3c18>

png

2. 广播机制

【重要】ndarray广播机制的两条规则
- 规则一:为缺失的维度补1
- 规则二:假定缺失元素用已有值填充

例1:
m = np.ones((2, 3))
a = np.arange(3)
求M+a

例2:
a = np.arange(3).reshape((3, 1))
b = np.arange(3)
求a+b

习题
a = np.ones((4, 1))
b = np.arange(4)
求a+b

六、ndarray的排序

小测验:
使用以上所学numpy的知识,对一个ndarray对象进行选择排序。

def Sortn(x):

代码越短越好

1. 快速排序

np.sort()与ndarray.sort()都可以,但有区别:
- np.sort()不改变输入
- ndarray.sort()本地处理,不占用空间,但改变输入

2. 部分排序

np.partition(a,k)

有的时候我们不是对全部数据感兴趣,我们可能只对最小或最大的一部分感兴趣。
- 当k为正时,我们想要得到最小的k个数
- 当k为负时,我们想要得到最大的k个数

猜你喜欢

转载自blog.csdn.net/weixin_41853490/article/details/81119121