python_数据_numpy

numpy

start

import numpy as np

array --> 将XX转成ndarray

nd = np.array([1,4,6,5,3,5,7,8,9,0,2])
nd,type(nd)
  • array(object, dtype=None, copy=True, order=‘K’, subok=False, ndmin=0)
  • out: (array([1, 4, 6, 5, 3, 5, 7, 8, 9, 0, 2]), numpy.ndarray)

索引

nd[::3]   # 差3取一个
  • out: array([1, 5, 7, 0])

常用函数

np.ones(shape=(3,4))
  • out:array([[1., 1., 1., 1.],
    [1., 1., 1., 1.],
    [1., 1., 1., 1.]])
np.linspace(0,99,num=60)   # 均分60段 
np.arange(0,100,5)    # 差5分
np.random.randint(0,150,size=(4,5))
np.random.normal(175,scale=10,size = 50)   # 模拟正太分布
import matplotlib.pyplot as plt
img = np.random.random(size=(300,400,3))
plt.imshow(img)

example

data 是读取的图片

# 转置矩阵
cat10  = data.transpose([1,0,2])  # 转换三维顺序
plt.imshow(cat10)
  • <matplotlib.image.AxesImage at 0xe34f860>
cat11 = np.concatenate([data,data])   # axis = 0   高度级联   轴---->控制方向
plt.figure(figsize=(12,9))
plt.imshow(cat11)
cat12 = np.hstack([cat10,cat10])
plt.imshow(cat12)
  • np.hstack与np.vstack
    水平级联与垂直级联,处理自己,进行维度的变更

切分example

a = np.random.randint(0, 10, size = (3,5))
a
  • out:array([[8, 1, 9, 5, 0],
    [7, 4, 6, 3, 4],
    [8, 6, 4, 5, 9]])
a.T            # 逆矩阵  a.transpose([1,0])    一个库流行起来,大多因为容易记,见名知意
  • array([[8, 7, 8],
    [1, 4, 6],
    [9, 6, 4],
    [5, 3, 5],
    [0, 4, 9]])
np.split(a,indices_or_sections=[2,3],axis=1)    # shift + Tab 查看方法的使用
  • [array([[8, 1],
    [7, 4],
    [8, 6]]), array([[9],
    [6],
    [4]]), array([[5, 0],
    [3, 4],
    [5, 9]])]
# cat 是图片数据
cat11,cat12,cat13 = np.split(cat,indices_or_sections=[300,500],axis=1)
plt.imshow(cat11)

其他聚合操作

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

快速排序

m = np.random.normal(50,scale=5,size = 20)
n = np.sort(m)
display(n)
m
  • out:array([39.40887649, 41.88521003, 44.76491466, 45.4822017 , 46.17289219,46.70453702, 47.91256991, 48.20295803, 48.7437394 , 49.26693877,51.04438549, 51.28749952,51.39541613,51.68891328,52.96908429,54.02395625, 54.37854927, 55.57017976, 57.83520591, 66.92652775])
    array([55.57017976, 54.37854927, 47.91256991, 51.04438549, 66.92652775,44.76491466, 48.20295803, 51.28749952, 57.83520591, 46.70453702,46.17289219, 51.68891328, 52.96908429, 54.02395625, 48.7437394 ,45.4822017 , 39.40887649, 41.88521003, 49.26693877, 51.39541613])
m.sort()
m
  • out:array([39.40887649, 41.88521003, 44.76491466, 45.4822017 , 46.17289219, 46.70453702, 47.91256991, 48.20295803, 48.7437394 , 49.26693877,51.04438549, 51.28749952,51.39541613,51.68891328,52.96908429,54.02395625, 54.37854927, 55.57017976, 57.83520591, 66.92652775])
  • np.sort()与ndarray.sort()都可以,但有区别:
    np.sort()不改变输入
    ndarray.sort()本地处理,不占用空间,但改变输入

猜你喜欢

转载自blog.csdn.net/sinat_39045958/article/details/86503232