Notice: Undefined index: limited_lang in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/code-syntax-highlighter/inc/src/rendrer.php on line 297
- From NumPy inbuilt functions
- From python structures like lists and tuples
Are you guys ready to learn all those ways to create Numpy array. If yes then keep reading how to create Numpy array in python tutorial.
Check some Pandas tutorials
How To Create Dataframe In Python Using Pandas
How To Read And Write CSV File In Python Pandas
How Do You Create An Array In NumPy Python?
Create numpy array from list(One Dimensional)
We can create numpy array from list in following way. Run the following code snippets.
import numpy as np my_array = np.array([1,2,3]) my_array
What We Did?
- First of all we have imported numpy as np. np is an alias.
- Then created a numpy array from list using array() method and stored this array in a variable named my_array.
- And at last, simply printed the array.
Output
So its output is following.
Finding Information Of The array
Let’s find some information of this array.
Dimension of the array
Now if we want to check dimension of this array then we have to use shape properties. Let’s see how to do this.
my_array.shape
And its output is like below. You can see in the output, there is no values after comma the reason for this is that it is a one dimensional array.
Data type of the array
my_array.dtype
Output
Changing the elements of array
We can change any element of the array in following way.
my_array[2]=20 my_array
Output
So you can see in the given output, the value of third element has been changed.
Accessing elements of array
If we want to access any particular element of the array then we can do it in following way.
my_array[2]
Here we have specified index 2 to access third element of the array.
Output
Create numpy array from list(Two Dimensional)
Let’s know numpy create 2d array from list.
import numpy as np my_array = np.array([[1,2,3],[7,8,9]]) my_array
What we did?
- We have passed a two lists to create 2 dimensional numpy array from list.
Let’s see its output.
Output
Create numpy array from tuple
We can create numpy 1d array in following way.
import numpy as np my_array = np.array((1,2,3)) my_array
What we did?
- We have passed a tuple to the array() method to create numpy array from tuple.
Output
So its output is following.
Create Numpy Array From NumPy inbuilt functions
In this section we will learn to create numpy array using some numpy inbuilt functions. Numpy has various functions to create array but here we will see only following functions.
- zeros()
- ones()
- arange()
- linspace()
- logspace()
So let’s learn these numpy array methods to create numpy array.
zeros()
This method create an array which are filled with zeros. We just have to pass dimension of the array as argument to zeros() method, it will create the array according to passed argument.
So if you have to create an array in which all the values by default should be zero then zeros() function is used.
import numpy as np zeros_array = np.zeros((2,2)) zeros_array
What we did?
- Here we have passed the dimension 2*2. It will create a numpy array of two rows and two columns.
Output
You can see in the output, a 2d numpy array has been created and filled with zeros. But in this array values are 0. which means they are float values. But if you want to work with integer then you have to specify data type. So let’s see how to do that.
import numpy as np zeros_array = np.zeros((2,2), int) zeros_array
Output
Ones()
This method create an array which are filled with zeros. We just have to pass dimension of the array as argument to ones() method, it will create the array according to passed argument.
So if you have to create an array in which all the values by default should be one then ones() function is used.
import numpy as np ones_array = np.ones((3,3), int) ones_array
- Here we have passed the dimension of array 3*3 to the ones() method. That will create an array of 3 rows and 3 columns filled with one.
Output
arange()
arange() function is frequently used to create numpy array. To create array using this method we have to pass the range of the array as argument. It creates an array of passed range. Let’s understand it with an example. See the code below.
import numpy as np arange_array = np.arange(15) arange_array
- Here we have passed value of range 15. It will create an array with values ranging from 0 to 14.
Output
Let’s see the output.
NumPy create 1d array with particular range
You can see the values are from 0 to 14. But if we want to create an array of particular range, for ex. we have to create an array of ranging from 15to 30. Then how to do that. Let’s learn it with an example.
import numpy as np arange_array = np.arange(15, 30) arange_array
- Here we have passed the range of the array 15-30. It will create the array with values ranging from 15-29.
- Let’s see its output.
Output
NumPy create 1d array with particular sequence
Now if we have a case where we have to create an array of particular range with a sequence. For ex. we have to create an array of range 15-30 but also have to skip some values. Let’s understand it practically.
import numpy as np arange_array = np.arange(15, 30, 2) arange_array
- Here we have passed the skipping values 2. This will create an array of range 15-30 but skip two values.
- Lets’ see the output to understand properly.
Output
NumPy create 2d array with arange() method
import numpy as np arange_array = np.arange(8).reshape(4,2) arange_array
Output
NumPy create 3d array with arange() method
import numpy as np arange_array = np.arange(8).reshape(2,2,2) arange_array
Output
linspace()
It returns evenly spaced numbers over a specified interval. We have to pass three parameters to this method to create numpy array.
Run the following code.
import numpy as np linspace_array = np.linspace(1,5,4) linspace_array
- It will take the values from 1-5 and here retstep is 4 so it will break the values into 4 different parts.
logspace()
logspace() computes its start and end points as base**start and base**stop respectively. The base value can be specified, but is 10.0 by default.
import numpy as np logspace_array = np.logspace(1,5,4) logspace_array
Output
So guys its all about how to create numpy array in python tutorial. I hope you have found it helpful. If so then don’t forget to share it with your friends. And if you have any query then must drop your comment in comment section.