Table of Contents

Array Creation Function

# import numpy 
import numpy as np 

Generate arrays using zeros()

  • Returns an array of given shape and type filled with zeros

  • Syntax: np.zeros(shape, dtype)

    • shape - integer or sequence of integers

    • dtype - data type(default: float)

# 1D array of length 3 with all values 0 
Z1 = np.zeros(3)
print(Z1)
[0. 0. 0.]
# 2D array of 3x4 with all values 0 
Z2 = np.zeros((3,4))
print(Z2)
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

Generate arrays using ones()

  • Returns an array of given shape and type filled with ones

  • Syntax: np.ones(shape, dtype)

    • shape - integer or sequence of integers

    • dtype - data type(default: float)

# 1D array of length 3 with all values 1
A1 = np.ones(3)  
print(A1) 
[1. 1. 1.]

Note

  • Rows = 3

  • Columns = 4

# 2D array of 3x4 with all values 1
A2 = np.ones((3,4))
A2
print(A2) 
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]

Generate arrays using arange()

  • Returns equally spaced numbers with in the given range based on step size.

  • Syntax: np.arange(start, stop, step)

    • start- starts of interval range

    • stop - end of interval range ‘

    • step - step size of interval

# not specify start and step 
A1 = np.arange(10)
print(A1)
[0 1 2 3 4 5 6 7 8 9]
# specifying start and step 
A2 = np.arange(start=1, stop=10, step=2)
print(A2)
[1 3 5 7 9]
# another way 
A3 = np.arange(10, 25, 2)
print(A3)
[10 12 14 16 18 20 22 24]

Generate arrays using linspace()

  • Returns equally spaced numbers within the given range based on the sample number.

  • Syntax: np.linspace(start, stop, num, dtype, retstep)

    • start-start of interval range

    • stop-end of the interval range

    • num- number of samples to be generated

    • dtype-type of output array

    • retstep-return the samples, step values

# array of evenly spaced values 0 to 2, here sample size = 9
L1 = np.linspace(0,2,9)
print(L1)
[0.   0.25 0.5  0.75 1.   1.25 1.5  1.75 2.  ]
# Array of 6 evenly divided values from 0 to 100
L2 = np.linspace(0, 100, 6)
print(L2) 
[  0.  20.  40.  60.  80. 100.]
# Array of 1 to 5
L3 = np.linspace(start=1, stop=5, endpoint=True, retstep=False)
print(L3) 
[1.         1.08163265 1.16326531 1.24489796 1.32653061 1.40816327
 1.48979592 1.57142857 1.65306122 1.73469388 1.81632653 1.89795918
 1.97959184 2.06122449 2.14285714 2.2244898  2.30612245 2.3877551
 2.46938776 2.55102041 2.63265306 2.71428571 2.79591837 2.87755102
 2.95918367 3.04081633 3.12244898 3.20408163 3.28571429 3.36734694
 3.44897959 3.53061224 3.6122449  3.69387755 3.7755102  3.85714286
 3.93877551 4.02040816 4.10204082 4.18367347 4.26530612 4.34693878
 4.42857143 4.51020408 4.59183673 4.67346939 4.75510204 4.83673469
 4.91836735 5.        ]
# Array of 1 to 5
L4 = np.linspace(start=1, stop=5, endpoint=True, retstep=True)
print(L4) 
(array([1.        , 1.08163265, 1.16326531, 1.24489796, 1.32653061,
       1.40816327, 1.48979592, 1.57142857, 1.65306122, 1.73469388,
       1.81632653, 1.89795918, 1.97959184, 2.06122449, 2.14285714,
       2.2244898 , 2.30612245, 2.3877551 , 2.46938776, 2.55102041,
       2.63265306, 2.71428571, 2.79591837, 2.87755102, 2.95918367,
       3.04081633, 3.12244898, 3.20408163, 3.28571429, 3.36734694,
       3.44897959, 3.53061224, 3.6122449 , 3.69387755, 3.7755102 ,
       3.85714286, 3.93877551, 4.02040816, 4.10204082, 4.18367347,
       4.26530612, 4.34693878, 4.42857143, 4.51020408, 4.59183673,
       4.67346939, 4.75510204, 4.83673469, 4.91836735, 5.        ]), 0.08163265306122448)

Specifying Endpoint

  • endpoint=True, inlcude 5

  • endpoint=False,exclude 5

Specifying Retstep

  • retstep=False, doesn’t return the step value

  • endpoint=False, returns the samples as well step value

Generate arrays using logspace()

  • Returns equally spaced numbers within the given range based on the log scale.

  • Syntax: np.logspace(start, stop, num, endpoint, base, dtype, retstep)

    • start- start of the sequence

    • stop- end of the sequence

    • num- number of samples to be generated(default: 50)

    • dtype- type of output array

    • retstep- return the samples, step values

    • endpoint - if true, stop is the last sample

    • base - base of the log space(default: 10.0)

# generate an array with 5 samples with base 10.0 
np.logspace(1, 10, num=5, endpoint=True)
array([1.00000000e+01, 1.77827941e+03, 3.16227766e+05, 5.62341325e+07,
       1.00000000e+10])
# generate an array with 5 samples with base 2.0
np.logspace(1, 10, num=5, endpoint=True, base=2.0)
array([   2.        ,    9.51365692,   45.254834  ,  215.2694823 ,
       1024.        ])

Generate constant arrays using full()

  • Return a new array of given shape and type, filled with fill_value.

  • Syntax: np.full(shape,fill_value, dtype)

    • shape - Shape of the new array, e.g., (2, 3) or 2.

    • fill_value - Fill value(scaler).

    • dtype - The desired data-type for the array

# generate 2x2 constant array, constant = 7
C = np.full((2, 2), 7)
print(C)
[[7 7]
 [7 7]]

Creating identity matrix using eye()

  • An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one

  • Syntax: np.eye(N, M, k, dtype)

    • N : Number of rows(int) in the output

    • M : Number of columns in the output. If None, defaults to N.

    • k : Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal

    • dtype: Data-type of the returned array.

# generate 2x2 identity matrix 
I = np.eye(2)
print(I) 
[[1. 0.]
 [0. 1.]]

Generate arrays using random.rand()

  • Returns an array of given shape filled with random values.

  • Syntax: np.random.rand(shape)

    • shape - integer or sequence of integer

# create an array with randomly generated 5 values 
R = np.random.rand(5)
print(R)
[0.45513967 0.70354531 0.02241306 0.77448528 0.962364  ]
# generate 2x2 array of random values 
R1 = np.random.random((2, 2))
print(R1)
[[0.31208389 0.56174622]
 [0.32987422 0.1946306 ]]
# generate 4x5 array of random floats between 0-1
R2 = np.random.rand(4,5)
print(R2)
[[0.87259122 0.56082051 0.85618747 0.03693688 0.17107112]
 [0.7521014  0.47955553 0.97165644 0.88002887 0.50976801]
 [0.86544636 0.41113986 0.15784519 0.40419721 0.48480864]
 [0.60158735 0.36346085 0.15261571 0.97495991 0.72009426]]
# generate 6x7 array of random floats between 0-100
R3 = np.random.rand(6,7)*100
print(R3)
[[99.31415645 15.0196931   3.00271047 63.77281293 71.18275927  2.91037171
   4.74333667]
 [28.06490579  5.76646237 89.04702439 22.5758234  29.11469877 40.09444909
  58.4264296 ]
 [82.2490006  80.79526782 76.99594701  1.9685933  25.07846397 11.38095187
  85.59705495]
 [45.96126458 67.61790942 14.03177335 35.61312041 31.10875915 40.10736233
  60.33119041]
 [90.06343119 65.74858394 20.10555469 86.91517645  7.76671357 60.92705619
  47.78330131]
 [10.1200659  14.61803345 24.60166322  6.45153739  4.5002143  34.95492306
  60.97330553]]

# generate 2x3 array of random ints between 0-4
R4 = np.random.randint(5, size=(2,3))
print(R4)
[[0 2 0]
 [4 1 4]]

Generate empty arrays using empty()

  • Return a new array of given shape and type, without initializing entries.

  • Syntax: np.empty(shape, dtype)

    • shape - integer or tuple of integer

    • dtype - data-type

# generate an empty array 
E1 = np.empty(2) 
print(E1)
[9.9e-324 1.5e-323]
# 2x2 empty array
E2 = np.empty((2, 2)) 
print(E2)
[[0.31208389 0.56174622]
 [0.32987422 0.1946306 ]]

Arrays using specific data type

  • float16

  • float32

  • int8

SEE MORE

  • https://numpy.org/devdocs/user/basics.types.html

# generate an array of floats 
D = np.ones((2, 3, 4), dtype=np.float16)
D
array([[[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]],

       [[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]]], dtype=float16)

References

  • https://numpy.org/

  • https://www.edureka.co/blog/python-numpy-tutorial/

  • https://github.com/enthought/Numpy-Tutorial-SciPyConf-2019

  • Python Machine Learning Cookbook


This notebook was created by Jubayer Hossain | Copyright © 2020, Jubayer Hossain