VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • 数据分析 之 NumPy(2)

, 60, 67, 86, 113, 92], [ 74, 118, 79, 86, 63, 97, 81, 106], [112, 118, 83, 105, 72, 78, 94, 88]]) np.concatenate((arr,arr),axis=1) # 轴向 0列 1行 #执行效果 array([[101, 67, 88, 94, 75, 82, 105, 96, 101, 67, 88, 94, 75, 82, 105, 96], [100, 86, 112, 69, 79, 100, 65, 114, 100, 86, 112, 69, 79, 100, 65, 114], [ 72, 86, 95, 84, 112, 94, 103, 115, 72, 86, 95, 84, 112, 94, 103, 115], [ 83, 110, 78, 60, 67, 86, 113, 92, 83, 110, 78, 60, 67, 86, 113, 92], [ 74, 118, 79, 86, 63, 97, 81, 106, 74, 118, 79, 86, 63, 97, 81, 106], [112, 118, 83, 105, 72, 78, 94, 88, 112, 118, 83, 105, 72, 78, 94, 88]]) #级联需要注意的点: 级联的参数是列表:一定要加中括号或小括号 维度必须相同 形状相符:在维度保持一致的前提下,如果进行横向(axis=1)级联,必须保证进行级联的数组行数保持一致。如果进行纵向(axis=0)级联, 必须保证进行级联的数组列数保持一致。 可通过axis参数改变级联的方向

ndarray的聚合操作

#求和np.sum
arr.sum(axis=None)
#执行结果
4374

#最大最小值:np.max/ np.min
#平均值:np.mean()

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 幂运算

ndarray的排序

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

#准备数据
import numpy as np 
arr = np.random.randint(60,120,size=(6,8))
arr
array([[101,  67,  88,  94,  75,  82, 105,  96],
       [100,  86, 112,  69,  79, 100,  65, 114],
       [ 72,  86,  95,  84, 112,  94, 103, 115],
       [ 83, 110,  78,  60,  67,  86, 113,  92],
       [ 74, 118,  79,  86,  63,  97,  81, 106],
       [112, 118,  83, 105,  72,  78,  94,  88]])

arr.sort(axis=0)#会作业到原始数据中  axis=0对每一列做排序
arr
#执行结果
array([[ 72,  67,  78,  60,  63,  78,  65,  88],
       [ 74,  86,  79,  69,  67,  82,  81,  92],
       [ 83,  86,  83,  84,  72,  86,  94,  96],
       [100, 110,  88,  86,  75,  94, 103, 106],
       [101, 118,  95,  94,  79,  97, 105, 114],
       [112, 118, 112, 105, 112, 100, 113, 115]])

arr.sort(axis=1)#会作业到原始数据中  axis=0对每一行做排序
arr
#执行结果
array([[ 60,  63,  65,  67,  72,  78,  78,  88],
       [ 67,  69,  74,  79,  81,  82,  86,  92],
       [ 72,  83,  83,  84,  86,  86,  94,  96],
       [ 75,  86,  88,  94, 100, 103, 106, 110],
       [ 79,  94,  95,  97, 101, 105, 114, 118],
       [100, 105, 112, 112, 112, 113, 115, 118]])

简单使用matplotlib.pyplot获取一个numpy数组,对其进行操作

  • 准备

  • 获取一个numpy数组,简单操作查看,展示图片

# 使用matplotlib.pyplot获取一个numpy数组,数据来源于一张图片
import matplotlib.pyplot as plt

img_arr = plt.imread('./part_1/cat.jpg')

img_arr.shape#数组形状
#执行结果
(456,730,3) #行,列,颜色,总个数是维度

img_arr.size#数组大小
#执行结果
998640   #多少元素

img_arr.dtype#数组类型
#执行结果
dtype('uint8')#数组类型, uint8是8位无符号整型

img_arr#查看数组
#执行结果 三位数组231, 186, 131前两位数是像素点,后一位是颜色
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]],
        #中间省略...
        ...,
        [188,  98,  64],
        [188,  95,  62],
        [188,  95,  62]]], dtype=uint8)

plt.imshow(img_arr)#展示图片
#执行结果 如下图

  • 数组减去某个值,图片发生变化

import matplotlib.pyplot as plt

img_arr = plt.imread('./part_1/cat.jpg')

img_arr.ndim #当前数组维度
#执行结果
3

img_arr-100

#执行结果
array([[[131,  86,  31],
        [132,  87,  32],
        [133,  88,  33],
        ...,
        [  0, 210, 210],
        [248, 204, 203],
        [241, 199, 200]],

       [[132,  87,  32],
        [132,  87,  32],
        [133,  88,  33],
        ...,
        [  0, 210, 210],
        [248, 204, 203],
        [240, 198, 199]],
	    #中间省略...
        ...,
        [ 88, 254, 220],
        [ 88, 251, 218],
        [ 88, 251, 218]]], dtype=uint8)

plt.imshow(img_arr - 100)
plt.savefig('./mao.jpg') #保存图片
#执行结果 如下图

  • 将图片进行对称反转操作

import matplotlib.pyplot as plt

img_arr = plt.imread('./part_1/bcy.jpg')
plt.imshow(img_arr[:,::-1,:])
#执行结果如 下图

  • 将图片倒置

plt.imshow(img_arr[::-1,:])
#执行结果如 下图

  • 将图片对称反转并倒置

plt.imshow(img_arr[::-1,::-1])
#执行结果如 下图

  • 将图片对称反转并倒置并修改颜色

plt.imshow(img_arr[::-1,::-1,::-1])
#执行结果如 下图

  • 对原图做剪裁

plt.imshow(img_arr[450:600,95:330])
#执行结果如 下图

  • 对图片进行拼接

arr_3 = np.concatenate((img_arr,img_arr,img_arr),axis=1)
plt.imshow(arr_3)
#执行结果如 下图

arr_3 = np.concatenate((img_arr,img_arr,img_arr),axis=1)#axis=1 轴向 行
arr_9 = np.concatenate((arr_3,arr_3,arr_3),axis=0)#axis=0 轴向 列
plt.imshow(arr_9)
#执行结果如 下图

学习网站

  • 点击访问

作 者:郭楷丰


相关教程