Skip to main content

NumPy Array Creation


In this tutorial you will learn:

  • NumPy Array
  • Dimensions of NumPy Array
  • Common Functions of NumPy Array

NumPy Array

Array in NumPy is a data structure that is similar to Python lists but it's a lot more powerful since it allows us to manage N number of dimensions which helps us in making different mathematical calculations. We can define same type of elements in a NumPy array. The elements of a NumPy array are actually dtype objects ( data-type objects) and the array object in NumPy itself is called ndarray. We can use simple array function to create an a NumPy ndarray object. Let’s take a look at an example that creates NumPy array object and print the contents.

  1. import numpy as np
  2. nparr = np.array(["Python", "Test", "Programming"])
  3. print("** Numpy Array Creation **")
  4. print("Content of NumPy array: ",nparr)

Dimensions of NumPy Array

One of the biggest advantages of NumPy array is that it allows us to create arrays that can have N-Dimensions. It means that we can have a very deeply nested array. Let’s have a look at examples in which we will create arrays that are 2 level, 3 level and 4 levels nested.

Example

In this example we are creating an array which is 2-Dimensional. For that we write both arrays in a NumPy array function separated by a comma.

  1. nparr2 = np.array([[8,9,5], [5,4,7], [7,7,9], [4,4,4]])
  2. print("\n\nContents of 2-D NumPy array: \n",nparr2)

In this example we are creating 2-D array by creating an array within an array.

  1. nparr2 = np.array([[8,9,5], [5,4,7], [7,7,9], [4,4,4]])
  2. print("\n\nContents of 3-D NumPy array: \n",nparr2)

Here we are creating 3-D array by creating an array within an array up to three levels.

  1. nparr3 = np.array([[[8,9,5,5], [4,4,4,4]], [[7,6,6,3], [7,7,9,1]]])
  2. print("\n\nContents of 4-D NumPy array: \n",nparr3)

Common Functions of NumPy Array

Now let’s have a look at the usage of some common functions of NumPy. NumPy gives us functions that can fill the array with ones, zeros and random numbers. These functions are simple yet very powerful while developing Machine Learning algorithms in Python.

  1. print("\n\nArray full of 4" ,np.full((3,3),4))
  2. print("\nRandom number array 3 rows 4cols" ,np.random.random((3,4)))
  3. print("\nCreate Array of ones" ,np.ones((2,4)))
  4. print("\nCreate Array of zeros" ,np.zeros((3,3),dtype=np.int16))

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.