Create Array in Python With NumPy Python Library

Array Creation in NumPy.

We will create arrays in different ways in the Python library NumPy and understand the creation process.



1. np.array()

It will help us to create a multi-dimensional or single-dimensional array, this is the simple way to create an array using NumPy, we can consider it primary.

Structure.

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

Output.

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


2. np.zeros()

np.zeros() takes the array dimensions as parameters and returns the value 0 for each.

Structure.

arr02 = np.zeros((5, 2))

Output.

array([[0., 0.],
       [0., 0.],
       [0., 0.],
       [0., 0.],
       [0., 0.]])


3. np.arange()

np.arange() takes three parameters. The first parameter specifies the starting point, the middle parameter specifies the ending point, and the last parameter specifies the space or gap between the numbers.

Structure.

arr03 = np.arange(5, 51, 5)

  • Here we are printing a table of five where we set the starting point to 5, if we set it to 0 then obviously it will start from zero, and like [0, 5, 10, 15, ...] will be printed. Which doesn't look very appropriate in a math table.
  • Then the middle value is 51 because this parameter checks that the value is less than, not equal. ie, if we write 50 instead of 51, the array will stop at 45 because 50 is not less than 50, but if we write values between 51 - 55, it will be perfect because of the difference of 5 between each value.

Output.

array([ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50])

  • If we want to add 55 to the array, we need to set the middle parameter between 56 - 60. Then it will be like this.

Structure.

arr03 = np.arange(5, 58, 5)

Output.

array([ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55])


4. np.linspace()

np.linspace() also takes three parameters, the first for start, the second for stop, and the third is the length. But its calculation is not the same as np.arange(), it divides the second parameter with the last parameter and keeps adding the last value of the array with the new value until it gets the value of what we have given to stop.

Structure.

arr04 = np.linspace(start = 2, stop = 20, num = 10)

  • Here we are printing a table of 2 where we have set the starting point 2, ending point 20, and length (num) 10. Now it will calculate like this.
  • First, it will divide 20 by 10 20/10 and print 2, then again divide 20 by 10 20/10 but this time it will add the last value which is 2 thus 20/10+2=4 and will do the same again and this time adds 4 thus 20/10+4=6 and it will continue to perform until the value is 20.

Output.

array([ 2.,  4.,  6.,  8., 10., 12., 14., 16., 18., 20.])

  • Sometimes it shows value in float so to fix that you can set dtype=int then it will be perfect but sometimes we need float also.


5. np.empty()

np.empty() takes array dimensions as parameters and prints random values in it. This will be more useful when you want an array immediately.

Structure.

arr05 = np.empty((2, 3, 3), dtype=int)

  • An integer document type would be a more comfortable choice for this.

Output.

array([[[4128860, 6029375, 3801155],
        [5570652, 6619251, 7536754],
        [4718684, 5898325, 4784193]],

       [[8257606, 6029361, 7340097],
        [4456560, 7602273, 6029409],
        [7274572, 6357091,     108]]])


6. np.empty_like()

np.empty_like() takes an array variable as a parameter and creates another array with the same structure with random values.

Structure.

arr06 = np.empty_like(arr01)

Output.

  • If arr01 looks like this.

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

  • So this will also look the same but with random values. and it will print like this in the output

array([[        0,         0, 424940768],
       [      528, 424940192,       528],
       [424940384,       528,      6258]])


7. np.identity

np.identity() takes only 1 parameter for the length and creates an array like the identity matrix.

Structure.

arr07 = np.identity(7)

Output.

array([[1., 0., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 0., 0., 1.]])

Post a Comment

1 Comments

  1. Testing cloud computing course:
    AWS Certification
    https://www.youtube.com/playlist?list=PL6XT0grm_TfgtwtwUit305qS-HhDvb4du

    GCP TG
    https://www.youtube.com/playlist?list=PLBGx66SQNZ8YWRUw6yicKtD4AIpUl_YiJ

    ReplyDelete

© Copyright 2024 & 2025 - Team Krope - All right reserved