Python NumPy 中@ at 符号的意思

本文深入探讨了Python中引入的@运算符,该运算符专门用于矩阵乘法,使得代码更加简洁易读。通过实例展示了如何使用@运算符进行矩阵运算,并对比了传统方式,突出了新运算符的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 除了python标准的@的功能(decorator ),一直不知道干什么的,后来查了一下,可以理解成矩阵乘法,见下面的官方文档

 https://docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-465

PEP 465 - A dedicated infix operator for matrix multiplication

PEP 465 adds the @ infix operator for matrix multiplication. Currently, no builtin Python types implement the new operator, however, it can be implemented by defining __matmul__()__rmatmul__(), and __imatmul__() for regular, reflected, and in-place matrix multiplication. The semantics of these methods is similar to that of methods defining other infix arithmetic operators.

Matrix multiplication is a notably common operation in many fields of mathematics, science, engineering, and the addition of @ allows writing cleaner code:

S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)

instead of:

S = dot((dot(H, beta) - r).T,
        dot(inv(dot(dot(H, V), H.T)), dot(H, beta) - r))

NumPy 1.10 has support for the new operator:

>>>

>>> import numpy

>>> x = numpy.ones(3)
>>> x
array([ 1., 1., 1.])

>>> m = numpy.eye(3)
>>> m
array([[ 1., 0., 0.],
       [ 0., 1., 0.],
       [ 0., 0., 1.]])

>>> x @ m
array([ 1., 1., 1.])

See also

PEP 465 – A dedicated infix operator for matrix multiplication

PEP written by Nathaniel J. Smith; implemented by Benjamin Peterson.

 另一个参考:

https://alysivji.github.io/python-matrix-multiplication-operator.html

2017 will forever be etched in our memories as the year Python overtook R to become the leading language for Data Science. There are many factors that play into this: Python's simple syntax, the fantastic PyData ecosystem, and of course buy-in from Python's BDFL.

PEP 465 introduced the @ infix operator that is designated to be used for matrix multiplication. The acceptance and implementation of this proposal in Python 3.5 was a signal to the scientific community that Python is taking its role as a numerical computation language very seriously.

I was a Computational Mathematics major in college so matrices are very near and dear to my heart. Shoutout to Professor Jeff Orchard for having us implement matrix algorithms in C++. His Numerical Linear Algebra course was the best class I've ever taken.

In this post, we will explore the @ operator.

In [1]:

import numpy as np

In [2]:

A = np.matrix('3 1; 8 2')
A

Out[2]:

matrix([[3, 1],
        [8, 2]])

In [3]:

B = np.matrix('6 1; 7 9')
B

Out[3]:

matrix([[6, 1],
        [7, 9]])

In [4]:

A @ B

Out[4]:

matrix([[25, 12],
        [62, 26]])

Let's confirm this works

In [5]:

# element at the top left. i.e. (1, 1) aka (0, 0) in python
A[0, 0] * B[0, 0] + A[0, 1] * B[1, 0]

Out[5]:

25

In [6]:

# element at the top right. i.e. (1, 2) aka (0, 1) in python
A[0, 0] * B[0, 1] + A[0, 1] * B[1, 1]

Out[6]:

12

In [7]:

# element at the bottom left. i.e. (2, 1) aka (1, 0) in python
A[1, 0] * B[0, 0] + A[1, 1] * B[1, 0]

Out[7]:

62

In [8]:

# element at the top right. i.e. (2, 2) aka (1, 1) in python
A[1, 0] * B[0, 1] + A[1, 1] * B[1, 1]

Out[8]:

26

In [9]:

# let's put it in matrix form
result = np.matrix([[A[0, 0] * B[0, 0] + A[0, 1] * B[1, 0], A[0, 0] * B[0, 1] + A[0, 1] * B[1, 1]],
                    [A[1, 0] * B[0, 0] + A[1, 1] * B[1, 0], A[1, 0] * B[0, 1] + A[1, 1] * B[1, 1]]])
result

Out[9]:

matrix([[25, 12],
        [62, 26]])

In [10]:

A @ B == result

Out[10]:

matrix([[ True,  True],
        [ True,  True]], dtype=bool)

Looks good!

The Python Data Model specifies that the @ operator invokes __matmul__ and __rmatmul__.

We can overload @ by defining custom behavior for each of the special methods above, but this would result in our API not being Pythonic.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值