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.31163798 0.31309809 0.69401724 0.77200135 0.61956375]
# generate 2x2 array of random values 
R1 = np.random.random((2, 2))
print(R1)
[[0.00775018 0.3589671 ]
 [0.14735505 0.25663696]]
# generate 4x5 array of random floats between 0-1
R2 = np.random.rand(4,5)
print(R2)
[[0.73906059 0.00608671 0.45925368 0.44963423 0.00543453]
 [0.0874653  0.85097053 0.35967102 0.61604068 0.53840071]
 [0.70945543 0.01814189 0.00231344 0.31122197 0.09760772]
 [0.54054545 0.99882996 0.74258807 0.78583987 0.84821897]]
# generate 6x7 array of random floats between 0-100
R3 = np.random.rand(6,7)*100
print(R3)
[[ 4.97601868 36.89316112 59.7302253  27.89480379 34.17181524 55.17128159
  17.87583079]
 [23.05599471 57.06266056 11.39918508 36.97067385 83.73938511 40.90407246
  89.48226788]
 [18.86615111 58.96207148 81.39123124 94.7752174  81.67399922 22.00848221
  69.79165599]
 [58.46704653 20.1734981   7.86733191 29.86632908 46.49716865 97.8816136
  59.97660017]
 [40.44364129 97.38439205 81.04340967 75.27362888  2.48025779 34.90904959
  14.75394826]
 [25.48339242 59.50979312 40.75748142 38.00203604 56.65945426 84.96912722
  24.99921508]]
# generate 2x3 array of random ints between 0-4
R4 = np.random.randint(5, size=(2,3))
print(R4)
[[4 0 1]
 [2 0 3]]

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.00775018 0.3589671 ]
 [0.14735505 0.25663696]]

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)