There are many methods of creating NumPy arrays. Below, we will go through some of different ways of creating arrays using NumPy
- From a list
- Using array function
- Using Ones function
- Using Zeros function
- Using arange function
- Using linspace function
- Using randint function
Convert a Python list to NumPy Array using array function
- First of all, we will import NumPy library to our programme by using import
- Now we will convert Python list to numpy array using the array(name of list) method
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# import numpy library as np import numpy as np # Create two lists distances = [10, 20, 30, 40] times = [1.0, 0.9, 0.8, 0.6] # convert python lists to a NumPy array using np.array distances = np.array(distances) times = np.array(times) # This is an n dimensional array type(distances) |
Create a two-dimensional NumPy array using array function
- If we pass two lists separated by comma to the NumPy arrays function, it will create a two-dimensional array. 2D array is a list of list.
1 2 3 |
# if we pass two lists to np.array, it will create a 2-dimensional array two_dim = np.array([[1,2,3,4,5,6], [7,8,9,10,11,12]]) print(two_dim) |
- If we pass three lists separated by comma to the NumPy arrays function, it will create a three-dimensional array. 3D array is a list of list of list
1 2 3 |
# if we pass three lists to np.array, it will create a 3-dimensional array three_dim = np.array([[[1,2,3,4,5,6], [7,8,9,10,11,12], [13,14,15,16,17,18]]]) print(three_dim) |
Create a NumPy array using zeros
- 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
- We can also use dtype argument for specifying the type of data in array
1 2 3 4 5 6 7 8 |
# numpy function for creating an array with zeros zeros_array = np.zeros(10) print(zeros_array) print() # create an array of zeros with data type float using dtype zeros_float_array = np.zeros(10, dtype=float) print(zeros_integer_array) |
Create a NumPy array using ones
- NumPy ones function can create an array filled with ones. We can specify the total number of values as an argument to this function
- We can also use dtype argument for specifying the type of data in array
- We can also use shape = (total_rows, total_columns) argument for creating a two dimensional array
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# numpy function for creating an array with 20 ones ones_array = np.ones(20) print(ones_array) print() # numpy function for creating an array with 20 ones of integer data type ones_array_int = np.ones(20, dtype=int) print(ones_array_int) print() # numpy function for creating a two dimensional array filled with value of ones in 5 rows and 7 columns ones_two_dim = np.ones(shape = (5, 7), dtype=float) print(ones_two_dim) |
Create a NumPy array using arange
- 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.
1 2 3 |
# Arange will create an array filled with a sequence of number excluding the stop arange_array = np.arange(start = 1, stop = 20, step = 3) print(arange_array) |
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
1 2 |
# Create an array of 20 integers and reshape it into 5 rows and 4 columns np.arange(20).reshape(5,4) |
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
1 2 |
# Create an array of 1 to 8 integers and reshape it into 2 rows and 4 columns np.arange(start = 1, stop = 9, step = 1).reshape(2,4) |
The above code will produce this output
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
Another way of doing above is like this
1 |
np.arange(1, 9, 1).reshape(2,4) |
We can use reshape function to reshape an array into three dimensions
1 2 |
# Create an array of 1 to 18 integers and reshape it into 3 arrays of 3 rows and 2 columns np.arange(1, 19, 1).reshape(3,3,2) |
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
- 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.
1 2 3 |
# create an array of three values evenly spaced between number 1 and number 20 including 20 linspace_array = np.linspace(1, 20, 3) print(linspace_array) |
The above code will produce this output [ 1. 10.5 20. ]
Create a NumPy array using randint
- 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.
- randint function is part of random module of NumPy
1 2 3 4 5 6 7 8 |
# use a function randint that comes in the numpy's module random rand_array1 = np.random.randint(low = 0, high = 2, size = 10) print(rand_array1) print() # Another way of using randint function rand_array2 = np.random.randint(0, 2, 10) print(rand_array2) |
The above code will produce this output [ 1. 10.5 20. ]
The below code will generate a two dimensional array using randint
1 2 3 4 |
# using randint, generate an array with 100 rows and 10 columns of numbers from 0 to 1. rand_array = np.random.randint(0, 2, size = (10, 6)) print(rand_array) print() |
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]]