ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|
基本: np.array()
Python リスト / タプルから NumPy 配列 (ndarray) を生成する最も基本的な関数:
import numpy as np
# 1 次元配列
a = np.array([1, 2, 3, 4, 5])
print(a) # [1 2 3 4 5]
print(a.shape) # (5,)
print(a.dtype) # int64
# 2 次元配列
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
# [[1 2 3]
# [4 5 6]]
print(b.shape) # (2, 3)
print(b.ndim) # 2
# 3 次元配列
c = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(c.shape) # (2, 2, 2)
定数充填: zeros / ones / full / empty
# すべて 0 で埋める
zeros = np.zeros((3, 4))
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]
# dtype はデフォルト float64
# すべて 1 で埋める
ones = np.ones((2, 3), dtype=np.int32)
# [[1 1 1]
# [1 1 1]]
# 任意の値で埋める
sevens = np.full((2, 2), 7)
# [[7 7]
# [7 7]]
# 未初期化(高速だが値はランダム)
empty = np.empty((1000, 1000))
# 後で全要素を上書きするときに使う
# 既存配列と同じ shape で
ref = np.array([[1, 2], [3, 4]])
zeros_like = np.zeros_like(ref) # shape=(2,2), dtype=int
ones_like = np.ones_like(ref, dtype=float)
full_like = np.full_like(ref, fill_value=99)
数列: arange / linspace
# Python の range() の NumPy 版
a = np.arange(10) # [0 1 2 3 4 5 6 7 8 9]
b = np.arange(2, 10) # [2 3 4 5 6 7 8 9]
c = np.arange(0, 10, 2) # [0 2 4 6 8]
d = np.arange(0, 1, 0.1) # [0. 0.1 0.2 ... 0.9] ※浮動小数では誤差注意
# 区間を等分(端点を含む点数指定)
e = np.linspace(0, 1, 11)
# [0. 0.1 0.2 0.3 ... 1.0] ← 11 個
# 対数スケール
f = np.logspace(0, 3, 4)
# [ 1. 10. 100. 1000.]
arange と linspace の使い分け:
| arange | linspace | |
|---|---|---|
| 引数 | start, stop, step | start, stop, num |
| stop の扱い | 含まない | 含む(endpoint=True デフォルト) |
| 浮動小数精度 | 誤差累積あり | 正確に分割 |
| 用途 | 整数系 | 浮動小数の等分 |
単位行列: eye / identity
# 単位行列(対角が 1、他は 0)
I = np.eye(3)
# [[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]
# 長方形行列も可能
I = np.eye(3, 5)
# [[1. 0. 0. 0. 0.]
# [0. 1. 0. 0. 0.]
# [0. 0. 1. 0. 0.]]
# 対角をずらす
I = np.eye(4, k=1) # 1 つ右上
# [[0. 1. 0. 0.]
# [0. 0. 1. 0.]
# [0. 0. 0. 1.]
# [0. 0. 0. 0.]]
# identity は eye の正方行列版
I = np.identity(4)
dtype(データ型)の指定
| dtype | バイト数 | 範囲・用途 |
|---|---|---|
| int8 | 1 | -128 〜 127 |
| int16 | 2 | -32768 〜 32767 |
| int32 | 4 | ±21 億 |
| int64 | 8 | デフォルト整数 |
| uint8 | 1 | 0-255 (画像によく使う) |
| float16 | 2 | 半精度(機械学習) |
| float32 | 4 | 単精度(GPU で標準) |
| float64 | 8 | デフォルト浮動小数 |
| bool | 1 | True / False |
| complex64 / 128 | 8 / 16 | 複素数 |
# dtype を明示
a = np.array([1, 2, 3], dtype=np.float32)
b = np.zeros(10, dtype=np.uint8) # 画像処理向け
c = np.array([True, False, True], dtype=bool)
d = np.array([1+2j, 3-4j], dtype=complex)
# 型変換
a_int = a.astype(np.int32)
# astype は新しい配列を返す(元は変更されない)
# 文字列も可能(固定長)
s = np.array(['abc', 'defgh'], dtype='U10') # Unicode 最大 10 文字
reshape: 次元変換
# 0-11 の 1 次元配列を 3×4 行列に
a = np.arange(12).reshape(3, 4)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# -1 は自動計算
b = np.arange(12).reshape(2, -1)
# 自動で 6 → shape (2, 6)
c = np.arange(24).reshape(2, 3, 4) # 3D
# shape = (2, 3, 4)
# 1 次元に戻す
d = c.reshape(-1)
# shape = (24,)
ndarray のメモリレイアウト
NumPy 配列は内部的には連続メモリ上の 1 次元バイト列です。多次元はストライド(次元ごとのバイト跨ぎ)で表現します:
a = np.arange(12).reshape(3, 4)
print(a.shape) # (3, 4)
print(a.strides) # (32, 8)
# 32: 行を 1 進めるのに 32 バイト(int64 × 4 列)
# 8: 列を 1 進めるのに 8 バイト
# C-order(行優先、デフォルト)
b = np.arange(12).reshape(3, 4, order='C')
print(b.flags.c_contiguous) # True
# F-order(列優先、Fortran 互換)
c = np.arange(12).reshape(3, 4, order='F')
print(c.flags.f_contiguous) # True
# メモリ消費
print(a.nbytes) # 96 (12 * 8 bytes)
print(a.itemsize) # 8
乱数からの生成
# 新 API(推奨、NumPy 1.17+)
rng = np.random.default_rng(seed=42)
a = rng.random((3, 4)) # [0, 1) の一様分布
b = rng.integers(0, 10, size=(2, 5)) # [0, 10) の整数
c = rng.normal(0, 1, size=100) # 標準正規分布
d = rng.choice([1, 2, 3], size=10) # サンプリング
# 旧 API(非推奨だが現役)
np.random.seed(42)
e = np.random.rand(3, 4)
f = np.random.randint(0, 10, (2, 5))
FAQ
Q: 大きな配列を作るのにメモリ不足
A: np.empty や np.memmap(ディスクマップ)を使う。dtype を float64 → float32 にすれば半分のサイズに。
Q: np.array([1, "a"]) の dtype は
A: 異なる型が混ざると共通の型に昇格。文字列と数値混在では
Q: copy と view の違いは
A: reshape や slice は view(同じメモリを共有)、copy() や flatten() はコピー。
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページ
子ページはありません
同階層のページ
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript base URL 取得方法|window.location.origin と SSR/Node.js 対応
- 4 YouTube Data API v3 エラー一覧|403/400/404 の主要原因と切り分け
- 5 Spring Frameworkのアノテーション一覧
- 6 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- Laravel キャッシュクリア完全ガイド(cache:clear / config:clear / 2026-05-18 07:42:07
- プロジェクトの作成と削除 2026-05-18 07:42:07
- インストール直後にNetbeansが反応しない 2026-05-18 07:42:07
- 動画やチャンネルの検索 2026-05-18 07:42:07
- APIキー取得方法 2026-05-18 07:42:07
- チャンネル情報の取得 2026-05-18 07:42:07
- API 入門 — Web API(REST / GraphQL / gRPC / 2026-05-18 07:42:07
- インストール(eclipseプラグイン) 2026-05-18 07:42:07
- Laravel「Dotenv values containing spaces must be surrounded 2026-05-18 07:42:07
- エラー一覧 2026-05-18 07:42:07
- curl: (51) SSL: certificate subject name '~' does not match 2026-05-18 07:42:07
- インストール方法(Windows版) 2026-05-18 07:42:07
- JSONから配列に変換 2026-05-18 07:42:07
- 処理を一定時間待つ 2026-05-18 07:42:07
- A non well formed numeric value encountered 2026-05-18 07:42:07
コメントを削除してもよろしいでしょうか?