이 문서에서는 NumPy를 설치한 다음 NumPy 배열을 만들고 읽고 정렬하는 방법을 설명합니다.
NumPy(일명 Numerical Python)는 Python의 선형 시퀀스 및 행렬에 대한 통계 및 집합 연산을 쉽게 수행할 수 있게 해주는 라이브러리입니다. Python의 목록보다 훨씬 빠른 Python 데이터 유형에 대한 내 노트에서 소개했습니다. NumPy는 데이터 분석 및 과학 컴퓨팅에 자주 사용됩니다.
NumPy를 설치한 다음 NumPy 배열을 만들고 읽고 정렬하는 방법을 다루겠습니다. NumPy 배열은 N차원 배열의 약자인 ndarray라고도 합니다.
pip
를 사용하여 NumPy 패키지를 설치하는 것은 매우 간단하며 다른 소프트웨어 패키지와 마찬가지로 설치할 수 있습니다. pip
安装 NumPy 包非常简单,可以像安装其他软件包一样进行安装:
<ol><li><code><span>pip install numpy</span></code></li></ol>
安装了 NumPy 包后,只需将其导入你的 Python 文件中:
<ol><li><code><span>import</span><span> numpy </span><span>as</span><span> np</span></code></li></ol>
将 numpy
以 np
之名导入是一个标准的惯例,但你可以不使用 np
,而是使用你想要的任何其他别名。
当涉及到处理大量的数值时,NumPy 比普通的 Python 列表快几个数量级。为了看看它到底有多快,我首先测量在普通 Python 列表上进行 min()
和 max()
操作的时间。
我将首先创建一个具有 999,999,999 项的 Python 列表:
<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>
现在我将测量在这个列表中找到最小值的时间:
<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>
这花了大约 27,007 毫秒,也就是大约 27 秒。这是个很长的时间。现在我试着找出寻找最大值的时间:
<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>
这花了大约 28,111 毫秒,也就是大约 28 秒。
现在我试试用 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>
找到最小值花了大约 1151 毫秒,找到最大值 1114 毫秒。这大约是 1 秒。
正如你所看到的,使用 NumPy 可以将寻找一个大约有 10 亿个值的列表的最小值和最大值的时间 从大约 28 秒减少到 1 秒。这就是 NumPy 的强大之处。
有几种方法可以在 NumPy 中创建 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> </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>
有了上面的 ndarray 定义,我将检查几件事。首先,上面定义的变量的类型是 numpy.ndarray
。这是所有 NumPy ndarray 的类型:
<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>
这里要注意的另一件事是 “形状”。ndarray 的形状是 ndarray 的每个维度的长度。你可以看到,my_ndarray
的形状是 (5,)
。这意味着 my_ndarray
包含一个有 5 个元素的维度(轴)。
<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>
数组中的维数被称为它的 “秩”。所以上面的 ndarray 的秩是 1。
我将定义另一个 ndarray my_ndarray2
作为一个多维 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> 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>
这是一个秩为 2 的 ndarray。另一个要检查的属性是 dtype
,也就是数据类型。检查我们的 ndarray 的 dtype
可以得到以下结果:
<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>
int64
意味着我们的 ndarray 是由 64 位整数组成的。NumPy 不能创建混合类型的 ndarray,必须只包含一种类型的元素。如果你定义了一个包含混合元素类型的 ndarray,NumPy 会自动将所有的元素类型转换为可以包含所有元素的最高元素类型。
例如,创建一个 int
和 float
的混合序列将创建一个 float64
的 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.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>
另外,将其中一个元素设置为 string
将创建 dtype
等于 <u21> 的字符串 ndarray,意味着我们的 ndarray 包含 unicode 字符串:</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>
size
属性将显示我们的 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>
如果你不想直接使用列表来创建 ndarray,还有几种可以用来创建它的 NumPy 方法。
你可以使用 np.zeros()
来创建一个填满 0 的 ndarray。它需要一个“形状”作为参数,这是一个包含行数和列数的列表。它还可以接受一个可选的 dtype
<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>
<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>
numpy
를 np
로 가져오는 것이 표준 규칙이지만 np
를 사용할 수는 없지만 원하는 대로 사용할 수 있습니다. 다른 별칭 원하다. 🎜🎜NumPy를 사용하는 이유는 Python 목록보다 훨씬 빠르기 때문입니다.🎜🎜많은 수를 처리할 때 NumPy는 일반 Python 목록보다 훨씬 빠릅니다. 얼마나 빠른지 확인하기 위해 먼저 일반 Python 목록에서 min()
및 max()
작업 시간을 측정했습니다. 🎜🎜먼저 999,999,999개의 항목이 포함된 Python 목록을 만듭니다. 🎜<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>
<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>
<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>
<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>
<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>
numpy.ndarray
입니다. 모든 NumPy 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><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>
my_ndarray
의 모양은 (5,)
입니다. 이는 my_ndarray
에 5개의 요소가 있는 차원(축)이 포함되어 있음을 의미합니다. 🎜<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>
my_ndarray2
를 다차원 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>
dtype
입니다. ndarray의 dtype
을 확인하면 다음과 같은 결과가 나옵니다. 🎜<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>
int64
는 ndarray가 64비트 정수로 구성되어 있음을 의미합니다. NumPy는 한 가지 유형의 요소만 포함해야 하는 혼합 유형의 ndarray를 생성할 수 없습니다. 혼합된 요소 유형을 포함하는 ndarray를 정의하면 NumPy는 모든 요소 유형을 모든 요소를 포함할 수 있는 가장 높은 요소 유형으로 자동 변환합니다. 🎜🎜예를 들어 int
와 float
의 혼합 시퀀스를 생성하면 float64
의 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>
string
의 경우 <u21>와 동일한 <code>dtype
의 문자열 ndarray가 생성됩니다. 즉, 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>
size
속성은 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>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>
np.zeros()
를 사용하여 0으로 채워진 ndarray를 만들 수 있습니다. 행과 열의 수를 포함하는 목록인 "모양"을 인수로 사용합니다. 또한 ndarray의 데이터 유형인 선택적 dtype
매개변수를 허용할 수도 있습니다. 🎜<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>
你可以使用 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>
你可以使用 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>
你可以使用 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>
你可以使用 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>
你可以使用 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>
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>
第四个值将是 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>
你也可以使用分片来读取 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>
分片创建了一个 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>
对于秩超过 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>
读取 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>
例如,为了获得一个 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>
而要得到所有的奇数值,你可以用这个方法:
<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>
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>
如果你将上述两个 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>
这里,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>
在 ndarray 中加一个标量值也有类似的效果,标量值被添加到 ndarray 的所有元素中。这被称为“广播”:
<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>
有两种方法可以对 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>
正如你所看到的,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>
正如你所看到的,np.sort()
方法返回一个已排序的 ndarray,但没有修改它。
我已经介绍了很多关于 NumPy 和 ndarray 的内容。我谈到了创建 ndarray,读取它们的不同方法,基本的向量和标量算术,以及排序。NumPy 还有很多东西可以探索,包括像 union()
和 intersection()
这样的集合操作,像 min()
和 max()
这样的统计操作,等等。
我希望我上面演示的例子是有用的。祝你在探索 NumPy 时愉快。
위 내용은 NumPy를 사용하여 Python에서 숫자 조작의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!