zoukankan      html  css  js  c++  java
  • Numpy API学习

    Numpy 常用API学习(全)

    一、介绍

    NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

    NumPy 的前身 Numeric 最早是由 Jim Hugunin 与其它协作者共同开发,2005 年,Travis Oliphant 在 Numeric 中结合了另一个同性质的程序库 Numarray 的特色,并加入了其它扩展而开发了 NumPy。NumPy 为开放源代码并且由许多协作者共同维护开发。

    二、常用API

    2.1.numpy.genfromtxt

    从文本上读取相应的矩阵数据,delimiter是分隔符

    import numpy

    data = numpy.genfromtxt("data.txt",dtype=str,delimiter=",")

    print(data)
    print(type(data))
    print(help(numpy.genfromtxt))
    2.2.numpy.array

    将列表转换为矩阵

    vector = numpy.array([5,10,15,20])
    matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
    print(vector)
    print(matrix)
    2.3.vector.shape matrix.shape

    获取一维矩阵的元素个数,获取二维矩阵的行列

    vector = numpy.array([1,2,3,4])
    # 一维则打印元素个数
    print(vector.shape)
    matrix = numpy.array([[5,10,15],[20,25,30]])
    # 二维打印数组的行列
    print(matrix.shape)
    2.4.dtype

    返回元素类型

    # 得相同类型
    vector2 = numpy.array([1,2.0,u"xx"])
    vector2.dtype
    2.5.取行列直接用中括号取
    matrix = numpy.array(
      [
      [5,10,15],
      [20,25,30]
      ])
    print(matrix[1][0])
    print(matrix[1][2])
    2.6.切片的使用

    2.6.1.一维切片

    vector = numpy.array([0,1,2,3])
    # 取[0,3)
    print(vector[0:3])

    2.6.2.二维切片

    切片列元素,并转换为行

    matrix = numpy.array(
      [
      [5,10,15],
      [20,25,30]
      ])
    # 取第一列
    print(matrix[:,1])
    # 取第[0,2)列
    print(matrix[:,0:2])
    print(matrix[0:1:,0:2])
    2.7.进行每个元素判断
    import numpy

    vector = numpy.array([5,10,15,20])

    # 对每个元素都进行判断
    vector == 10
    2.8.进行判断二维
    matrix = numpy.array(
      [
      [5,10,15],
      [20,25,30],
      [35,40,45]
      ])
    matrix == 20
    2.9.这个判断值可以作为索引
    vector = numpy.array([5,10,15,20])
    equal_to_ten = (vector == 10)

    print(equal_to_ten)
    print(vector[equal_to_ten])

    matrix = numpy.array(
      [
      [5,10,25],
      [20,25,30],
      [35,40,45]
      ])
    value_25 = matrix==25
    print(value_25)
    print(matrix[value_25])
    2.10.与或进行条件判断

    多条件判断矩阵

    vector = numpy.array([5,10,15,20])
    equal_to_ten_and_five = (vector == 10) & (vector==5)
    print(equal_to_ten_and_five)
    equal_to_ten_and_five = (vector == 10) | (vector==5)
    print(equal_to_ten_and_five)
    vector[equal_to_ten_and_five] = 100
    print(vector)
    2.11.元素转换类型
    vector = numpy.array(["1","2","3"])
    print(vector)
    print(vector.dtype)
    vector = vector.astype(float)
    print(vector)
    print(vector.dtype)
    2.12.求最大最小值min max
    vector = numpy.array([5,6,10,10])
    min = vector.min()
    max = vector.max()
    print(min)
    print(max)
    2.13.求列和
    matrix = numpy.array(
      [
      [5,10,25],
      [20,25,30],
      [35,40,45]
      ])
    # 维度为0时候,按行读取
    matrix.sum(axis=0)
    2.14.求行和
    matrix = numpy.array(
      [
      [5,10,25],
      [20,25,30],
      [35,40,45]
      ])
    # 维度为1时候,按列读取
    matrix.sum(axis=1)
    2.15.列表
    import numpy as np

    # 进行15个元素
    print(np.arange(15))

    # 转成3*5的
    a = np.arange(15).reshape(3,5)
    print(a)
    2.16.打印行列
    # 打印行列
    print(a.shape)
    2.17.打印维度
    # 打印维度
    a.ndim
    2.18.打印dtype的名字
    a.dtype.name
    2.19.数组大小
    # 数组大小
    a.size
    2.20.生成3行4列0矩阵
    # 生成3行4列的0矩阵
    np.zeros((3,4))
    2.21.生成3行4列1矩阵
    # 生成3行4列的1矩阵
    np.ones((3,4))
    2.22.生成10~30,隔着5
    np.arange(10,30,5)
    2.23.重置行列
    np.arange(12).reshape(4,3)
    2.24.随机数
    # 2行3列的矩阵
    np.random.random((2,3))
    2.25.造100个值,0~2*pi,均分100
    # 造100个值
    from numpy import pi
    np.linspace(0,2*pi,100)
    2.26.矩阵的加减点乘法和直接乘法,次方
    a = np.array([20,30,40,50])
    b = np.arange(4)
    print(a)
    print(b)

    c = a-b
    print(c)

    c -= 1
    print(c)

    b**=2
    print(b)

    print(a<35)

     

    A = np.array([[1,1],
                [0,1]])
    B = np.array([[2,0],
                [3,4]])
    print(A)
    print(B)
    print("--------")

    print(A*B)

    print("--------")

    print(A.dot(B))

     

     

  • 相关阅读:
    mac下通过brew切换php版本
    大白话,讲编程之《ES6系列连载》汇总
    Mysql命令大全
    值得收藏的前端大牛博客
    web前端 —— 移动端知识的一些总结
    Linux 目录结构
    【WebSocket No.3】使用WebSocket协议来做服务器
    【WebSocket No.2】WebSocket和Socket实现聊天群发
    GroupBy分组的运用和linq左连接
    try、catch、finally详解,你不知道的异常处理
  • 原文地址:https://www.cnblogs.com/littlepage/p/11963053.html
Copyright © 2011-2022 走看看