NumPy ってなあに?を 1 ページで
What is NumPy? in one page
このページは、PyTorch に入る前に 「数字の表をどう見るか」 に慣れるための導入です。NumPy は、数字を表の形に並べて、まとめて計算するための道具です。このページではまず、配列・shape・axis の 3 つを中心に見ていきます。
This page is a gentle introduction before PyTorch, helping you get used to thinking in tables of numbers. NumPy is a tool for arranging numbers into table-like forms and computing on them together. We focus first on arrays, shape, and axis.
ひとことで言うと、数字を表の形に並べて、まとめて計算するための道具です。
In one sentence: a tool for arranging numbers into tables and computing on them together.
NumPy は、数字を表の形でまとめてあつかうための道具です。Python のふつうのリストでも数字は置けますが、NumPy を使うと、数字をきれいに並べやすく、表の大きさを確認しやすく、まとめて計算しやすくなります。
NumPy is a tool for handling numbers in table-like form. Ordinary Python lists can also store numbers, but NumPy makes it easier to arrange them neatly, inspect their size, and compute on many of them together.
たとえば、[1, 2, 3] は 1 本の数字の列、[[1, 2, 3], [4, 5, 6]] は 2 行 3 列の表、と考えやすくなります。
For example, [1, 2, 3] can be seen as one row of numbers, while [[1, 2, 3], [4, 5, 6]] is a table with 2 rows and 3 columns.
このページでまず大事なのは 3 つだけ: 配列 / shape / axis
The first three things that matter here: array / shape / axis
最初は、この 3 つがわかれば十分です。
At the start, these three ideas are enough.
数字を並べたものです。1 列でも、表でも、もっと大きなかたまりでもよいです。
A collection of numbers. It can be one row, a table, or something larger.
表の大きさです。たとえば (2, 3) は「2 行 3 列」と考えれば十分です。
The size of the table. For example, (2, 3) means “2 rows and 3 columns.”
どの向きに見るかを表します。最初は axis=0 はたて、axis=1 はよこ、と考えると入りやすいです。
The direction you are looking along. A simple first picture is axis=0 for vertical and axis=1 for horizontal.
あとでよく出る 1 語: dtype は「数字の種類」です。最初は後回しでも大丈夫です。
One more word you will meet later: dtype means the kind of numbers. It is okay to leave it for later at first.
補足: ドキュメントでは ndarray という名前も出ますが、このページではまず「数字の表」と読めば十分です。
Note: In documentation you may see the name ndarray, but on this page it is enough to read it as “a table of numbers.”
まずは 1 本の数字の列から始めます。
Start with one row of numbers.
この例では、3 つの数字をひとまとめにしています。import numpy as np は、NumPy を np という短い名前で呼ぶ、という意味です。
This example groups three numbers together. import numpy as np means “use the short name np for NumPy.”
np.array(...) は、数字を NumPy の配列にする、という意味です。
np.array(...) means “turn these numbers into a NumPy array.”
a.shape を見ると、その数字のまとまりがどんな大きさかがわかります。
Looking at a.shape tells you the size of that group of numbers.
ここで見てほしいこと: 1 個ずつ数字を見るのではなく、まとまったかたまりとして見ていることです。
What to notice here: we are not looking at numbers one by one, but as one structured bundle.
次は 2 次元の表にしてみます。
Next, let’s make a two-dimensional table.
この b は、2 行 3 列の表です。だから shape は (2, 3) になります。
This b is a table with 2 rows and 3 columns, so its shape is (2, 3).
ここで大事なのは、shape を見て 「いま自分はどんな表を持っているのか」 を確認するくせをつけることです。
The important habit is to check the shape and ask: “What kind of table am I holding right now?”
axis は、「どの向きにまとめるか」を決める番号です。
axis is the number that chooses which direction to combine along.
表の数字を足すとき、たて方向に見るか、よこ方向に見るかで結果が変わります。
When you add numbers in a table, the result changes depending on whether you look vertically or horizontally.
最初は、axis=0 は たて方向、axis=1 は よこ方向、と考えるだけで十分です。
At first, it is enough to think of axis=0 as vertical and axis=1 as horizontal.
最初はここで迷いやすいです: axis は慣れるまで混乱しやすいので、「向きを決める番号」と思えば十分です。
This is a common confusion point: axis often feels tricky at first, so it is enough to think of it as “the number that chooses a direction.”
行列のかけ算は最後に見ます。ここでもまず大事なのは shape です。
We leave matrix multiplication for later. Here too, the first thing that matters is shape.
a の [[1, 2, 3], [4, 5, 6]] は、「1 行目が [1, 2, 3]、2 行目が [4, 5, 6] の表」です。
a as [[1, 2, 3], [4, 5, 6]] means “a table whose first row is [1, 2, 3] and second row is [4, 5, 6].”
数式では、その同じ表を横と縦にきれいに並べて行列として書きます。つまり、a は数式では $A$、b は $B$、c は $C$ と見るとつながりやすいです。
In math, we write that same table as a matrix. So it helps to read a as $A$, b as $B$, and c as $C$.
shape も同じです。$A$ は 2 行 3 列だから (2, 3)、$B$ は 3 行 2 列だから (3, 2) です。
The shapes are the same idea too. $A$ has 2 rows and 3 columns, so its shape is (2, 3), while $B$ has shape (3, 2).
ここでは、表どうしのかけ算をしています。@ は、このページでは「表どうしをかける記号」くらいに読めば十分です。
Here we multiply two tables. On this page, it is enough to read @ as “the symbol for multiplying two tables.”
まず数字そのものより、shape を先に見るのが大事です。
The important thing is to look at the shapes first, before the numbers themselves.
a は (2, 3)、b は (3, 2) です。内側の 3 と 3 がつながるので計算できて、結果の c は (2, 2) になります。
a has shape (2, 3), and b has shape (3, 2). The inner 3 and 3 match, so multiplication works, and the result c has shape (2, 2).
この読み方はあとで効きます: PyTorch の tensor や ROCm 関連ページでも、まず shape を見る習慣がとても大事です。
This reading habit matters later: on PyTorch tensor pages and ROCm-related pages, checking shape first is very important.
NumPy は、このあと出てくる PyTorch や線形代数のページを読みやすくするための土台です。
NumPy is a foundation that makes the later PyTorch and linear algebra pages easier to read.
| ページ | Page | NumPy が役立つところ | How NumPy Helps |
|---|---|---|---|
| 線形代数 / 行列とAI | Linear algebra / Matrices & AI | ベクトルや行列を「数字の表」として見る練習になる | It helps you see vectors and matrices as tables of numbers |
| PyTorch特急便 | PyTorch Express | shape や axis の感覚が、tensor を読む助けになる | Its shape and axis intuition helps you read tensors later |
| ROCmで見る 系 | See in ROCm series | 入力や出力の「形」を読む基礎になる | It becomes a basis for reading the shapes of inputs and outputs |
よくある詰まり:
1) shape を見ないまま計算する
2) list と NumPy の配列を同じ気分で扱う
3) axis の向きを混乱する
Common stumbles:
1) Computing without checking shape
2) Treating Python lists and NumPy arrays as if they were the same
3) Getting confused about axis directions
NumPy は、数字の表を読む練習に向いています。PyTorch は、その考え方を持ったまま、AI モデルを書いたり、学習で必要な計算をしたり、GPU で動かしたりできる道具です。
NumPy is great for practising how to read tables of numbers. PyTorch keeps that same way of thinking, but extends it to writing AI models, doing training-related computation, and running on GPUs.
つまり、NumPy = 数字の表の練習、PyTorch = その考え方で AI を作って動かす道具 と考えるとわかりやすいです。
A useful picture is: NumPy = practice with numeric tables, and PyTorch = use the same style to build and run AI.
ROCm とのつながり: ROCm 系のページで出てくる入力や出力の shape も、まずは NumPy の表を見る感覚で読むと入りやすいです。
Connection to ROCm: The input and output shapes shown on ROCm-related pages are easier to read if you keep the NumPy “table of numbers” picture in mind.