Magicode

# Numpy の作成方法

## numpy の作成方法

import numpy as np
np.random.seed(seed=42)

### 0 埋め np.zeros

a = np.zeros(2)
print(f"a.shape = {a.shape} \na = {a}\n")

b = np.zeros((2, 3))
print(f"b.shape = {b.shape} \nb =\n{b}\n")

c = np.zeros((2, 3, 4))
print(f"c.shape = {c.shape} \nc =\n{c}\n")

a.shape = (2,) a = [0. 0.] b.shape = (2, 3) b = [[0. 0. 0.] [0. 0. 0.]] c.shape = (2, 3, 4) c = [[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]]

### 1埋め np.ones

a = np.ones(2)
print(f"a.shape = {a.shape} \na = {a}\n")

b = np.ones((2, 3))
print(f"b.shape = {b.shape} \nb =\n{b}\n")

c = np.ones((2, 3, 4))
print(f"c.shape = {c.shape} \nc =\n{c}\n")

a.shape = (2,) a = [1. 1.] b.shape = (2, 3) b = [[1. 1. 1.] [1. 1. 1.]] c.shape = (2, 3, 4) c = [[[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.full

a = np.full(2, 3.14)
print(f"a.shape = {a.shape} \na = {a}\n")

b = np.full((2, 3), 3.14)
print(f"b.shape = {b.shape} \nb =\n{b}\n")

c = np.full((2, 3, 4), 3.14)
print(f"c.shape = {c.shape} \nc =\n{c}\n")

a.shape = (2,) a = [3.14 3.14] b.shape = (2, 3) b = [[3.14 3.14 3.14] [3.14 3.14 3.14]] c.shape = (2, 3, 4) c = [[[3.14 3.14 3.14 3.14] [3.14 3.14 3.14 3.14] [3.14 3.14 3.14 3.14]] [[3.14 3.14 3.14 3.14] [3.14 3.14 3.14 3.14] [3.14 3.14 3.14 3.14]]]

### 一様分布（0.0以上、1.0未満）の一様乱数で埋める random.rand

a = np.random.rand(2)
print(f"a.shape = {a.shape} \na = {a}\n")

b = np.random.rand(2, 3)
print(f"b.shape = {b.shape} \nb =\n{b}\n")

c = np.random.rand(2, 3, 4)
print(f"c.shape = {c.shape} \nc =\n{c}\n")

a.shape = (2,) a = [0.37454012 0.95071431] b.shape = (2, 3) b = [[0.73199394 0.59865848 0.15601864] [0.15599452 0.05808361 0.86617615]] c.shape = (2, 3, 4) c = [[[0.60111501 0.70807258 0.02058449 0.96990985] [0.83244264 0.21233911 0.18182497 0.18340451] [0.30424224 0.52475643 0.43194502 0.29122914]] [[0.61185289 0.13949386 0.29214465 0.36636184] [0.45606998 0.78517596 0.19967378 0.51423444] [0.59241457 0.04645041 0.60754485 0.17052412]]]
a = np.random.random(2)
print(f"a.shape = {a.shape} \na = {a}\n")

b = np.random.rand(2, 3)
print(f"b.shape = {b.shape} \nb =\n{b}\n")

c = np.random.random([2, 3, 4])
print(f"c.shape = {c.shape} \nc =\n{c}\n")

a.shape = (2,) a = [0.06505159 0.94888554] b.shape = (2, 3) b = [[0.96563203 0.80839735 0.30461377] [0.09767211 0.68423303 0.44015249]] c.shape = (2, 3, 4) c = [[[0.12203823 0.49517691 0.03438852 0.9093204 ] [0.25877998 0.66252228 0.31171108 0.52006802] [0.54671028 0.18485446 0.96958463 0.77513282]] [[0.93949894 0.89482735 0.59789998 0.92187424] [0.0884925 0.19598286 0.04522729 0.32533033] [0.38867729 0.27134903 0.82873751 0.35675333]]]

### 一様分布（任意の範囲の整数）の乱数で埋める random.randint

# 0 以上 10 未満
a = np.random.randint(0, 10, (2))
print(f"a.shape = {a.shape} \na = {a}\n")

b = np.random.randint(0, 10, (2, 3))
print(f"b.shape = {b.shape} \nb =\n{b}\n")

c = np.random.randint(0, 10, (2, 3, 4))
print(f"c.shape = {c.shape} \nc =\n{c}\n")

a.shape = (2,) a = [8 0] b.shape = (2, 3) b = [[8 6 8] [7 0 7]] c.shape = (2, 3, 4) c = [[[7 2 0 7] [2 2 0 4] [9 6 9 8]] [[6 8 7 1] [0 6 6 7] [4 2 7 5]]]

### 連番で埋める np.arange

a = np.arange(10)
print(f"a.shape = {a.shape} \na = {a}\n")

b = np.arange(0, 20, 2)
print(f"b.shape = {b.shape} \nb =\n{b}\n")

c = np.arange(1, 10).reshape((3, 3))
print(f"c.shape = {c.shape} \nc =\n{c}\n")

a.shape = (10,) a = [0 1 2 3 4 5 6 7 8 9] b.shape = (10,) b = [ 0 2 4 6 8 10 12 14 16 18] c.shape = (3, 3) c = [[1 2 3] [4 5 6] [7 8 9]]