zoukankan      html  css  js  c++  java
  • ML numpy、pandas、matplotlib的使用

    Numpy的使用

    创建Numpy数组

    import numpy as np
    # 创建一维数组
    np.array([1, 2, 3])
    # 创建二维数组
    np.array([[1, 1, 2, 3], [3, 2, 3, 3]])
    # 创建数组元素都为1的数组
    np.ones(shape=(2, 3))
    # 创建数组元素都为5的数组
    np.full(shape=(2, 3), fill_value=5)
    # 创建等差数列数组
    np.linspace(1, 100, 20)
    # 创建步长相等的数组
    np.arange(0, 100, step=2)
    
    np.random.seed(10) # 设置随机种子
    np.random.randint(0, 100, size=(2, 4))  # 数组中的元素都为整数,范围在0到100之间
    np.random.randn(2, 4)  # 创建标准正态分布的数组
    创建Numpy数组
    查看numpy数组的元素
    # 二维数组的查看方式
    arr[2][3] = 4
    
    arr = np.random.randint(30, 300, size=(4, 5))  # 创建一个4行5列的二维数组
    # 取前两行
    arr[0:2]
    # 取前两列
    arr[:,0: 2]
    
    # 查看数组形状
    arr.shape
    # 改变数组形状
    arr.reshape(2, 10)
    查看numpy数组的元素
    数组的合并
    import matplotlib.pyplot as plt
    img_arr = plt.imread('F:/1.png')
    plt.imshow(img_arr)
    # 把图片进行纵向合并
    arr1 = np.concatenate((img_arr, img_arr), axis = 0)
    plt.imshow(arr1)
    # 把图片进行横向合并
    arr2 = np.hstack((img_arr, img_arr))
    plt.imshow(arr2)
    
    img = np.split(img_arr, [10, 250], axis=0)[1]
    img = np.split(img, [770, 970], axis=1)[1]
    plt.imshow(img)
    数组的合并
    广播机制
    m = np.ones((2, 3))  # 创建元素都为1的数组
    a = np.arange(3)  # 创建长度为3的等差数列
    
    m + a  # 把a的每个元素对应相加
    
    arr = np.random.randint(0, 100, size=(4, 7))
    # 1. 快速排序
    np.sort(arr)  # 每一行进行排序
    np.sort(arr, axis=0)  # 每一列进行排序
    广播机制

    Pandas的使用

    创建Series

    # 使用字典创建
    dic = {
        'math': 100,
        'english': 90,
    }
    Series(data=dic)
    # 使用numpy创建Series
    df = DataFrame(data=np.random.randint(30, 40, size=(3, 5)), index=['a', 'b', 'c'], columns=['A', 'B', 'C', 'D', 'E'])
    Series的创建

    查看Series

    s = Series(data={'a': 'zhangsan', 'b': 'lisi'})
    # 查看所有元素
    s
    # 查看某个元素
    s['a']
    s.loc['a']
    Series的查看

    创建DateFrame

    # 使用numpy创建
    df = DataFrame(data=np.random.randint(30, 40, size=(3, 5)), index=['a', 'b', 'c'], columns=['A', 'B', 'C', 'D', 'E'])
    # 使用字典创建
    dic = {
        'java': [60, 70, 80],
        'python': [90, 100, 100]
    }
    df = DataFrame(data=dic, index=['张三', '李四', '王五'])
    DataFrame的创建

    查看DateFrame

    dic = {
        'java': [60, 70, 80],
        'python': [100, 200, 200]
    }
    df = DataFrame(data=dic, index=['张三', '李四', '王五'])
    df['java']  # 按列查看
    df.loc['李四']  # 按行查看
    df.iloc[1]  # 按照行下标查看
    查看DataFrame

    DataFrame关于缺失值的处理

    import numpy as np
    import pandas as pd
    from pandas import DataFrame, Series
    np.random.seed(10)
    df = DataFrame(np.random.randint(50, 200, size=(4, 5)), index=['a', 'b', 'c', 'd'], columns=['a', 's', 'd', 'd', 'f'])
    df.loc['a', 'a'] = np.nan  # 把其中的某些值赋值为np.nan
    df.iloc[2, 4] = None
    df.iloc[3, 0] = None
    df.isnull().any(axis=1)  # 查看某一列是否全部为空值
    df.notnull().all(axis=1)
    处理缺失值

    Matplotlib的使用

    利用公式  x^2 + y^2 < 1,画一个圆:

    import numpy as np
    import matplotlib.pyplot as plt
    
    data = 2*np.random.rand(10000, 2)-1 # 在-1到1之间显示
    print(data)
    
    x = data[:, 0]
    y = data[:, 1]
    
    # 显示圆形 x^2 + y^2 < 1 其中idx中满足的点即为true,不满足的点为false
    idx = x**2 + y**2 < 1.000
    # print(idx)  # numpy中很少使用循环,可以直接对数组进行操作
    plt.plot(x[idx], y[idx], 'ro', markersize=1)
    plt.show()
    画一个圆

    利用公式  x^2 + y^2 < 1, x^2 + y^2<0.5^2,做差值,画一个圆环:

    import numpy as np
    import matplotlib.pyplot as plt
    
    data = 2*np.random.rand(10000, 2)-1
    print(data)
    
    x = data[:, 0]
    y = data[:, 1]
    
    # 画一个圆环
    idx = x**2 + y**2 < 1.000
    hole = x**2 + y**2 < 0.5**2
    idx = np.logical_and(idx, ~hole)
    plt.plot(x[idx], y[idx], 'ro', markersize=1)
    plt.show()
    圆环

    直方图的显示

    import numpy as np
    import matplotlib.pyplot as plt
    
    data = np.random.random(10000)
    
    print(data.shape)  # 显示形状
    
    np.set_printoptions(edgeitems=5000, suppress=True)
    plt.hist(data, bins=20, color='r', edgecolor='k')  # 属性:edgecolor为边界颜色
    plt.show()
    直方图

    中心极限定理

    import numpy as np
    import matplotlib.pyplot as plt
    
    N = 10000
    times = 100
    z = np.zeros(N)
    for i in range(times):
        z += np.random.random(N)
    z /= times
    plt.hist(z, bins=20, color='b', edgecolor='k')
    plt.show()
    中心极限定理
  • 相关阅读:
    递归函数及Java范例
    笔记本的硬盘坏了
    “References to generic type List should be parameterized”
    配置管理软件(configuration management software)介绍
    WinCE文件目录定制及内存调整
    使用Silverlight for Embedded开发绚丽的界面(3)
    wince国际化语言支持
    Eclipse IDE for Java EE Developers 与Eclipse Classic 区别
    WinCE Heartbeat Message的实现
    使用Silverlight for Embedded开发绚丽的界面(2)
  • 原文地址:https://www.cnblogs.com/abc23/p/11020532.html
Copyright © 2011-2022 走看看