This tutorial explains how Numpy arrays can be created using different functions. Since n-Dimensional arrays play a very important role in machine learning and data science, you might not want to miss this tutorial.
By the end of this tutorial, you will be able to create Numpy arrays using different approaches on your own. Let's get started!
We saw in our previous post - Beginners Guide to Numpy, how to create Numpy arrays and how powerful they are. Let us dive a bit deeper into the Numpy array creation part with more examples in this tutorial
Let us list ways of creating Numpy arrays one by one:
1. Intuitive approach: Using numpy.array()
:
Numpy arrays can be created very easily by passing a list to the function numpy.array()
. The python list when passed to this function gives us a Numpy array.
First of all, let's import our favorite library Numpy:
# why to write numpy again & again,
# use the power of 'as' keyword, now you can simply write 'np'
# instead of 'numpy'
import numpy as np
Now, first of all, let us make a python list li
# making a python list li
li = [8,5,6,9,4,2]
Now to create Numpy array using this list, use the code below,
# creating array out of it
numpy_array = np.array(li)
print(numpy_array)
We can also pass a list manually without explicitly defining it.
numpy_array = np.array([5,7,4,1,5,6])
Other arguments which are optional can also be included while creating the Numpy arrays. One of the optional parameter, that you might find useful is:
dtype
: This argument specifies the data-type of the array being created. Since, unlike lists, all the elements in the Numpy array are of the same data-type. The datatypes ranging from float to complex, all are acceptable.
Let us see with an example:
numpy_array = np.array([1,8,5,59,8,98], dtype = 'float')
print(numpy_array)
# let's try Numpy array with string as input
str = ['the','air','after','summer','rain']
arr = np.array(str, dtype = 'str')
print(arr)
Output:
[ 1. 8. 5. 59. 8. 98.]
['the' 'air' 'after' 'summer' 'rain']
In a similar manner, other data-types can also be used to create a Numpy array.
2. Using arange()
function to create a Numpy array:
The arange()
function is one of the Numpy's most used method for creating an array within a specified range. The first argument takes the starting point of the array you want to create, second is the stop point and the third is the step (just like python list slicing function). The last argument is again dtype
, which is optional.
arange(start, end, step, dtype)
Among all of the arguments only end
is mandatory and by default start=0 and step=1. Let's take a few examples,
# creating an array starting from 0 ending with 10
# and increasing with a step of 2
arange_array = np.arange(0,11,2) # writing 11 instead of 10 since range is exclusive
print("First array", arange_array)
# array starting from 50 going till 120 with step of 4
arr = np.arange(50,121,4)
print("Second Array", arr)
# starting from 0(by default), stopping at 14(don't forget 15 is exclusive), step=1(by default)
arr1 = np.arange(15)
print("Third Array", arr1)
# okay now lets print backward counting: 20 to 1
arr2 = np.arange(20,0,-1)
print("Reverse Array", arr2)
Output:
First array [ 0 2 4 6 8 10]
Second Array [ 50 54 58 62 66 70 74 78 82 86 90 94 98 102 106 110 114 118]
Third Array [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
Reverse Array [20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1]
3. Creatin Numpy array using linspace()
function:
Like arange()
function, linspace()
function can also be used to create Numpy array but with more discipline.
In the arange()
function, we had control over where to start the Numpy array from, where to stop and step points but with linspace()
we can maintain a proper linear stepping or spacing between array elements value while generating the array. Imagine if you have some arguments in arange()
function to generate a Numpy array, which gives you the output array that has elements not linearly stepped, in such a case, linspace()
comes to the rescue.
linspace()
function takes arguments: start index, end index and the number of elements to be outputted. These number of elements would be linearly spaced in the range mentioned. Following is the syntax,
linspace(start_index, end_index, num_of_elements)
Now let's take a code example,
# printing arr consisting of 10 values in between
# range 15, 75 spaced appropriately.
arr = np.linspace(15, 75, 10)
print("First Array", arr)
# properly spaced array of 25 elements
arr1 = np.linspace(50,100,25)
print("Second Array", arr1)
Output:
First Array [15. 21.66666667 28.33333333 35. 41.66666667 48.33333333
55. 61.66666667 68.33333333 75. ]
Second Array [ 50. 52.08333333 54.16666667 56.25 58.33333333
60.41666667 62.5 64.58333333 66.66666667 68.75
70.83333333 72.91666667 75. 77.08333333 79.16666667
81.25 83.33333333 85.41666667 87.5 89.58333333
91.66666667 93.75 95.83333333 97.91666667 100. ]
4. Using empty()
function:
The empty()
function is used to create arrays when we don't really have any values to create an array. What it does is, it takes the shape of the array as desired and the array is then filled with random values. The trick here is that without even using the random module we are able to build an array full of random values. Isn't that interesting? Following is the syntax of the function,
empty(shape, dtype)
Let's take an example,
# arr of shape (5,2) with datatype=float filled with random values
arr = np.empty((5,2), dtype=float)
print("Array with Float values", arr)
# observe what happens when executed
arr1 = np.empty((4,4), dtype=int)
print("Second Array", arr1)
Output:
Array with Float values [[6.90895867e-310 1.56460630e-316]
[6.90895946e-310 6.90895946e-310]
[6.90895947e-310 6.90895947e-310]
[6.90895938e-310 6.90895946e-310]
[6.90895946e-310 6.90895945e-310]]
Second Array [[139838880218104 28198256 139838686798448 139838896129448]
[139838895481104 139838693862768 139838896515984 139838693862832]
[ 11065920 11065888 139838896324712 139838896513352]
[139838693849464 139838896282904 139838693862896 139838895838296]]
5. Special Numpy Array using eye()
, ones()
, zeros()
, identity()
etc functions
Numpy arrays can also be created using the random module and its functions like randn
, randint
, etc. We can also use some awesome functions to create array full of ones or zeros using np.ones(shape_of_array)
, np.zeros(shape_of_arrays)
, etc functions. We will explore these functions in details in the upcoming tutorials.
Till then stay connected, stay curious and show some love.
Don't forget to share (np.array(['sharing','is','caring'])
.
You may also like: