• 数据分析第三篇:Numpy知识点


    Numpy

    将字符型数据转为datetime

    import numpy as np
    f = np.array(['2018','2019-01-01','2019-01-02 01:01:01'])
    
    # 把f数组的元素类型改为日期类型
    g = f.astype('M8[D]') # M8[Y] M8[M] M8[D]
    print(g)
    
    # 时间戳(将日期转为数) 上面g的单位不同,这边的数值也不同
    # g中的值距离1970年总共有多少天
    h = g.astype('int32')
    print(h)
    print(h[2] - h[1])

    生成ndarray数组

    - np.random.random((2,2))
    - np.ones((3,4))
    - np.zeros((2,2), dtype='int32')
    - np.arange(1,10)
    - np.linspace(0,2,10)
    - np.eye(3)
    - np.full((3,3),7)

    np.random.random((2,2))
    Out[2]: 
    array([[ 0.61705652, 0.48264423],
    [ 0.69303143, 0.35004567]])
    
    np.ones((3,4))
    Out[3]: 
    array([[ 1., 1., 1., 1.],
    [ 1., 1., 1., 1.],
    [ 1., 1., 1., 1.]])
    
    np.zeros((2,2), dtype='int32')
    Out[4]: 
    array([[0, 0],
    [0, 0]])
    
    np.arange(1,10)
    Out[5]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    np.linspace(0,2,10)
    Out[6]: 
    array([ 0. , 0.22222222, 0.44444444, 0.66666667, 0.88888889,
    1.11111111, 1.33333333, 1.55555556, 1.77777778, 2. ])
    
    np.eye(3)
    Out[7]: 
    array([[ 1., 0., 0.],
    [ 0., 1., 0.],
    [ 0., 0., 1.]])
    
    np.full((3,3),7)
    Out[8]: 
    array([[7, 7, 7],
    [7, 7, 7],
    [7, 7, 7]])

    Numpy 的random模块

    # 使用numpy.random的normal函数生成符合二项分布的随机数
    n = 100
    # 172:期望值
    # 20:标准差
    # n:数字生成数量
    x= np.random.normal(172, 20, n)
    y= np.random.normal(60, 10, n)

    ndarray数组对象的维度操作

    视图变维:array.reshape() array.ravel()

    - ravel() 是扁平化但是不复制,公用一个对象
    - flatten() 是扁平化同时复制,会生成一个新对象并且返回

    import numpy as np
    a = np.arange(1,9)
    # 视图变维使用的还是原始数组中的数据,如果修改了原始数组中的数据,那么新数组读到的数据也会发生变化。
    b = a.reshape((2,4))
    print(a,b)
    a[0] =999
    print(b)
    c = b.ravel()
    print(c)

    复制变维(数据独立):flatten()

    # 测试flatten
    d=b.flatten().reshape((2,4))
    d[0] = 110
    print(b)
    print(d)

     就地变维:直接修改数组维度,不返回新数组 resize() shape

    d.resize(2,2,2)
    d.shape=(2,4)
    print(d)

    ndarray数组的切片操作

    # 数组的切片与列表的切片参数类似
    # 步长为正:默认从前往后切
    # 步长为负:默认从后往前切
    array[起始位置:终止位置:步长]
    a = np.arange(1,10) # array([1, 2, 3, 4, 5, 6, 7, 8, 9])
    a.resize(3,3) # array([[1, 2, 3],
    # [4, 5, 6],
    # [7, 8, 9]])
    a[1:, :] # 第2行到最后一行,所有列

    ndarray数组的掩码操作

    a = np.array([1,2,3,4,5,6,7,8])
    f = np.array([True, False, True, False,False, True, False, True])
    a[f]
    Out[35]: array([1, 3, 6, 8])
    
    # 现在有数组的1-100,我们现在要拿到数组中3的倍数或7的倍数
    flag_a = a%3==0
    flag_b = a%7==0
    
    flag_a
    Out[43]: 
    array([False, False, True, False, False, True, False, False, True,
    False, False, False, False, False, ... False, False, False,
    False, False, True, False, False, True, False, False, True, False], dtype=bool)
    
    flag = np.any([flag_a, flag_b], axis=0)
    
    a[flag]
    Out[45]: 
    array([ 3, 6, 7, 9, 12, 14, 15, 18, 21, 24, 27, 28, 30, 33, 35, 36, 39,
    42, 45, 48, 49, 51, 54, 56, 57, 60, 63, 66, 69, 70, 72, 75, 77, 78,
    81, 84, 87, 90, 91, 93, 96, 98, 99])

    多维数组的组合和拆分

    垂直方向的操作:vstack() vsplit()

    a = np.arange(1,7).reshape(2,3)
    b = np.arange(7,13).reshape(2,3)
    
    a
    Out[54]: 
    array([[1, 2, 3],
    [4, 5, 6]])
    b
    Out[55]: 
    array([[ 7, 8, 9],
    [10, 11, 12]])
    
    c = np.vstack((a,b))
    c
    Out[57]: 
    array([[ 1, 2, 3],
    [ 4, 5, 6],
    [ 7, 8, 9],
    [10, 11, 12]])
    
    a,b = np.vsplit(c, 2)

    水平方向的操作:hstack() hsplit()

    d = np.hstack((a,b))
    a,b = np.hsplit(d, 2)

    深度方向的操作:dstack() dsplit() 二维数组深度操作会变为三维数组,最后拆分也是三维数组

    a
    Out[59]: 
    array([[1, 2, 3],
    [4, 5, 6]])
    
    b
    Out[60]: 
    array([[ 7, 8, 9],
    [10, 11, 12]])
    
    e = np.dstack((a,b))
    
    e
    Out[62]: 
    array([[[ 1, 7],
    [ 2, 8],
    [ 3, 9]],
    
    [[ 4, 10],
    [ 5, 11],
    [ 6, 12]]])
    
    a,b = np.dsplit(e,2)
    a
    Out[64]: 
    array([[[1],
    [2],
    [3]],
    
    [[4],
    [5],
    [6]]])

    多维数组组合与拆分的相关函数

    # 根据axis所指定的轴向(0,1,2)进行多维数组的组合
    # 如果待组合的两个数组都是二维数组
    # axis=0:垂直方向
    # axis=1:水平方向
    # 如果待组合的两个数组都是三维数组
    # axis=0:垂直方向
    # axis=1:水平方向
    # axis=2:深度方向
    np.concatenate((a,b), axis=0)
    # 通过axis给定的轴向和拆分的份数对c进行拆分
    np.split(c,2,axis=0)

    长度不等的两个数组的组合操作

    np.pad(ary, # 原始数组
    pad_width=(0,1), # 补全方式(头部补0个,尾部补1个)
    mode='constant', # 设置补全模式
    constant_values=-1) # 设置补全的默认值为-1
    
    a = np.arange(1,5)
    a
    Out[66]: array([1, 2, 3, 4])
    # 返回一个新数组
    np.pad(a, pad_width=(0,3),mode='constant',constant_values=-1)
    Out[67]: array([ 1, 2, 3, 4, -1, -1, -1])

    简单的一维数组的组合方案

    a = np.arange(1,10)
    b = np.arange(11,20)
    # 垂直方向叠加
    np.row_stack((a,b))
    # 水平方向叠加
    np.column_stack((a,b))

    Numpy数组的其他属性

    1.shape 维度

    2.dtype 元素类型

    3.size 元素的个数

    4.ndim 维度

    5.itemsize 元素字节数

    6.nbytes 数组的总字节数

    7.real 复数数组的实部

    8.imag 复数数组的虚部

    9.T 数组对象的转置视图

    10.flat 返回数组的扁平迭代器

    a = np.arange(1,28)
    a.resize(3,3,3)
    
    a.size
    Out[75]: 27
    
    len(a)
    Out[76]: 3
    
    a.ndim
    Out[77]: 3
    
    a.shape
    Out[78]: (3, 3, 3)
    
    a.dtype
    Out[79]: dtype('int32')
    
    a.dtype.name
    Out[81]: 'int32'
    
    # ndarray数组的扁平迭代器
    for i in a.flat:
    print(i)
    [e for e in a.flat]

  • 相关阅读:
    Android中连接蓝牙设备时遇到createRfcommSocketToServiceRecord的UUID问题和BluetoothSocket的connect失败
    android4.0蓝牙使能的详细解析 (转载)
    蓝牙介绍
    Bluetooth 4.0之Android 讲解
    jQuery来源学习笔记:扩展的实用功能
    Linux 0.12 内核管理存储器
    java战斗系列-战斗MAVENPW结构
    牟大哥:《App自我促销》连载2 直立人迁移走
    AC自己主动机 总结
    SpringMVC 上下文webApplicationContext
  • 原文地址:https://www.cnblogs.com/leijing0607/p/7682957.html
Copyright © 2020-2023  润新知