One-dimensional array
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d: ", sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5]
A two-dimensional array
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d: ", sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5]
One-dimensional array
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d[0]: ", sample_ndarray_1d[0])
print("sample_ndarray_1d[1]: ", sample_ndarray_1d[1])
print("sample_ndarray_1d[2]: ", sample_ndarray_1d[2])
print("sample_ndarray_1d[3]: ", sample_ndarray_1d[3])
print("sample_ndarray_1d[4]: ", sample_ndarray_1d[4])
print("sample_ndarray_1d[-1]: ", sample_ndarray_1d[-1])
sample_ndarray_1d[0]: 1
sample_ndarray_1d[1]: 2
sample_ndarray_1d[2]: 3
sample_ndarray_1d[3]: 4
sample_ndarray_1d[4]: 5
sample_ndarray_1d[-1]: 5
A two-dimensional array
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d[0,0]: ", sample_ndarray_2d[0,0])
print("sample_ndarray_2d[0,1]: ", sample_ndarray_2d[0,1])
print("sample_ndarray_2d[0,2]: ", sample_ndarray_2d[0,2])
print("sample_ndarray_2d[0,3]: ", sample_ndarray_2d[0,3])
print("sample_ndarray_2d[0,4]: ", sample_ndarray_2d[0,4])
print("sample_ndarray_2d[1,0]: ", sample_ndarray_2d[1,0])
print("sample_ndarray_2d[1,1]: ", sample_ndarray_2d[1,1])
print("sample_ndarray_2d[1,2]: ", sample_ndarray_2d[1,2])
print("sample_ndarray_2d[1,3]: ", sample_ndarray_2d[1,3])
print("sample_ndarray_2d[1,4]: ", sample_ndarray_2d[1,4])
sample_ndarray_2d[0,0]: 1
sample_ndarray_2d[0,1]: 2
sample_ndarray_2d[0,2]: 3
sample_ndarray_2d[0,3]: 4
sample_ndarray_2d[0,4]: 5
sample_ndarray_2d[1,0]: 6
sample_ndarray_2d[1,1]: 7
sample_ndarray_2d[1,2]: 8
sample_ndarray_2d[1,3]: 9
sample_ndarray_2d[1,4]: 10
One-dimensional array
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d: ", sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5]
start_id = 1
end_id = 3
print("Item numbers "1" to "3":",sample_ndarray_1d[start_id:end_id+1])
print("Item number "First" to "3":",sample_ndarray_1d[:end_id+1])
print("Item number "1" to "last":",sample_ndarray_1d[start_id:])
print("All elements:",sample_ndarray_1d[:])
Item numbers "1" to "3":[2 3 4]
Item number "First" to "3":[1 2 3 4]
Item number "1" to "last":[2 3 4 5]
All elements:[1 2 3 4 5]
One-dimensional array
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("Array length:",len(sample_ndarray_1d))
Array length: 5
A two-dimensional array
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("Parent list length:",len(sample_ndarray_2d)) ###Parent list
print("First child list length:",len(sample_ndarray_2d[0])) ###First child list
Parent list length: 2
First child list length: 5
One-dimensional array
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("Array size:",sample_ndarray_1d.shape)
Array size:(5,)
A two-dimensional array
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("Array size:",sample_ndarray_2d.shape)
Array size:(2, 5)
One-dimensional array
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("Number of dimensions:",sample_ndarray_1d.ndim)
Number of dimensions: 1
A two-dimensional array
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("Number of dimensions:",sample_ndarray_2d.ndim)
Number of dimensions: 2
One-dimensional array
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("Element count:",sample_ndarray_1d.size)
Number of elements: 5
A two-dimensional array
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("Element count:",sample_ndarray_2d.size)
Number of elements: 10
One-dimensional array
import numpy as np
sample_ndarray = np.array([1,2,3,4,5])
print("Storage type:",type(sample_ndarray))
Storage type:<class 'numpy.ndarray'>
One-dimensional array
import numpy as np
sample_ndarray = np.array([1,2,3,4,5])
print("Element data type:",sample_ndarray.dtype)
Element data type: int32
One-dimensional array
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d: ",sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5]
deleted_ndarray_1d = np.delete(sample_ndarray_1d,2,0)
print("deleted_ndarray_1d: ",deleted_ndarray_1d)
deleted_ndarray_1d: [1 2 4 5]
A two-dimensional array[Delete: line]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d: ", sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
deleted_ndarray_2d = np.delete(sample_ndarray_2d,0,0)
print("deleted_ndarray_2d: ",deleted_ndarray_2d)
deleted_ndarray_2d: [[ 6 7 8 9 10]]
A two-dimensional array[Delete: Column]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d: ", sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
deleted_ndarray_2d = np.delete(sample_ndarray_2d,2,1)
print("deleted_ndarray_2d: ",deleted_ndarray_2d)
deleted_ndarray_2d: [[ 1 2 4 5]
[ 6 7 9 10]]
One-dimensional array
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.concatenate((A,B),axis=0)
print("joint ndarray (Horizontal): ", C)
joint ndarray (Horizontal): [1 2 3 4 5 6]
A two-dimensional array
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.concatenate((A,B),axis=0)
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
D = np.concatenate((A,B),axis=1)
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
One-dimensional array
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.block([[A,B]])
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[1 2 3]
[4 5 6]]
D = np.block([[A],[B]])
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [1 2 3 4 5 6]
A two-dimensional array
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.block([[A],[B]])
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
D = np.block([[A,B]])
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
One-dimensional array
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.vstack((A,B))
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[1 2 3]
[4 5 6]]
D = np.hstack((A,B))
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [1 2 3 4 5 6]
A two-dimensional array
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.vstack((A,B))
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
D = np.hstack((A,B))
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
A two-dimensional array
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.dstack((A,B))
print("stack ndarray: ", C)
stack ndarray: [[[ 1 7]
[ 2 8]
[ 3 9]]
[[ 4 10]
[ 5 11]
[ 6 12]]]
print("Array size:",C.shape)
print("Number of dimensions:",C.ndim)
Array size:(2, 3, 2)
Number of dimensions: 3
A two-dimensional array
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.stack((A,B))
print("stack ndarray: ", C)
stack ndarray: [[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
print("Array size:",C.shape)
print("Number of dimensions:",C.ndim)
Array size:(2, 2, 3)
Number of dimensions: 3
One-dimensional array
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5,6])
print("sample_ndarray_1d: ",sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5 6]
split_ndarray_1d = np.split(sample_ndarray_1d,2)
print("split_ndarray_1d[0]: ",split_ndarray_1d[0])
print("split_ndarray_1d[1]: ",split_ndarray_1d[1])
split_ndarray_1d[0]: [1 2 3]
split_ndarray_1d[1]: [4 5 6]
A two-dimensional array[Split: line]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.split(sample_ndarray_2d,2,axis=0)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3 4 5 6]]
split_ndarray_2d[1]: [[ 7 8 9 10 11 12]]
A two-dimensional array[Split: Column]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.split(sample_ndarray_2d,2,axis=1)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3]
[7 8 9]]
split_ndarray_2d[1]: [[ 4 5 6]
[10 11 12]]
A two-dimensional array[Split: line]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.vsplit(sample_ndarray_2d,2)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3 4 5 6]]
split_ndarray_2d[1]: [[ 7 8 9 10 11 12]]
A two-dimensional array[Split: Column]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.hsplit(sample_ndarray_2d,2)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3]
[7 8 9]]
split_ndarray_2d[1]: [[ 4 5 6]
[10 11 12]]
3D array
import numpy as np
sample_ndarray_3d = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])
print("sample_ndarray_3d: ",sample_ndarray_3d)
sample_ndarray_3d: [[[1 2]
[3 4]]
[[5 6]
[7 8]]]
split_ndarray_3d = np.dsplit(sample_ndarray_3d,2)
print("split_ndarray_3d[0]: ",split_ndarray_3d[0])
print("split_ndarray_3d[1]: ",split_ndarray_3d[1])
split_ndarray_3d[0]: [[[1]
[3]]
[[5]
[7]]]
split_ndarray_3d[1]: [[[2]
[4]]
[[6]
[8]]]
One-dimensional array
import numpy as np
zero_ndarray_1d = np.zeros(5)
print("zero_ndarray_1d: ",zero_ndarray_1d)
zero_ndarray_1d: [0. 0. 0. 0. 0.]
A two-dimensional array
import numpy as np
zero_ndarray_2d = np.zeros((2,3))
print("zero_ndarray_2d: ",zero_ndarray_2d)
zero_ndarray_2d: [[0. 0. 0.]
[0. 0. 0.]]
Array size copy
import numpy as np
sample_ndarray = np.array([[1,2,3,4,5],[6,7,8,9,10]])
zero_ndarray = np.zeros_like(sample_ndarray)
print("zero ndarray: ",zero_ndarray)
zero ndarray: [[0 0 0 0 0]
[0 0 0 0 0]]
Unit array[Offset: None]
import numpy as np
identity_ndarray = np.eye(3)
print("identity ndarray: ",identity_ndarray)
identity ndarray: [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Unit array[Offset: Yes]
import numpy as np
offset = 1
identity_ndarray = np.eye(3,k=offset)
print("identity ndarray: ",identity_ndarray)
identity ndarray: [[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
import numpy as np
offset = -1
identity_ndarray = np.eye(3,k=offset)
print("identity ndarray: ",identity_ndarray)
identity ndarray: [[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]]
Unit array[Offset: None]
import numpy as np
identity_ndarray = np.identity(3)
print("identity ndarray: ",identity_ndarray)
One-dimensional array
import numpy as np
empty_ndarray_1d = np.empty(5)
print("empty_ndarray_1d: ",empty_ndarray_1d)
empty_ndarray_1d: [4.94065646e-324 4.94065646e-324 0.00000000e+000 0.00000000e+000
1.32373351e+079]
A two-dimensional array
import numpy as np
empty_ndarray_2d = np.empty((2,3))
print("empty_ndarray_2d: ",empty_ndarray_2d)
empty_ndarray_2d: [[0. 0. 0.]
[0. 0. 0.]]
One-dimensional array
import numpy as np
specified_value = 10
array_size = 5
specified_value_ndarray = np.empty(array_size)
specified_value_ndarray.fill(specified_value)
print("specified_value_ndarray: ",specified_value_ndarray)
specified_value_ndarray: [10. 10. 10. 10. 10.]
A two-dimensional array
import numpy as np
specified_value = 3
array_size = (2,3)
specified_value_ndarray = np.empty(array_size)
specified_value_ndarray.fill(specified_value)
print("specified_value_ndarray: ",specified_value_ndarray)
specified_value_ndarray: [[3. 3. 3.]
[3. 3. 3.]]
One-dimensional array
start_num = 1
end_num = 10
difference = 1
arithmetic_sequence_ndarray = np.array(range(start_num, end_num + 1, difference))
print("arithmetic sequence ndarray: ",arithmetic_sequence_ndarray)
Recommended Posts