Python NumPy Create Arrays

There are many methods of creating NumPy arrays. Below, we will go through some of different ways of creating arrays using NumPy

  1. From a list
  2. Using array function
  3. Using Ones function
  4. Using Zeros function
  5. Using arange function
  6. Using linspace function
  7. Using randint function

Convert a Python list to NumPy Array using array function

  1. First of all, we will import NumPy library to our programme by using import
  2. Now we will convert Python list to numpy array using the array(name of list) method

Create a two-dimensional NumPy array using array function

  1. If we pass two lists separated by comma to the NumPy arrays function, it will create a two-dimensional array

  1. If we pass three lists separated by comma to the NumPy arrays function, it will create a three-dimensional array

Create a NumPy array using zeros

  1. NumPy zeros has a function that can create an array filled with zeros. We can specify the total number of values as an argument to this function
  2. We can also use dtype argument for specifying the type of data in array

Create a NumPy array using ones

  1. NumPy ones function can create an array filled with ones. We can specify the total number of values as an argument to this function
  2. We can also use dtype argument for specifying the type of data in array
  3. We can also use shape = (total_rows, total_columns) argument for creating a two dimensional array

Create a NumPy array using arange

  1. NumPy arange function can create an array with a sequence of numbers. The first argument is where to start the values, second argument is where to stop values and last one is for how to jump from first to last value.

The above code will produce this output [ 1 4 7 10 13 16 19]

We can use reshape function to reshape an array into multi dimensions

The above code will produce this output

array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])

We can use reshape function to reshape an array into multi dimensions

The above code will produce this output

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

Another way of doing above is like this

We can use reshape function to reshape an array into three dimensions

The above code will produce this output

array([[[ 1, 2],
[ 3, 4],
[ 5, 6]],

[[ 7, 8],
[ 9, 10],
[11, 12]],

[[13, 14],
[15, 16],
[17, 18]]])

Create a NumPy array using linspace

  1. NumPy linspace function can create an array of specified values between two numbers. We can specify the total number of values as an argument to this function.

The above code will produce this output [ 1.    10.5   20. ]

Create a NumPy array using randint

  1. NumPy randint function can create an array of random integers values between two numbers. We can specify the total number of values as size argument to this function.
  2. randint function is part of random module of NumPy

The above code will produce this output [ 1.    10.5   20. ]

The below code will generate a two dimensional array using randint

The above code will produce this output

[[0 0 0 0 1 1]
[0 0 0 0 0 0]
[0 0 1 0 1 1]
[1 1 1 0 0 0]
[1 1 0 1 0 1]
[0 0 1 1 0 0]
[1 1 0 1 1 1]
[1 1 0 0 0 0]
[0 0 0 1 0 0]
[1 1 1 1 1 1]]

[[0 0 0 0 1 1]
[0 0 0 0 0 0]
[0 0 1 0 1 1]]