In this Python NumPy tutorial, I will explain how to create a Python NumPy array using different methods with the help of some illustrative examples. I will also explain what are the different kinds of NumPy array dimensions in Python.
To create a Python NumPy array we can use methods like array() that converts a Python sequence into an array, np.zeros() and np.ones() for random array of zero and one inside it, the arrange() and linspace() for array with range of numbers, different data type while ensure that the array contains only one kind of data inside it, and reshape() function to create a NumPy array with another array in Python.
NumPy, which stands for Numerical Python, is the foundational package for mathematical computing in Python. One of its most notable features is the powerful numpy.array object, which is a multi-dimensional array that is suitable for mathematical operations. This article will guide you through different ways to create a numpy.array using a variety of functions.
Create a Python NumPy array (Methods)
There are seven different methods in the NumPy library to create an array in Python:
- array() function
- zeros() function
- ones() function
- arrange() function
- linspace() function
- different data type
- reshape() function
Let’s see them one by one using some illustrative examples:
Method 1: Create NumPy array in Python using array()
The array() function is NumPy’s most basic way to create an array in Python. We provide it with a Python list (or other sequence type in Python), and it returns a Python NumPy array containing the same values. Essentially, it converts standard Python sequences into NumPy arrays.
Example: Consider a situation, where we have to convert a standard Python sequence(list, tuple, etc) into a Python NumPy array.
import numpy as np
temperatures = np.array([72, 74, 69, 70, 73, 75, 71])
print('Array:', temperatures)
print('Printing the Datatype to check:', type(temperatures))
Output: Here, the array() function has been used to convert the Python list into a NumPy array, and now, it can be used to perform various calculations.
Array: [72 74 69 70 73 75 71]
Printing the Datatype to check: <class 'numpy.ndarray'>
We can use the array() function to create a Python NumPy array.
Method 2: Create NumPy array of zeros in Python using zeros()
The zeros() function creates an array in Python NumPy where every element is initialized to the value 0. We just provide the desired shape of the array in Python as the argument, and it will produce a NumPy array of that shape filled with zeros.
Example: Suppose we want to create a placeholder in Python NumPy array, that we intend to fill later in the process.
import numpy as np
products_sold = np.zeros(100)
print('Array:', products_sold)
print('Printing the Datatype to check:', type(products_sold))
Output:
Array: [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.]
Printing the Datatype to check: <class 'numpy.ndarray'>
This we can create a Python NumPy array with zeros as placeholders with numpy.zeros() function
Method 3: Create NumPy array of ones in Python
The NumPy ones() function is analogous to np.zeros() in Python. Instead of filling the array in numpy Python with zeros, it fills it with ones.
Example: Suppose we need a Python NumPy array to be initialized with the value 1 for certain mathematical operations or as another kind of placeholder using np.ones() in Python.
import numpy as np
bacteria_count = np.ones(50)
print('Array:', bacteria_count)
print('Printing the Datatype to check:', type(bacteria_count))
Output:
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.
1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
1. 1.]
Printing the Datatype to check: <class 'numpy.ndarray'>
This way, we can use numpy.ones() function to create a Python NumPy array.
Method 4: Create NumPy array with random values using arange()
The arange() function in the NumPy library creates a Python array with values in a range, similar to Python’s built-in range() function. We can provide start, stop, and step values. If we only provide one argument, it will be interpreted as the stop value, starting at zero. This is great for quickly generating sequences of numbers.
Example: Let’s generate a sequence of numbers in the form of a NumPy array in Python.
import numpy as np
grades = np.arange(13)
print('Array:', grades)
print('Printing the Datatype to check:', type(grades))
Output:
Array: [ 0 1 2 3 4 5 6 7 8 9 10 11 12]
Printing the Datatype to check: <class 'numpy.ndarray'>
This way we can use the arange() function to create a Python NumPy array of numbers.
Method 5: Create NumPy array in Python using linspace()
The Python NumPy linspace() function produces an array of evenly spaced numbers over a specified range. We provide it with a start value, an end value, and the number of values we want in between. It will then generate a NumPy array in Python with many evenly spaced values between the start and end values, inclusive.
Example: Suppose, we have to generate a NumPy array in Python that will include many evenly spaced values between two points.
import numpy as np
years = np.linspace(0, 10, 11)
print('Array:', years)
print('Printing the Datatype to check:', type(years))
Output:
Array: [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
Printing the Datatype to check: <class 'numpy.ndarray'>
We can create a Python NumPy array using the linspace() function in the NumPy library.
Method 6: NumPy array creation using different data types
By default, NumPy in Python will determine the most suitable data type for the elements of the Python array. But there are times when we need to be explicit about the data type in Python.
The dtype argument allows us to specify the data type for the array’s elements in NumPy Python. For example, we can create a NumPy array of integers (int), floating-point numbers (float), or more complex data types.
Example: Let’s create a NumPy array of only integers in Python.
import numpy as np
lines_of_code = np.array([200, 150, 400, 100], dtype=np.int32)
print('Array:', lines_of_code)
print('Printing the Datatype to check:', type(lines_of_code))
Output:
Array: [200 150 400 100]
Printing the Datatype to check: <class 'numpy.ndarray'>
This way we can define the data type of the values and can create a Python NumPy array of only integers or, str, etc.
Method 7: Create NumPy array with shape using reshape()
The NumPy reshape() function lets us change the shape (dimensions) of an array in Python without altering its data. For instance, we can transform a flat, one-dimensional NumPy array into a two-dimensional matrix or even higher-dimensional tensors in Python. It’s a powerful tool in data processing, especially when working with matrices and multi-dimensional data.
Example: Create a Python NumPy array using one 1 dimensional array in Python.
import numpy as np
profits = np.array([5, 8, 6, 7, 9, 10, 8, 7, 12, 11, 8, 10, 9, 13, 12, 10, 14, 15, 12, 11])
profits_reshaped = profits.reshape(5, 4)
print('Array:', profits_reshaped)
print('Printing the Datatype to check:', type(profits_reshaped))
Output:
Array: [[ 5 8 6 7]
[ 9 10 8 7]
[12 11 8 10]
[ 9 13 12 10]
[14 15 12 11]]
Printing the Datatype to check: <class 'numpy.ndarray'>
This way we can create a Python NumPy array as a matrix or tensorflow using another array with the reshape() function of NumPy.
Python NumPy array dimensions
A dimension is a direction in which you can vary the specification of an array’s elements. Dimension in an array is one level of array depth. Let us see a few examples of Python NumPy array dimensions.
Case 1: 0-D arrays in NumPy
Let us see how to create a 0-D array in NumPy Python.
- The 0-Dimension arrays in NumPy are scalar and they cannot be accessed via indexing.
- Firstly we will import numpy as np.
- The 0-dimensional arrays consist of only one value inside it.
import numpy as np
my_arr = np.array(50)
print(my_arr)
You can refer to the screenshot below to see the output for 0-D arrays in NumPy Python.
This is how to work with 0-D arrays in NumPy Python.
For full description, Please read: 0 Dimensional Array NumPy in Python
Case 2: 1-D arrays in NumPy
Now, we will see 1-D arrays in NumPy Python.
- 1-D arrays in NumPy Pyhton are one dimension that can be thought of as a list in Python where we can access the elements with the help of indexing.
- Firstly we will import numpy as np.
- The Python numpy array with 0-D arrays as its elements is called a 1-D NumPy array.
import numpy as np
my_arr = np.array([10, 11, 12, 13, 14])
print(my_arr)
You can refer to the screenshot below to see the output for Python 1-D arrays in NumPy.
This is how to work with 1-D arrays in NumPy in Python
Case 3: 2-D arrays in Numpy
Now, we will see how to create a 2-D array in NumPy in Python.
- 2-D arrays in NumPy are two-dimensional arrays that can be distinguished based on the number of square brackets used in Python.
- Firstly we will import numpy as np.
- The array that has 1-D arrays as its elements is called a 2-D NumPy array in Python.
import numpy as np
my_arr = np.array([[11, 12, 13], [14, 15, 16]])
print(my_arr)
You can refer to the screenshot below to see the output for Python 2-D arrays in Numpy.
This is how to work with 2-D arrays in NumPy Python.
For more detailed information, please read: Python NumPy 2D Array
Case 4: 3-D arrays in Numpy
Here, you will see 3-Dimension arrays in NumPy Python
- The Python 3-D arrays in NumPy are the three-dimensional arrays in Python that can have three square brackets with the NumPy array in Python.
- Firstly we will import numpy as np.
- The array that has 2-Dinemsion arrays as its elements is called a Python 3-D array in NumPy.
import numpy as np
my_arr = np.array([[[11, 12, 13], [14, 15, 16]], [[11, 12, 13], [14, 15, 16]]])
print(my_arr)
You can refer to the screenshot below to see the output for Python 3-D arrays in NumPy
This is how to work with 3-D arrays in Numpy Python.
For more information on the 3D arrays in Python NumPy, please read: Python NumPy 3D Array.
In this Python NumPy array tutorial, we learned about how to create a Python NumPy Array using seven different methods such as array(), zeros(), ones(), arrange(), linspace(), different data type, and reshape() functions. and also we have seen what the different Python NumPy array dimensions are.
Now, the choice depends on the requirements of the problems we are facing.
You may like the following Python tutorials:
- Create an empty array in Python
- Convert string to float in Python
- Check if NumPy Array is Empty in Python
- Python NumPy zeros
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.