Skip to content

Instantly share code, notes, and snippets.

@801YutaKa108
Created April 27, 2020 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 801YutaKa108/bb7b662d73dea352a15130e7a5ea9ca2 to your computer and use it in GitHub Desktop.
Save 801YutaKa108/bb7b662d73dea352a15130e7a5ea9ca2 to your computer and use it in GitHub Desktop.
import numpy as np
# zeros(shape)
a = np.zeros(5)
# a → array([0., 0., 0., 0., 0.])
A = np.zeros((2, 3))
# A → array([[0., 0., 0.],
# [0., 0., 0.]])
my_shape = (2, 2)
B = np.zeros(my_shape)
# B → array([[0., 0.],
# [0., 0.]])
# zeros()悪い例
C = np.zeros(2, 3)
# TypeError: data type not understood
# ones(shape)
a = np.ones(5)
# a → array([1., 1., 1., 1., 1.])
A = np.ones((2, 3))
# A → array([[1., 1., 1.],
# [1., 1., 1.]])
my_shape = (2, 2)
B = np.ones(my_shape)
# B → array([[1., 1.],
# [1., 1.]])
# ones()悪い例
C = np.ones(2, 3)
# TypeError: data type not understood
# full(shape)
a = np.full(5, 3)
# a → array([3, 3, 3, 3, 3])
A = np.full((2, 3), 0.1)
# A → array([[0.1, 0.1, 0.1],
# [0.1, 0.1, 0.1]])
my_shape = (2, 2)
B = np.full(my_shape, -2)
# B → array([[-2, -2],
# [-2, -2]])
# ones vs full
val = 0.1
X = np.ones(3)*val
# X → array([0.1, 0.1, 0.1])
# 2.4 µs ± 21.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Y = np.full(3, val)
# Y → array([0.1, 0.1, 0.1])
# 1.85 µs ± 36.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
# full()悪い例
C = np.ones(2, 3, 0.5)
# TypeError: data type not understood
# empty(shape)
a = np.empty(3)
# a → array([6.95216240e-310, 1.07193797e-311, 6.95218871e-310])
A = np.empty((2, 2))
# A → array([[4.67296746e-307, 1.69121096e-306],
# [7.56597770e-307, 1.89146896e-307]])
# zerso vs empty
X = np.zeros(3)
# X → array([0., 0., 0.])
# 570 ns ± 17.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Y = np.empty(3)
# Y → array([ 6.95216299e-310, 5.55977121e-312, -0.00000000e+000])
# 555 ns ± 0.951 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
# zeros_like
A = np.zeros((2,2))
# A → array([[0., 0.],
# [0., 0.]])
B = np.zeros_like(A)
# B → array([[0., 0.],
# [0., 0.]])
# ones_like
A = np.zeros((2,2))
# A → array([[0., 0.],
# [0., 0.]])
B = np.ones_like(A)
# B → array([[1., 1.],
# [1., 1.]])
# full_like
A = np.zeros((2,2))
# A → array([[0., 0.],
# [0., 0.]])
B = np.full_like(A, 0.5)
# B → array([[0.5, 0.5],
# [0.5, 0.5]])
# empty_like
A = np.zeros((2,2))
# A → array([[0., 0.],
# [0., 0.]])
B = np.empty_like(A)
# B → array([[2.23754644e-312, 2.05210913e-307],
# [3.32653424e-111, 1.13477778e+118]])
# 真偽値の配列
mask = np.zeros((2,2), dtype=bool)
# mask → array([[False, False],
# [False, False]])
mask = np.ones((2,2), dtype=bool)
# mask → array([[ True, True],
# [ True, True]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment