Numpy arrays are used in python, especially in data analytics, machine learning, and data science to manipulate numerical data. In this article, we will discuss different ways to create a numpy array in Python using examples and working code.
There are various functions to create numpy arrays in Python. Let us discuss them one by one.
List to Numpy Array in Python
We can use the numpy.array()
function to create a numpy array from a python list. The array()
function takes a list as its input argument and returns a numpy array. In this case, the data type of array elements is the same as the data type of the elements in the list.
myList=[1,2,3,4,5]
print("The list is:")
print(myList)
myArr = np.array(myList)
print("The array is:")
print(myArr)
print("The data type of array is:")
print(myArr.dtype)
Output:
The list is:
[1, 2, 3, 4, 5]
The array is:
[1 2 3 4 5]
The data type of array is:
int64
In the above code, we have first created a numpy array using the array()
function. After that, we used the dtype
attribute of the NumPy arrays to obtain the data type of the elements in the array. Here, you can see that a list of integers gives us an array of elements with data type int64
.
You can also explicitly specify the data type of the array elements using the dtype
parameter in the array()
function as shown in the following example.
myList=[1,2,3,4,5]
print("The list is:")
print(myList)
myArr = np.array(myList,dtype="float")
print("The array is:")
print(myArr)
print("The data type of array is:")
print(myArr.dtype)
Output:
The list is:
[1, 2, 3, 4, 5]
The array is:
[1. 2. 3. 4. 5.]
The data type of array is:
float64
In the above code, we have given a list of integers as the input argument. However, the output array contains elements with data type float64
because we have specified that while creating the array.
To create a 2-D numpy array, you can pass a list of lists to the array()
function as shown below.
myList=[[1,2,3,4,5],[6,7,8,9,10]]
print("The list is:")
print(myList)
myArr = np.array(myList,dtype="float")
print("The array is:")
print(myArr)
print("The data type of array is:")
print(myArr.dtype)
Output:
The list is:
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
The array is:
[[ 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 10.]]
The data type of array is:
float64
Create a Numpy Array With Elements of Different Data Types
If the input list contains elements of different but compatible data types, the elements in the numpy array are auto-converted into a broader data type. For instance, if we have a list of floats and ints, the resultant numpy array elements will be of float64 data type. You can observe that in the following example.
myList=[1,3.14,4,5]
print("The list is:")
print(myList)
myArr = np.array(myList)
print("The array is:")
print(myArr)
print("The data type of array is:")
print(myArr.dtype)
Output:
The list is:
[1, 3.14, 4, 5]
The array is:
[1. 3.14 4. 5. ]
The data type of array is:
float64
Here, we have given a list containing integers as well as floating point numbers. Due to this, all the elements of the numpy array have been converted to floating point numbers.
If the input list contains data types such as str and int, the resultant numpy array element will have string data type denoted by <u32 or <u64. It shows that the elements are stored as unicode objects in 4 bytes or 8 bytes respectively.
myList=[1,"Aditya", 3.14,4,5]
print("The list is:")
print(myList)
myArr = np.array(myList)
print("The array is:")
print(myArr)
print("The data type of array is:")
print(myArr.dtype)
Output:
The list is:
[1, 'Aditya', 3.14, 4, 5]
The array is:
['1' 'Aditya' '3.14' '4' '5']
The data type of array is:
<U32
You can also choose to specify the data type of the array elements when the input list elements have different data types. For instance, if you have a list of floats and ints and you want the data type of the array elements to be int, you can specify it in the dtype
parameter as shown below.
myList=[1, 3.14,4,5]
print("The list is:")
print(myList)
myArr = np.array(myList,dtype="int")
print("The array is:")
print(myArr)
print("The data type of array is:")
print(myArr.dtype)
Output:
The list is:
[1, 3.14, 4, 5]
The array is:
[1 3 4 5]
The data type of array is:
int64
Here, you can observe that the list elements with float data type have been converted to int. Hence, 3.14 has been converted to 3.
You can also convert the elements to other data types such as strings as shown below.
myList=[1, 3.14,4,5]
print("The list is:")
print(myList)
myArr = np.array(myList,dtype="str")
print("The array is:")
print(myArr)
print("The data type of array is:")
print(myArr.dtype)
Output:
The list is:
[1, 3.14, 4, 5]
The array is:
['1' '3.14' '4' '5']
The data type of array is:
<U4
While specifying the data type of the array elements, you need to make sure that all the elements in the input list can be converted to the specified data type in the numpy array. If it doesn’t happen, the program will run into an error. For instance, we cannot convert an alphabet into an integer. Therefore, if we specify the target data type for the numpy array to be int for an input list containing strings with alphabets, the program will run into a TypeError exception as shown below.
myList=[1,"Aditya", 3.14,4,5]
print("The list is:")
print(myList)
myArr = np.array(myList,dtype="int")
print("The array is:")
print(myArr)
print("The data type of array is:")
print(myArr.dtype)
Output:
The list is:
[1, 'Aditya', 3.14, 4, 5]
ValueError Traceback (most recent call last)
/tmp/ipykernel_4810/3347589872.py in <module>
2 print("The list is:")
3 print(myList)
----> 4 myArr = np.array(myList,dtype="int")
5 print("The array is:")
6 print(myArr)
ValueError: invalid literal for int() with base 10: 'Aditya'
In this example, we have specified the datatype of the array elements to be integer. However, the input list contains a string that cannot be converted to to an integer. Hence, the program runs into ValueError invalid literal for int() with base 10 error.
Check Properties of a Numpy Array in Python
You can perform many operations on a Numpy array to check its properties. For instance, you can use the ndim
attribute of numpy arrays to check the dimensions of the array as follows.
myList=[1,"Aditya", 3.14,4,5]
print("The list is:")
print(myList)
myArr = np.array(myList,dtype="str")
print("The array is:")
print(myArr)
print("The dimension of array is:")
print(myArr.ndim)
Output:
The list is:
[1, 'Aditya', 3.14, 4, 5]
The array is:
['1' 'Aditya' '3.14' '4' '5']
The dimension of array is:
1
Here, we have created a 1-D array. Hence, its dimension is 1.
For a 2-D list input, the dimension of the array will be 2 as shown below.
myList=[[1,2,3,4,5],[6,7,8,9,10]]
print("The list is:")
print(myList)
myArr = np.array(myList,dtype="str")
print("The array is:")
print(myArr)
print("The dimension of array is:")
print(myArr.ndim)
Output:
The list is:
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
The array is:
[['1' '2' '3' '4' '5']
['6' '7' '8' '9' '10']]
The dimension of array is:
2
To check the datatype of the array elements in the numpy array, you can use the dtype
attribute. The dtype
attribute gives a dtype
object as output. To obtain the name of the data type, you can use dtype.name
attribute as already discussed in the previous sections.
You can also find the shape of the numpy array using the shape
attribute. The shape
attribute, of the numpy array returns a tuple having the number of rows and columns as its first and second element respectively. You can observe this in the following example.
myList=[[1,2,3,4,5],[6,7,8,9,10]]
print("The list is:")
print(myList)
myArr = np.array(myList,dtype="str")
print("The array is:")
print(myArr)
print("The shape of array is:")
print(myArr.shape)
Output:
The list is:
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
The array is:
[['1' '2' '3' '4' '5']
['6' '7' '8' '9' '10']]
The shape of array is:
(2, 5)
In this example, we have created a numpy array with a 2-D list having two internal lists of 5 elements each. Hence, the shape of the numpy array is (2, 5).
Create Numpy Arrays of Zeros, Ones, and Specific Sequences
Using the built-in array operations, we can create different types of numpy arrays. Let us discuss some of them.
Create Numpy Array Containing Ones in Python
You can create numpy arrays containing ones using the ones()
function. To create a 1-D array containing ones using the ones()
function, you need to pass the number of elements required in the array as the input argument. After execution, the ones()
function will return a 1-D numpy array having the desired number of elements as shown below.
myArr = np.ones(5)
print("The array is:")
print(myArr)
Output:
The array is:
[1. 1. 1. 1. 1.]
By default, the datatype of elements in the array is float64. To create an array of integers, you can use the dtype
parameter of the ones()
function to specify the data type of the elements as shown in the following example.
myArr = np.ones(5,dtype="int")
print("The array is:")
print(myArr)
Output:
The array is:
[1 1 1 1 1]
Here, you can see that the array contains integers as its elements and not floating point numbers.
To create 2-D arrays using the ones()
function, you can pass a tuple containing the number of rows and columns in the format (number_of_rows, number_of_columns)
to the ones()
function. After execution, the ones()
function will return the desired array.
myArr = np.ones((2,3),dtype="int")
print("The array is:")
print(myArr)
Output:
The array is:
[[1 1 1]
[1 1 1]]
Again, the default datatype of the elements in the array is float64
. Hence, you can change the data type of the array elements using the dtype
parameter of the ones()
function.
Create Numpy Array Containing Zeros in Python
Just like an array of ones, you can also create numpy arrays containing zeros using the zeros()
function.
To create a 1-D array containing ones using the zeros()
function, you need to pass the number of elements required in the array as the input argument. After execution, the zeros()
function will return a 1-D numpy array having the desired number of zeros as shown below.
myArr = np.zeros(5)
print("The array is:")
print(myArr)
Output:
The array is:
[0. 0. 0. 0. 0.]
By default, the datatype of elements in the array is float64
. To create an array of integers, you can use the dtype
parameter of the zeros()
function to specify the data type of the elements as shown in the following example.
myArr = np.zeros(5,dtype="int")
print("The array is:")
print(myArr)
Output:
The array is:
[0 0 0 0 0]
Here, you can see that the array contains integers as its elements and not the floating point numbers.
To create 2-D arrays using the zeros()
function, you can pass a tuple containing the number of rows and columns in the format (number_of_rows, number_of_columns)
to the zeros()
function. After execution, the zeros()
function will return the desired array with zeros as shown below.
myArr = np.zeros((2,3),dtype="int")
print("The array is:")
print(myArr)
Output:
The array is:
[[0 0 0]
[0 0 0]]
Again, the default datatype of the elements in the array is float64
. Hence, you can change the data type of the array elements using the dtype
parameter of the zeros()
function.
Create Numpy Array With Random Numbers Between 0 and 1
You can use the numpy.random.rand()
function to create numpy arrays with elements ranging from 0 to 1.
To create a 1-D numpy array, you can pass the number of required elements as the input argument to the rand()
function. After execution, the rand()
function returns a numpy array containing the specified number of floating point numbers between 0 and 1.
myArr = np.random.rand(5)
print("The array is:")
print(myArr)
Output:
The array is:
[0.693256 0.26514033 0.86510414 0.52163653 0.1188453 ]
To create a 2-D array of random numbers, you can pass the number of rows as the first input argument and the number of columns in the required array as the second argument. After execution, the rand()
function will return the numpy array with the desired shape as shown below.
myArr = np.random.rand(2,3)
print("The array is:")
print(myArr)
Output:
The array is:
[[0.03166493 0.06408176 0.73167115]
[0.49889714 0.34302884 0.9395251 ]]
Suggested Reading: If you are into machine learning and want to explore more in this area, you can read this article on regression in machine learning. You might also like this article on k-means clustering with a numerical example.
Create Numpy Array With Random Integers
To create a numpy array with random integers within a range, you can use the random.randint()
function. The random.randint()
function has the following syntax.
random.randint(start, end, number_of_elements)
Here,
- The parameter
start
denotes the starting number of a range. - The parameter
end
denotes the last number of a range. - The parameter
number_of_elements
denotes the number of elements in the required array.
By default, the data type of array elements returned by the random.randint()
function is int64
.
myArr = np.random.randint(2,100,5)
print("The array is:")
print(myArr)
Output:
The array is:
[22 10 30 87 96]
You cannot create an array of floating point numbers using the randint()
function by specifying the dtype
parameter. If you try to do so, the program will run into a TypeError
exception.
Create Numpy Array With Elements in a Range in Python
If you want to create a numpy array with the elements within a range, you can use the numpy.arange()
function for that.
To create an array with elements from 0 to N, you can pass N as an input argument to the arange()
function. In the array returned by the arange()
function, you will get numbers only till N-1. This is because N is exclusive in the range.
myArr = np.arange(10)
print("The array is:")
print(myArr)
Output:
The array is:
[0 1 2 3 4 5 6 7 8 9]
Here, we have passed 10 to the arange()
function. Therefore, it has returned an array containing elements from 0 to 9.
To create a numpy array with elements between numbers M and N, you can pass M as the first input argument and N as the second input argument to the arange()
function. After execution, you will get a numpy array containing numbers from M to N-1.
myArr = np.arange(3,10)
print("The array is:")
print(myArr)
Output:
The array is:
[3 4 5 6 7 8 9]
Here, N must be greater than M. Otherwise, you will get an empty array as shown below.
myArr = np.arange(13,10)
print("The array is:")
print(myArr)
Output:
The array is:
[]
You can also decide the difference between two consecutive elements in the array. For this, you can pass the desired difference between the numbers as the third input argument to the arange()
function. After execution, it will return a numpy array having elements in a range and a constant difference between them.
myArr = np.arange(3,10,2)
print("The array is:")
print(myArr)
Output:
The array is:
[3 5 7 9]
If you want to get an array in decreasing order of elements, you can pass a greater number as the first input and a smaller number as the second input argument to the arange()
function. As the third input argument, you need to pass a negative number as the difference between two consecutive elements. In this way, you will get a numpy array with elements in decreasing order as shown in the following example.
myArr = np.arange(23,10,-2)
print("The array is:")
print(myArr)
Output:
The array is:
[23 21 19 17 15 13 11]
By default, the arange()
function returns an array with integers as its elements. To get an array of floating point numbers, you can specify the data type in the dtype
parameter of the arange()
function as shown below.
myArr = np.arange(23,10,-2,dtype="float")
print("The array is:")
print(myArr)
Output:
The array is:
[23. 21. 19. 17. 15. 13. 11.]
Numpy Array of the Desired Number of Elements Within a Range
Instead of deciding the range of the elements in the numpy array, you can also specify the number of elements you want to include in the array that are within a given range. For this, you can use the linspace()
function.
The syntax for linspace()
function is as follows.
linspace(start, end, number_of_elements)
Here,
- The parameter
start
denotes the starting number of a range. - The parameter
end
denotes the last number of a range. - The parameter
number_of_elements
denotes the number of elements in the required array.
By default, the data type of array elements returned by the linspace()
function is float64
.
myArr = np.linspace(2,50,10)
print("The array is:")
print(myArr)
Output:
The array is:
[ 2. 7.33333333 12.66666667 18. 23.33333333 28.66666667
34. 39.33333333 44.66666667 50. ]
You can also get the elements in the array in reverse order by passing a smaller number to the end parameter and a larger number to the start parameter as shown below.
myArr = np.linspace(122,50,10)
print("The array is:")
print(myArr)
Output:
The array is:
[122. 114. 106. 98. 90. 82. 74. 66. 58. 50.]
The linspace()
function returns an array of floating point numbers. However, you can create a numpy array with integer elements by using the dtype
parameter of the linspace()
function. For this, you just need to pass the literal “int
” to the dtype
parameter in the linspace()
method along with other input arguments.
Load a File Into Numpy Array in Python
You can also create a numpy array by loading a text file. For this, you can use the loadtxt()
function.
The loadtxt()
function takes the file name as its first input argument and the datatype of the elements as the input argument to the dtype parameter. After execution, it returns a numpy array as shown below.
File_data = np.loadtxt("1d.txt", dtype=int)
print(File_data)
Output:
[1 2 3 5 6]
If the input file has characters other than spaces and numbers, the program will run into error. You can observe this in the following example.
File_data = np.loadtxt("1d.txt", dtype=int)
print(File_data)
Output:
ValueError Traceback (most recent call last)
/tmp/ipykernel_4810/1616002059.py in <module>
----> 1 File_data = np.loadtxt("1d.txt", dtype=int)
2 print(File_data)
ValueError: invalid literal for int() with base 10: '1,2,3,5,6'
You can create a 2-D numpy array by reading a file having two dimensional data as shown below.
File_data = np.loadtxt("2d.txt", dtype=int)
print(File_data)
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
BY observing the outputs, we can deduce that the loadtxt()
function first reads the file as a string. After that, it splits the file at newlines. Each string after newline is considered to be a new row. Then, it splits each row at spaces to get the individual elements for the numpy array.
Conclusion
In this article, we have discussed different ways to create arrays using the numpy module in Python. To learn more python topics, You can read this article on list comprehension in Python. You might also like this article on how to create a chat application in Python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.