[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]

table of contents

  1. [Input data setting](# step1-Input data setting)
  2. [Data Manipulation / Algorithm](# step2-Data Manipulation--Algorithm)
  3. [Output data setting](# step3-Output data setting)

Step1 Input data setting

1. Basic operation

1-1. Easy definition method

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]

1-2. Element access [number of elements: 1]

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

1-3. Element access [Number of elements: Multiple]

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]

1-4. Array Length

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

1-5. Array Size

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)

1-6. Dimension Number

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

1-7. Element Number

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

1-8. Storage type (Data Type)

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'>

1-9. Element Data Type

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

1-10. Axis Delete [Axis Item Number, Axis Direction](Axis Delete)

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]]

1-11. Array concatenation [Method 1]

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]]

1-12. Array concatenation [Method 2]

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]]

1-13. Array concatenation [Method 3]

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]]

1-14. Array concatenation [Array generation by element concatenation]

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

1-15. Array stack

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

1-16. Array partitioning [Method 1]

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]]

1-16. Array partitioning [Method 2]

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]]

1-17. Array division [Array generation by element division]

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]]]

2. Data definition

2-1. Zero Array

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]]

2-2. Unit Array [Method 1](Identity Array)

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.]]

2-3. Unit Array [Method 2](Identity Array)

Unit array[Offset: None]


import numpy as np
identity_ndarray = np.identity(3)
print("identity ndarray: ",identity_ndarray)

2-3. Empty Array

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.]]

2-2. Specified Value Array

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.]]

2-4. Arithmetic Sequence List

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

[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python Numerical Library NumPy
Introduction to Python language
Introduction to OpenCV (python)-(2)
Introduction to Python Django (2) Win
Introduction to serial communication [Python]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Introduction to Python numpy pandas matplotlib (~ towards B3 ~ part2)
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Subscript access to python numpy array
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Python Tutorial] An Easy Introduction to Python
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Python] How to use class in Python?
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement
Introduction to Discrete Event Simulation Using Python # 1
[Introduction to Udemy Python3 + Application] 13. Character method
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
A super introduction to Python bit operations
[Introduction to Udemy Python 3 + Application] 10. Numerical values
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Web-WF Python Tornado Part 3 (Introduction to Openpyexcel)
[Introduction to Udemy Python3 + Application] 21. Tuple type
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Introduction to Udemy Python3 + Application] 41. Input function
[Introduction to Python] Let's use foreach with Python
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Udemy Python3 + Application] 17. List operation
[Introduction to Udemy Python3 + Application] 65. Exception handling
[Python] Introduction to CNN with Pytorch MNIST
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)