3.5. Array Create

3.5.1. Array Declaration

3.5.1.1. 1-dimensional Array

import numpy as np


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

repr(a)
# array([1, 2, 3])

str(a)
# [1 2 3]

print(a)
# [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.])

3.5.1.2. 2-dimensional Array

import numpy as np


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

repr(a)
# array([[1, 2, 3],
#        [4, 5, 6]])

str(a)
# [[1 2 3]
#  [4 5 6]]
import numpy as np


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

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

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

3.5.2. Array Generation

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

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

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

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

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

3.5.2.6. Random and randint

import numpy as np


np.random.rand(3)
# array([0.36477855, 0.3654733 , 0.56707875])

np.random.rand(2, 3)
# array([[0.12840072, 0.14798816, 0.94352656],
#        [0.24807979, 0.6355252 , 0.65943694]])

np.random.rand(3, 2)
# array([[0.65997255, 0.60316048],
#        [0.15598197, 0.30253777],
#        [0.86367738, 0.21519753]])
import numpy as np


np.random.randint(10, size=(2,3))
# array([[9, 5, 0],
#        [7, 0, 6]])

np.random.randint(5, 10, size=(2,3))
# array([[6, 6, 5],
#        [9, 9, 7]])

np.random.randint(low=5, high=10, size=(2,3))
# array([[5, 7, 8],
#        [6, 8, 6]])

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

3.5.3. Assignments

3.5.3.1. Create

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

  2. Numbers must be float type

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

  2. Liczby muszą być typu float

The whys and wherefores
  • Defining ndarray

3.5.3.2. Create Random

English
  1. Set random seed to zero

  2. Create a: ndarray with size 16x16

  3. Structure must contains random integers (0-9)

  4. Print structure

Polish
  1. Ustaw ziarno losowości na zero

  2. Stwórz a: ndarray o rozmiarze 16x16

  3. Struktura musi zawierać losowe liczby (0-9)

  4. Wypisz strukturę

The whys and wherefores
  • Defining ndarray

  • Using np.random.seed()

  • Generating random np.array