首页 > 其他分享 >numpy基本操作

numpy基本操作

时间:2022-08-16 10:03:14浏览次数:57  
标签:Prints print 数组 np 基本操作 array numpy

数据类型

每个numpy数组都是相同类型元素的网格。Numpy提供了一组可用于构造数组的大量数值数据类型。Numpy在创建数组时尝试猜测数据类型,但构造数组的函数通常还包含一个可选参数来显式指定数据类型。这是一个例子:

import numpy as np

x = np.array([1, 2])   # Let numpy choose the datatype
print(x.dtype)         # Prints "int64"

x = np.array([1.0, 2.0])   # Let numpy choose the datatype
print(x.dtype)             # Prints "float64"

x = np.array([1, 2], dtype=np.int64)   # Force a particular datatype
print(x.dtype)                         # Prints "int64"

数组中的数学

基本数学函数在数组上以元素方式运行,既可以作为运算符重载,也可以作为numpy模块中的函数:

import numpy as np

x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)

# Elementwise sum; both produce the array
# [[ 6.0  8.0]
#  [10.0 12.0]]
print(x + y)
print(np.add(x, y))

# Elementwise difference; both produce the array
# [[-4.0 -4.0]
#  [-4.0 -4.0]]
print(x - y)
print(np.subtract(x, y))

# Elementwise product; both produce the array
# [[ 5.0 12.0]
#  [21.0 32.0]]
print(x * y)
print(np.multiply(x, y))

# Elementwise division; both produce the array
# [[ 0.2         0.33333333]
#  [ 0.42857143  0.5       ]]
print(x / y)
print(np.divide(x, y))

# Elementwise square root; produces the array
# [[ 1.          1.41421356]
#  [ 1.73205081  2.        ]]
print(np.sqrt(x))

请注意,与MATLAB不同,*是元素乘法,而不是矩阵乘法。 我们使用dot函数来计算向量的内积,将向量乘以矩阵,并乘以矩阵。 dot既可以作为numpy模块中的函数,也可以作为数组对象的实例方法:

import numpy as np

x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])

v = np.array([9,10])
w = np.array([11, 12])

# Inner product of vectors; both produce 219
print(v.dot(w))
print(np.dot(v, w))

# Matrix / vector product; both produce the rank 1 array [29 67]
print(x.dot(v))
print(np.dot(x, v))

# Matrix / matrix product; both produce the rank 2 array
# [[19 22]
#  [43 50]]
print(x.dot(y))
print(np.dot(x, y))

 

 Numpy为在数组上执行计算提供了许多有用的函数;其中最有用的函数之一是 SUM

import numpy as np

x = np.array([[1,2],[3,4]])

print(np.sum(x))  # Compute sum of all elements; prints "10"
print(np.sum(x, axis=0))  # Compute sum of each column; prints "[4 6]" //计算列和
print(np.sum(x, axis=1))  # Compute sum of each row; prints "[3 7]" //计算行和

除了使用数组计算数学函数外,我们经常需要对数组中的数据进行整形或其他操作。这种操作的最简单的例子是转置一个矩阵;要转置一个矩阵,只需使用一个数组对象的T属性:

import numpy as np

x = np.array([[1,2], [3,4]])
print(x)    # Prints "[[1 2]
            #          [3 4]]"
print(x.T)  # Prints "[[1 3]
            #          [2 4]]"

# Note that taking the transpose of a rank 1 array does nothing:
v = np.array([1,2,3])
print(v)    # Prints "[1 2 3]"
print(v.T)  # Prints "[1 2 3]"

广播是一种强大的机制,它允许numpy在执行算术运算时使用不同形状的数组。通常,我们有一个较小的数组和一个较大的数组,我们希望多次使用较小的数组来对较大的数组执行一些操作。

例如,假设我们要向矩阵的每一行添加一个常数向量。我们可以这样做:

import numpy as np

# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x)   # Create an empty matrix with the same shape as x

# Add the vector v to each row of the matrix x with an explicit loop
for i in range(4):
    y[i, :] = x[i, :] + v

# Now y is the following
# [[ 2  2  4]
#  [ 5  5  7]
#  [ 8  8 10]
#  [11 11 13]]
print(y)

这会凑效; 但是当矩阵 x 非常大时,在Python中计算显式循环可能会很慢。注意,向矩阵 x 的每一行添加向量 v 等同于通过垂直堆叠多个 v 副本来形成矩阵 vv,然后执行元素的求和x 和 vv。 我们可以像如下这样实现这种方法:

import numpy as np

# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
vv = np.tile(v, (4, 1))   # Stack 4 copies of v on top of each other
print(vv)                 # Prints "[[1 0 1]
                          #          [1 0 1]
                          #          [1 0 1]
                          #          [1 0 1]]"
y = x + vv  # Add x and vv elementwise
print(y)  # Prints "[[ 2  2  4
          #          [ 5  5  7]
          #          [ 8  8 10]
          #          [11 11 13]]"

 

标签:Prints,print,数组,np,基本操作,array,numpy
From: https://www.cnblogs.com/fengwenzhee/p/16590574.html

相关文章

  • numpy切片X[:,0]和X[:,1]
    1.X[:,0]是numpy中数组的一种写法,表示对一个二维数组,取该二维数组第一维中的所有数据,第二维中取第0个数据,直观来说,X[:,0]就是取所有行的第0个数据,X[:,1]就是取所有行的......
  • centos7防火墙基本操作
    开启、关闭、查看防火墙状态systemctl方式systemctlstatusfirewalld  #查看状态(防火墙为开启状态active(running),防火墙为关闭状态inactive(dead))systemctl......
  • Pandas基本操作
    Pandas介绍2008年WesMcKinney开发出的库专门用于数据挖掘的开源python库以Numpy为基础,借力Numpy模块在计算方面性能高的优势基于matplotlib,能够简便的画图独特的数据......
  • Numpy基本操作
     Numpy介绍Numpy(NumericalPython)是一个开源的Python科学计算库,用于快速处理任意维度的数组。Numpy支持常见的数组和矩阵操作。对于同样的数值计算任务,使用Nump......
  • centOS7 firewall 防火墙基本操作
    centOS7firewall防火墙基本操作运维家 2022-07-2309:09 发表于北京收录于合集#防火墙1个#linux22个 一、防火墙的开启、关闭、禁用命令 (1)设置开机......
  • 抽取基本JDBC中的基本操作与数据连接池
     1.JDBCDBC(JavaDataBaseConnectivity):Java数据库连接技术:具体讲就是通过Java连接广泛的数据库,并对表中数据执行增、删、改、查等操作的技术。JDBC是数据库与Java代......
  • HCIA-Datacom 1.1实验 华为VRP系统基本操作
    前言:最近有很多老哥,会私信问我一些华为的网络配置和规划,在调试的时候我发现其实我命令也忘了很多,所以写一个文档,方便大家查阅实验介绍:  实现功能:1.完成设备重命名,路......