4.4. Array Create

4.4.1. Array Declaration

4.4.1.1. 1-dimensional Array

import numpy as np


np.array([1, 2, 3])
import numpy as np


np.array([1.0, 2.0, 3.0])
# array([1., 2., 3.])

np.array([1.1, 2.2, 3.3])
# array([1.1, 2.2, 3.3])

np.array([1, 2, 3], float)
# array([ 1., 2., 3.])

np.array([1, 2, 3], dtype=float)
# array([ 1., 2., 3.])

4.4.1.2. 2-dimensional Array

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

4.4.2. Array str vs repr

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

str(a)
# '[[1 2 3]\n [4 5 6]\n [7 8 9]]'

print(a)
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]

repr(a)
# 'array([[1, 2, 3],\n       [4, 5, 6],\n       [7, 8, 9]])'

a
# array([[1, 2, 3],
#        [4, 5, 6],
#        [7, 8, 9]])

print(repr(a))
# array([[1, 2, 3],
#        [4, 5, 6],
#        [7, 8, 9]])

4.4.3. Array Generation

4.4.3.1. Array from range()

import numpy as np


np.array(range(5))
# array([0, 1, 2, 3, 4])

np.array(range(5), float)
# array([ 0., 1., 2., 3., 4.])
import numpy as np


np.array(range(5, 10))
# array([5, 6, 7, 8, 9])

np.array(range(5, 10), float)
# array([5., 6., 7., 8., 9.])
import numpy as np


np.array(range(5, 10, 2))
# array([5, 7, 9])

np.array(range(5, 10, 2), float)
# array([5., 7., 9.])

4.4.3.2. Array from np.arange()

  • similar to range()

  • array-range

import numpy as np


np.arange(5)
# array([0, 1, 2, 3, 4])

np.arange(5, dtype=float)
# array([0., 1., 2., 3., 4.])

np.arange(5.0)
# array([0., 1., 2., 3., 4.])
import numpy as np


np.arange(5, 10)
# array([5, 6, 7, 8, 9])

np.arange(5, 10, step=2)
# array([5, 7, 9])

np.arange(start=5, stop=10, step=2)
# array([5, 7, 9])

np.arange(start=5, stop=10, step=2, dtype=float)
# array([5., 7., 9.])
import numpy as np


np.arange(0.0, 1.0, 0.1)
# array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])

np.arange(0.0, 1.0, 0.2)
# array([0. , 0.2, 0.4, 0.6, 0.8])

np.arange(0.0, 1.0, 0.3)
# array([0. , 0.3, 0.6, 0.9])

4.4.3.3. Zeros and zeros-like

import numpy as np


np.zeros((2, 3))
# array([[0., 0., 0.],
#       [0., 0., 0.]])

np.zeros(shape=(2, 3))
# array([[0., 0., 0.],
#        [0., 0., 0.]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.zeros_like(a)
# array([[0, 0, 0],
#        [0, 0, 0]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]], float)

np.zeros_like(a)
# array([[0., 0., 0.],
#        [0., 0., 0.]])

4.4.3.4. Ones and ones-like

import numpy as np


np.ones((3, 2))
# array([[1., 1.],
#        [1., 1.],
#        [1., 1.]])

np.ones(shape=(3, 2))
# array([[1., 1.],
#        [1., 1.],
#        [1., 1.]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.ones_like(a)
# array([[1, 1, 1],
#        [1, 1, 1]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]], float)

np.ones_like(a)
# array([[1., 1., 1.],
#        [1., 1., 1.]])

4.4.3.5. Empty and empty-like

  • Garbage from memory

  • Will reuse previous if given shape was already created

import numpy as np


np.empty((3,4))
# array([[ 2.31584178e+077,  1.29073692e-231,  2.96439388e-323, 0.00000000e+000],
#       [-2.32034891e+077,  2.68678047e+154,  2.18018101e-314, 2.18022275e-314],
#       [ 0.00000000e+000,  2.18023445e-314,  1.38338381e-322, 9.03690495e-309]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.empty((2,3))
# array([[1., 2., 3.],
#        [4., 5., 6.]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.empty_like(a)
# array([[1, 2, 3],
#        [4, 5, 6]])

4.4.3.6. Identity

import numpy as np


np.identity(2)
# array([[1., 0.],
#        [0., 1.]])

np.identity(3)
# array([[1., 0., 0.],
#        [0., 1., 0.],
#        [0., 0., 1.]])

np.identity(4, int)
# array([[1, 0, 0, 0],
#        [0, 1, 0, 0],
#        [0, 0, 1, 0],
#        [0, 0, 0, 1]])

4.4.4. Assignments

4.4.4.1. Numpy Create Arange

English
  1. Create a: ndarray with even numbers from 0 to 100 (without 100)

  2. Numbers must be float type

Polish
  1. Stwórz a: ndarray z liczbami parzystymi od 0 do 100 (bez 100)

  2. Liczby muszą być typu float

The whys and wherefores
  • Defining ndarray