0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

実務でよく出てくるNumPyのトピックまとめ

Last updated at Posted at 2025-10-09

NumPyの基本トピック

NumPyの概要

NumPyPythonで行列演算を行うにあたってデファクトスタンダード的に用いられるライブラリです。scikit-learnなど、様々なライブラリがNumPyを用いて実装されています。

NumPyのインストールと動作確認

$ pip install numpy

NumPyは上記を実行することでPyPIから入手することができます。

NumPyの動作確認にあたっては下記などを実行すると良いです。

import numpy as np

x1 = np.array([[1,1,1], [2,2,2]])
x2 = np.arange(1,11,1)

print(x1.shape)
print(x1)
print("===")
print(x2.shape)
print(x2)

・実行結果

(2, 3)
[[1 1 1]
 [2 2 2]]
===
(10,)
[ 1  2  3  4  5  6  7  8  9 10]

実務でよく用いるNumPyの機能

ファイルの保存・読み込み(txt・csvなど)

import numpy as np

x1 = np.array([[1,10,100], [0.2,0.02,0.002]])

np.savetxt("sample.csv", x1, delimiter=",")

上記を実行すると、下記のようなcsvファイルがカレントディレクトリに生成されます。

sample.csv
1.000000000000000000e+00,1.000000000000000000e+01,1.000000000000000000e+02
2.000000000000000111e-01,2.000000000000000042e-02,2.000000000000000042e-03

それぞれの行における要素の区切りをdelimiterで指定することに着目しておくと良いです。また、+00+01は桁を表しており、それぞれ下記のように理解すると良いです。

表記 解釈
+00 表された数$\times 10^{0}$
+01 表された数$\times 10^{1}$
+02 表された数$\times 10^{2}$
-01 表された数$\times 10^{-1}$
-02 表された数$\times 10^{-2}$
-03 表された数$\times 10^{-3}$

このように作成したcsvファイルは下記を実行することで読み込むことができます。

import numpy as np

x1 = np.loadtxt("sample.csv", delimiter=",")

print(x1.shape)
print(x1)

・実行結果

(2, 3)
[[1.e+00 1.e+01 1.e+02]
 [2.e-01 2.e-02 2.e-03]]

元ファイルの+00e+00で表記されている点に着目しておくと良いです。

多次元配列の操作

連結(np.concatenate)

NumPyを用いて多次元配列の連結を行うにあたってはnp.concatenateを用いると良いです。

import numpy as np

x1 = np.zeros([2,6])
x2 = np.ones([5,6])

x_concat = np.concatenate([x1,x2], axis=0)
print(x_concat.shape)
print(x_concat)

・実行結果

(7, 6)
[[0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]]

np.concatenateでは上記のように連結する多次元配列をリスト形式で指定し、連結する方向をaxisで定義します。axis=0は行方向、axis=1は列方向に対応します。

連結する多次元配列はリスト形式で与えるので下記のように3つ以上の多次元配列を同時に連結することも可能です。

import numpy as np

x1 = np.zeros([2,6])
x2 = np.ones([2,6])
x3 = np.zeros([5,6])

x_concat = np.concatenate([x1,x2,x3], axis=0)
print(x_concat.shape)
print(x_concat)

・実行結果

(9, 6)
[[0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]]

多次元配列の変形(np.reshape)

import numpy as np

x1 = np.arange(1,21,1)
x2 = x1.reshape([10,2])
x3 = x1.reshape([2,10]).T

print(x1.shape, x2.shape, x3.shape)
print("===")
print(x1)
print("===")
print(x2)
print("===")
print(x3)

・実行結果

(20,) (10, 2) (10, 2)
===
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]
===
[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]
 [13 14]
 [15 16]
 [17 18]
 [19 20]]
===
[[ 1 11]
 [ 2 12]
 [ 3 13]
 [ 4 14]
 [ 5 15]
 [ 6 16]
 [ 7 17]
 [ 8 18]
 [ 9 19]
 [10 20]]

多次元配列を変形するにあたっては上記のようにnp.reshapeを用いると良いです。要素の対応については合わせて確認しておくと良いと思います。

np.percentile

import numpy as np

x = np.arange(0,101,1)

print(x)
print(np.percentile(x,10))
print(np.percentile(x,50))

・実行結果

[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35
  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53
  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71
  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89
  90  91  92  93  94  95  96  97  98  99 100]
10.0
50.0

配列の分位数を取得する際にはnp.percentileを用いると良いです。上記の例では0%〜100%をそれぞれの値に対応させるにあたって101個の数字を用いました。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?