Home > Backend Development > Python Tutorial > Manipulating numbers in Python with NumPy

Manipulating numbers in Python with NumPy

WBOY
Release: 2023-04-15 17:55:03
forward
1508 people have browsed it

Manipulating numbers in Python with NumPy

This article discusses installing NumPy and then creating, reading, and sorting NumPy arrays.

NumPy (i.e. Numerical Python) is a library that makes it easy to perform statistical and set operations on linear sequences and matrices in Python easy. I introduced it in my notes on Python data types, which are orders of magnitude faster than Python's lists. NumPy is used quite frequently in data analysis and scientific computing.

I'll cover installing NumPy, then creating, reading, and sorting NumPy arrays. NumPy arrays are also called ndarrays, short for N-dimensional arrays.

Install NumPy

Use pip Installing the NumPy package is very simple and can be installed like other software packages:

<ol><li><code><span>pip install numpy</span></code></li></ol>
Copy after login

After installing the NumPy package , just import it into your Python file:

<ol><li><code><span>import</span><span> numpy </span><span>as</span><span> np</span></code></li></ol>
Copy after login

It is a standard convention to import numpy as np, but you can do without np, instead use any other alias you want.

Why use NumPy? Because it is orders of magnitude faster than Python lists

When it comes to processing large numbers, NumPy is orders of magnitude faster than regular Python lists. To see how fast it really is, I first measured the time for min() and max() operations on a normal Python list.

I will first create a Python list with 999,999,999 items:

<ol>
<li><code><span>>>></span><span> my_list </span><span>=</span><span> range</span><span>(</span><span>1</span><span>,</span><span> </span><span>1000000000</span><span>)</span></code></li>
<li><code><span>>>></span><span> len</span><span>(</span><span>my_list</span><span>)</span></code></li>
<li><code><span>999999999</span></code></li>
</ol>
Copy after login

Now I will measure the time to find the minimum value in this list:

<ol>
<li><code><span>>>></span><span> start </span><span>=</span><span> </span><span>time</span><span>.</span><span>time</span><span>()</span></code></li>
<li><code><span>>>></span><span> min</span><span>(</span><span>my_list</span><span>)</span></code></li>
<li><code><span>1</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>'Time elapsed in milliseconds: '</span><span> </span><span>+</span><span> str</span><span>((</span><span>time</span><span>.</span><span>time</span><span>()</span><span> </span><span>-</span><span> start</span><span>)</span><span> </span><span>*</span><span> </span><span>1000</span><span>))</span></code></li>
<li><code><span>Time</span><span> elapsed </span><span>in</span><span> milliseconds</span><span>:</span><span> </span><span>27007.00879096985</span></code></li>
</ol>
Copy after login

This took approx. 27,007 milliseconds, which is approximately 27 seconds. This is a long time. Now I try to find the time to find the maximum value:

<ol>
<li><code><span>>>></span><span> start </span><span>=</span><span> </span><span>time</span><span>.</span><span>time</span><span>()</span></code></li>
<li><code><span>>>></span><span> max</span><span>(</span><span>my_list</span><span>)</span></code></li>
<li><code><span>999999999</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>'Time elapsed in milliseconds: '</span><span> </span><span>+</span><span> str</span><span>((</span><span>time</span><span>.</span><span>time</span><span>()</span><span> </span><span>-</span><span> start</span><span>)</span><span> </span><span>*</span><span> </span><span>1000</span><span>))</span></code></li>
<li><code><span>Time</span><span> elapsed </span><span>in</span><span> milliseconds</span><span>:</span><span> </span><span>28111.071348190308</span></code></li>
</ol>
Copy after login

This took about 28,111 milliseconds, which is about 28 seconds.

Now I try to find the minimum and maximum times using NumPy:

<ol>
<li><code><span>>>></span><span> my_list </span><span>=</span><span> np</span><span>.</span><span>arange</span><span>(</span><span>1</span><span>,</span><span> </span><span>1000000000</span><span>)</span></code></li>
<li><code><span>>>></span><span> len</span><span>(</span><span>my_list</span><span>)</span></code></li>
<li><code><span>999999999</span></code></li>
<li><code><span>>>></span><span> start </span><span>=</span><span> </span><span>time</span><span>.</span><span>time</span><span>()</span></code></li>
<li><code><span>>>></span><span> my_list</span><span>.</span><span>min</span><span>()</span></code></li>
<li><code><span>1</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>'Time elapsed in milliseconds: '</span><span> </span><span>+</span><span> str</span><span>((</span><span>time</span><span>.</span><span>time</span><span>()</span><span> </span><span>-</span><span> start</span><span>)</span><span> </span><span>*</span><span> </span><span>1000</span><span>))</span></code></li>
<li><code><span>Time</span><span> elapsed </span><span>in</span><span> milliseconds</span><span>:</span><span> </span><span>1151.1778831481934</span></code></li>
<li><code><span>>>></span></code></li>
<li><code><span>>>></span><span> start </span><span>=</span><span> </span><span>time</span><span>.</span><span>time</span><span>()</span></code></li>
<li><code><span>>>></span><span> my_list</span><span>.</span><span>max</span><span>()</span></code></li>
<li><code><span>999999999</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>'Time elapsed in milliseconds: '</span><span> </span><span>+</span><span> str</span><span>((</span><span>time</span><span>.</span><span>time</span><span>()</span><span> </span><span>-</span><span> start</span><span>)</span><span> </span><span>*</span><span> </span><span>1000</span><span>))</span></code></li>
<li><code><span>Time</span><span> elapsed </span><span>in</span><span> milliseconds</span><span>:</span><span> </span><span>1114.8970127105713</span></code></li>
</ol>
Copy after login

It took about 1151 milliseconds to find the minimum and 1114 milliseconds to find the maximum. This is approximately 1 second.

As you can see, using NumPy can reduce the time to find the minimum and maximum values ​​of a list of about 1 billion values ​​from about 28 seconds to 1 second. This is the power of NumPy.

Creating ndarrays using Python lists

There are several ways to create ndarrays in NumPy.

You can create an ndarray by using a list of elements:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([</span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>5</span><span>])</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>[</span><span>1</span><span> </span><span>2</span><span> </span><span>3</span><span> </span><span>4</span><span> </span><span>5</span><span>]</span></code></li>
</ol>
Copy after login

With the ndarray definition above, I will check a few things. First of all, the type of the variable defined above is numpy.ndarray. This is the type for all NumPy ndarrays:

<ol>
<li><code><span>>>></span><span> type</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span><span>class</span><span> </span><span>'numpy.ndarray'</span><span>></span></span></code></li>
</ol>
Copy after login

Another thing to note here is the "shapeshape". The shape of an ndarray is the length of each dimension of the ndarray. You can see that the shape of my_ndarray is (5,). This means my_ndarray contains a dimension (axis) with 5 elements.

<ol>
<li><code><span>>>></span><span> np</span><span>.</span><span>shape</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>(</span><span>5</span><span>,)</span></code></li>
</ol>
Copy after login

The number of dimensions in an array is called its "rankrank". So the rank of the ndarray above is 1.

I will define another ndarray my_ndarray2 as a multidimensional ndarray. So what will its shape be? Please see below:

<ol>
<li><code><span>>>></span><span> my_ndarray2 </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([(</span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>),</span><span> </span><span>(</span><span>4</span><span>,</span><span> </span><span>5</span><span>,</span><span> </span><span>6</span><span>)])</span></code></li>
<li><code><span>>>></span><span> np</span><span>.</span><span>shape</span><span>(</span><span>my_ndarray2</span><span>)</span></code></li>
<li><code><span>(</span><span>2</span><span>,</span><span> </span><span>3</span><span>)</span></code></li>
</ol>
Copy after login

This is an ndarray of rank 2. Another property to check is dtype, which is the data type. Checking the dtype of our ndarray yields the following result:

<ol>
<li><code><span>>>></span><span> my_ndarray</span><span>.</span><span>dtype</span></code></li>
<li><code><span>dtype</span><span>(</span><span>'int64'</span><span>)</span></code></li>
</ol>
Copy after login

int64 means that our ndarray is composed of 64-bit integers. NumPy cannot create ndarrays of mixed types, which must contain elements of only one type. If you define an ndarray containing mixed element types, NumPy will automatically convert all element types to the highest element type that can contain all elements.

For example, creating a mixed sequence of int and float will create an ndarray of float64:

<ol>
<li><code><span>>>></span><span> my_ndarray2 </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([</span><span>1</span><span>,</span><span> </span><span>2.0</span><span>,</span><span> </span><span>3</span><span>])</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2</span><span>)</span></code></li>
<li><code><span>[</span><span>1.</span><span> </span><span>2.</span><span> </span><span>3.</span><span>]</span></code></li>
<li><code><span>>>></span><span> my_ndarray2</span><span>.</span><span>dtype</span></code></li>
<li><code><span>dtype</span><span>(</span><span>'float64'</span><span>)</span></code></li>
</ol>
Copy after login

Additionally, Setting one of the elements to string will create a string ndarray with dtype equal to <u21>, meaning our ndarray contains unicode strings: </u21>

<ol>
<li><code><span>>>></span><span> my_ndarray2 </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([</span><span>1</span><span>,</span><span> </span><span>'2'</span><span>,</span><span> </span><span>3</span><span>])</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2</span><span>)</span></code></li>
<li><code><span>[</span><span>'1'</span><span> </span><span>'2'</span><span> </span><span>'3'</span><span>]</span></code></li>
<li><code><span>>>></span><span> my_ndarray2</span><span>.</span><span>dtype</span></code></li>
<li><code><span>dtype</span><span>(</span><span>'<u21><span>)</span></u21></span></code></li>
</ol>
Copy after login

size The property will show the total number of elements present in our ndarray:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([</span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>5</span><span>])</span></code></li>
<li><code><span>>>></span><span> my_ndarray</span><span>.</span><span>size</span></code></li>
<li><code><span>5</span></code></li>
</ol>
Copy after login

Using NumPy methods to create ndarray

If you don’t want to use a list directly to create an ndarray, There are also several NumPy methods that can be used to create it.

You can use np.zeros() to create an ndarray filled with 0s. It takes a "shape" as argument, which is a list containing the number of rows and columns. It can also accept an optional dtype parameter, which is the data type of ndarray:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>zeros</span><span>([</span><span>2</span><span>,</span><span>3</span><span>],</span><span> dtype</span><span>=</span><span>int</span><span>)</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>[[</span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span>]]</span></code></li>
</ol>
Copy after login

你可以使用 np. ones() 来创建一个填满 1 的 ndarray:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>ones</span><span>([</span><span>2</span><span>,</span><span>3</span><span>],</span><span> dtype</span><span>=</span><span>int</span><span>)</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>[[</span><span>1</span><span> </span><span>1</span><span> </span><span>1</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span>1</span><span> </span><span>1</span><span> </span><span>1</span><span>]]</span></code></li>
</ol>
Copy after login

你可以使用 np.full() 来给 ndarray 填充一个特定的值:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>full</span><span>([</span><span>2</span><span>,</span><span>3</span><span>],</span><span> </span><span>10</span><span>,</span><span> dtype</span><span>=</span><span>int</span><span>)</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>[[</span><span>10</span><span> </span><span>10</span><span> </span><span>10</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span>10</span><span> </span><span>10</span><span> </span><span>10</span><span>]]</span></code></li>
</ol>
Copy after login

你可以使用 np.eye() 来创建一个单位矩阵 / ndarray,这是一个沿主对角线都是 1 的正方形矩阵。正方形矩阵是一个行数和列数相同的矩阵:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>eye</span><span>(</span><span>3</span><span>,</span><span> dtype</span><span>=</span><span>int</span><span>)</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>[[</span><span>1</span><span> </span><span>0</span><span> </span><span>0</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span>0</span><span> </span><span>1</span><span> </span><span>0</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span>0</span><span> </span><span>0</span><span> </span><span>1</span><span>]]</span></code></li>
</ol>
Copy after login

你可以使用 np.diag() 来创建一个沿对角线有指定数值的矩阵,而在矩阵的其他部分为 0

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>diag</span><span>([</span><span>10</span><span>,</span><span> </span><span>20</span><span>,</span><span> </span><span>30</span><span>,</span><span> </span><span>40</span><span>,</span><span> </span><span>50</span><span>])</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>[[</span><span>10</span><span></span><span>0</span><span></span><span>0</span><span></span><span>0</span><span></span><span>0</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span> </span><span>0</span><span> </span><span>20</span><span></span><span>0</span><span></span><span>0</span><span></span><span>0</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span> </span><span>0</span><span></span><span>0</span><span> </span><span>30</span><span></span><span>0</span><span></span><span>0</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span> </span><span>0</span><span></span><span>0</span><span></span><span>0</span><span> </span><span>40</span><span></span><span>0</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span> </span><span>0</span><span></span><span>0</span><span></span><span>0</span><span></span><span>0</span><span> </span><span>50</span><span>]]</span></code></li>
</ol>
Copy after login

你可以使用 np.range() 来创建一个具有特定数值范围的 ndarray。它是通过指定一个整数的开始和结束(不包括)范围以及一个步长来创建的:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>arange</span><span>(</span><span>1</span><span>,</span><span> </span><span>20</span><span>,</span><span> </span><span>3</span><span>)</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>[</span><span> </span><span>1</span><span></span><span>4</span><span></span><span>7</span><span> </span><span>10</span><span> </span><span>13</span><span> </span><span>16</span><span> </span><span>19</span><span>]</span></code></li>
</ol>
Copy after login

读取 ndarray

ndarray 的值可以使用索引、分片或布尔索引来读取。

使用索引读取 ndarray 的值

在索引中,你可以使用 ndarray 的元素的整数索引来读取数值,就像你读取 Python 列表一样。就像 Python 列表一样,索引从 0 开始。

例如,在定义如下的 ndarray 中:

<ol><li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>arange</span><span>(</span><span>1</span><span>,</span><span> </span><span>20</span><span>,</span><span> </span><span>3</span><span>)</span></code></li></ol>
Copy after login

第四个值将是 my_ndarray[3],即 10。最后一个值是 my_ndarray[-1],即 19

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>arange</span><span>(</span><span>1</span><span>,</span><span> </span><span>20</span><span>,</span><span> </span><span>3</span><span>)</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[</span><span>0</span><span>])</span></code></li>
<li><code><span>1</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[</span><span>3</span><span>])</span></code></li>
<li><code><span>10</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[-</span><span>1</span><span>])</span></code></li>
<li><code><span>19</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[</span><span>5</span><span>])</span></code></li>
<li><code><span>16</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[</span><span>6</span><span>])</span></code></li>
<li><code><span>19</span></code></li>
</ol>
Copy after login

使用分片读取 ndarray

你也可以使用分片来读取 ndarray 的块。分片的工作方式是用冒号(:)操作符指定一个开始索引和一个结束索引。然后,Python 将获取该开始和结束索引之间的 ndarray 片断:

<ol>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[:])</span></code></li>
<li><code><span>[</span><span> </span><span>1</span><span></span><span>4</span><span></span><span>7</span><span> </span><span>10</span><span> </span><span>13</span><span> </span><span>16</span><span> </span><span>19</span><span>]</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[</span><span>2</span><span>:</span><span>4</span><span>])</span></code></li>
<li><code><span>[</span><span> </span><span>7</span><span> </span><span>10</span><span>]</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[</span><span>5</span><span>:</span><span>6</span><span>])</span></code></li>
<li><code><span>[</span><span>16</span><span>]</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[</span><span>6</span><span>:</span><span>7</span><span>])</span></code></li>
<li><code><span>[</span><span>19</span><span>]</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[:-</span><span>1</span><span>])</span></code></li>
<li><code><span>[</span><span> </span><span>1</span><span></span><span>4</span><span></span><span>7</span><span> </span><span>10</span><span> </span><span>13</span><span> </span><span>16</span><span>]</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>[-</span><span>1</span><span>:])</span></code></li>
<li><code><span>[</span><span>19</span><span>]</span></code></li>
</ol>
Copy after login

分片创建了一个 ndarray 的引用(或视图)。这意味着,修改分片中的值也会改变原始 ndarray 的值。

比如说:

<ol>
<li><code><span>>>></span><span> my_ndarray</span><span>[-</span><span>1</span><span>:]</span><span> </span><span>=</span><span> </span><span>100</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>[</span><span></span><span>1</span><span> </span><span>4</span><span> </span><span>7</span><span></span><span>10</span><span></span><span>13</span><span></span><span>16</span><span> </span><span>100</span><span>]</span></code></li>
</ol>
Copy after login

对于秩超过 1 的 ndarray 的分片,可以使用 [行开始索引:行结束索引, 列开始索引:列结束索引] 语法:

<ol>
<li><code><span>>>></span><span> my_ndarray2 </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([(</span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>),</span><span> </span><span>(</span><span>4</span><span>,</span><span> </span><span>5</span><span>,</span><span> </span><span>6</span><span>)])</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2</span><span>)</span></code></li>
<li><code><span>[[</span><span>1</span><span> </span><span>2</span><span> </span><span>3</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span>4</span><span> </span><span>5</span><span> </span><span>6</span><span>]]</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2</span><span>[</span><span>0</span><span>:</span><span>2</span><span>,</span><span>1</span><span>:</span><span>3</span><span>])</span></code></li>
<li><code><span>[[</span><span>2</span><span> </span><span>3</span><span>]</span></code></li>
<li><code><span> </span><span>[</span><span>5</span><span> </span><span>6</span><span>]]</span></code></li>
</ol>
Copy after login

使用布尔索引读取 ndarray 的方法

读取 ndarray 的另一种方法是使用布尔索引。在这种方法中,你在方括号内指定一个过滤条件,然后返回符合该条件的 ndarray 的一个部分。

例如,为了获得一个 ndarray 中所有大于 5 的值,你可以指定布尔索引操作 my_ndarray[my_ndarray > 5]。这个操作将返回一个包含所有大于 5 的值的 ndarray:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([</span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>5</span><span>,</span><span> </span><span>6</span><span>,</span><span> </span><span>7</span><span>,</span><span> </span><span>8</span><span>,</span><span> </span><span>9</span><span>,</span><span> </span><span>10</span><span>])</span></code></li>
<li><code><span>>>></span><span> my_ndarray2 </span><span>=</span><span> my_ndarray</span><span>[</span><span>my_ndarray </span><span>></span><span> </span><span>5</span><span>]</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2</span><span>)</span></code></li>
<li><code><span>[</span><span> </span><span>6</span><span></span><span>7</span><span></span><span>8</span><span></span><span>9</span><span> </span><span>10</span><span>]</span></code></li>
</ol>
Copy after login

例如,为了获得一个 ndarray 中的所有偶数值,你可以使用如下的布尔索引操作:

<ol>
<li><code><span>>>></span><span> my_ndarray2 </span><span>=</span><span> my_ndarray</span><span>[</span><span>my_ndarray </span><span>%</span><span> </span><span>2</span><span> </span><span>==</span><span> </span><span>0</span><span>]</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2</span><span>)</span></code></li>
<li><code><span>[</span><span> </span><span>2</span><span></span><span>4</span><span></span><span>6</span><span></span><span>8</span><span> </span><span>10</span><span>]</span></code></li>
</ol>
Copy after login

而要得到所有的奇数值,你可以用这个方法:

<ol>
<li><code><span>>>></span><span> my_ndarray2 </span><span>=</span><span> my_ndarray</span><span>[</span><span>my_ndarray </span><span>%</span><span> </span><span>2</span><span> </span><span>==</span><span> </span><span>1</span><span>]</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2</span><span>)</span></code></li>
<li><code><span>[</span><span>1</span><span> </span><span>3</span><span> </span><span>5</span><span> </span><span>7</span><span> </span><span>9</span><span>]</span></code></li>
</ol>
Copy after login

ndarray 的矢量和标量算术

NumPy 的 ndarray 允许进行矢量和标量算术操作。在矢量算术中,在两个 ndarray 之间进行一个元素的算术操作。在标量算术中,算术运算是在一个 ndarray 和一个常数标量值之间进行的。

如下的两个 ndarray:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([</span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>5</span><span>])</span></code></li>
<li><code><span>>>></span><span> my_ndarray2 </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([</span><span>6</span><span>,</span><span> </span><span>7</span><span>,</span><span> </span><span>8</span><span>,</span><span> </span><span>9</span><span>,</span><span> </span><span>10</span><span>])</span></code></li>
</ol>
Copy after login

如果你将上述两个 ndarray 相加,就会产生一个两个 ndarray 的元素相加的新的 ndarray。例如,产生的 ndarray 的第一个元素将是原始 ndarray 的第一个元素相加的结果,以此类推:

<ol>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2 </span><span>+</span><span> my_ndarray</span><span>)</span></code></li>
<li><code><span>[</span><span> </span><span>7</span><span></span><span>9</span><span> </span><span>11</span><span> </span><span>13</span><span> </span><span>15</span><span>]</span></code></li>
</ol>
Copy after login

这里,7 是 1 和 6 的和,这是我相加的 ndarray 中的前两个元素。同样,15 是 5 和10 之和,是最后一个元素。

请看以下算术运算:

<ol>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2 </span><span>-</span><span> my_ndarray</span><span>)</span></code></li>
<li><code><span>[</span><span>5</span><span> </span><span>5</span><span> </span><span>5</span><span> </span><span>5</span><span> </span><span>5</span><span>]</span></code></li>
<li><code><span>>>></span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2 </span><span>*</span><span> my_ndarray</span><span>)</span></code></li>
<li><code><span>[</span><span> </span><span>6</span><span> </span><span>14</span><span> </span><span>24</span><span> </span><span>36</span><span> </span><span>50</span><span>]</span></code></li>
<li><code><span>>>></span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray2 </span><span>/</span><span> my_ndarray</span><span>)</span></code></li>
<li><code><span>[</span><span>6.</span><span> </span><span>3.5</span><span></span><span>2.66666667</span><span> </span><span>2.25</span><span> </span><span>2.</span><span></span><span>]</span></code></li>
</ol>
Copy after login

在 ndarray 中加一个标量值也有类似的效果,标量值被添加到 ndarray 的所有元素中。这被称为“广播broadcasting”:

<ol>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray </span><span>+</span><span> </span><span>10</span><span>)</span></code></li>
<li><code><span>[</span><span>11</span><span> </span><span>12</span><span> </span><span>13</span><span> </span><span>14</span><span> </span><span>15</span><span>]</span></code></li>
<li><code><span>>>></span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray </span><span>-</span><span> </span><span>10</span><span>)</span></code></li>
<li><code><span>[-</span><span>9</span><span> </span><span>-</span><span>8</span><span> </span><span>-</span><span>7</span><span> </span><span>-</span><span>6</span><span> </span><span>-</span><span>5</span><span>]</span></code></li>
<li><code><span>>>></span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray </span><span>*</span><span> </span><span>10</span><span>)</span></code></li>
<li><code><span>[</span><span>10</span><span> </span><span>20</span><span> </span><span>30</span><span> </span><span>40</span><span> </span><span>50</span><span>]</span></code></li>
<li><code><span>>>></span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray </span><span>/</span><span> </span><span>10</span><span>)</span></code></li>
<li><code><span>[</span><span>0.1</span><span> </span><span>0.2</span><span> </span><span>0.3</span><span> </span><span>0.4</span><span> </span><span>0.5</span><span>]</span></code></li>
</ol>
Copy after login

ndarray 的排序

有两种方法可以对 ndarray 进行原地或非原地排序。原地排序会对原始 ndarray 进行排序和修改,而非原地排序会返回排序后的 ndarray,但不会修改原始 ndarray。我将尝试这两个例子:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([</span><span>3</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>5</span><span>,</span><span> </span><span>4</span><span>])</span></code></li>
<li><code><span>>>></span><span> my_ndarray</span><span>.</span><span>sort</span><span>()</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>[</span><span>1</span><span> </span><span>2</span><span> </span><span>3</span><span> </span><span>4</span><span> </span><span>5</span><span>]</span></code></li>
</ol>
Copy after login

正如你所看到的,sort() 方法对 ndarray 进行原地排序,并修改了原数组。

还有一个方法叫 np.sort(),它对数组进行非原地排序:

<ol>
<li><code><span>>>></span><span> my_ndarray </span><span>=</span><span> np</span><span>.</span><span>array</span><span>([</span><span>3</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>5</span><span>,</span><span> </span><span>4</span><span>])</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>np</span><span>.</span><span>sort</span><span>(</span><span>my_ndarray</span><span>))</span></code></li>
<li><code><span>[</span><span>1</span><span> </span><span>2</span><span> </span><span>3</span><span> </span><span>4</span><span> </span><span>5</span><span>]</span></code></li>
<li><code><span>>>></span><span> </span><span>print</span><span>(</span><span>my_ndarray</span><span>)</span></code></li>
<li><code><span>[</span><span>3</span><span> </span><span>1</span><span> </span><span>2</span><span> </span><span>5</span><span> </span><span>4</span><span>]</span></code></li>
</ol>
Copy after login

正如你所看到的,np.sort() 方法返回一个已排序的 ndarray,但没有修改它。

总结

我已经介绍了很多关于 NumPy 和 ndarray 的内容。我谈到了创建 ndarray,读取它们的不同方法,基本的向量和标量算术,以及排序。NumPy 还有很多东西可以探索,包括像 union() 和 intersection()这样的集合操作,像 min() 和 max() 这样的统计操作,等等。

我希望我上面演示的例子是有用的。祝你在探索 NumPy 时愉快。 

 

The above is the detailed content of Manipulating numbers in Python with NumPy. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template