This post is a part of a series of 2 post on the operations with numpy arrays.We will go through some basic functions in this post and also in the next post.The post contains description of some basic functions and details about the syntax to use them.
Hello All,
Today we are going to take a look at the following functions in this first part of this 2 part series.
Following are the functions which we are going to go through:
1. copy
2. arange
3. random.rand
Moving on we will have a look in the copy function.
1.copy[1]
If we want to make a new array and need to change the values inside it then we can do the following process to get the reference of the existing array:
But there is one problem with this approach.
arr_2 still refers to the same array which arr_1 refers. So if we try to change the elements inside the arr_2 array, then the elements of the arr_1 are also changed.
In order to avoid this problem we can use the copy() function.
Using the copy function we can create the copy of the array and avoid the problem as specified above.
Making necessary changes for using the copy function in the previous code we get:
We can check from the code snippet that the object id for arr_1 and arr_2 were same before using the copy() function. After we use the copy function we can see that the id of arr_2 is different when compared to arr_1. So our initial problem is solved using the copy() function.
2.arange[2]
If we want to generate a range of numbers with some specific common difference between them we can use the arange() function. The arange function takes 4 arguments of which 3 are optional and one is mandatory.First parameter is start value which specifies from where the generation of the number starts.Next parameter is the stop value before which the generation of the number should stop.The third parameter is the step size which will guide the step size to generate the next number.Also there is another parameter for specifying the data type.The arange() function returns an ndimension array and not a list when compared to the range() function within python. For more details check the references at the end of the post.
The following code snippet specifies how to use the arange() function.
3.random.rand[3]
Now we have to generate 10 numbers which can be done using the arange() function. But if we want to generate random numbers between 0 and 1 , we can use the rand function of the Random module in Numpy. The rand() function takes the dimension of the ndim array to be generated.
Following code snippet shows the above concept:
From the above code snippet we can see that there are 2 examples: one with single dimension and one with 2 dimensions.
Thank you.
That's all for this post!!
Thank you for reading this post.
If you have any suggestions regarding the post contents or if you need some more details on any other topic, please post it in the comments section.
Your suggestions are too valuable so they should not be missed.
References:
1.https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.copy.html
2.https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.arange.html
3.https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.random.rand.html
Hello All,
Today we are going to take a look at the following functions in this first part of this 2 part series.
Following are the functions which we are going to go through:
1. copy
2. arange
3. random.rand
Moving on we will have a look in the copy function.
1.copy[1]
If we want to make a new array and need to change the values inside it then we can do the following process to get the reference of the existing array:
But there is one problem with this approach.
arr_2 still refers to the same array which arr_1 refers. So if we try to change the elements inside the arr_2 array, then the elements of the arr_1 are also changed.
In order to avoid this problem we can use the copy() function.
Using the copy function we can create the copy of the array and avoid the problem as specified above.
Making necessary changes for using the copy function in the previous code we get:
We can check from the code snippet that the object id for arr_1 and arr_2 were same before using the copy() function. After we use the copy function we can see that the id of arr_2 is different when compared to arr_1. So our initial problem is solved using the copy() function.
2.arange[2]
If we want to generate a range of numbers with some specific common difference between them we can use the arange() function. The arange function takes 4 arguments of which 3 are optional and one is mandatory.First parameter is start value which specifies from where the generation of the number starts.Next parameter is the stop value before which the generation of the number should stop.The third parameter is the step size which will guide the step size to generate the next number.Also there is another parameter for specifying the data type.The arange() function returns an ndimension array and not a list when compared to the range() function within python. For more details check the references at the end of the post.
The following code snippet specifies how to use the arange() function.
3.random.rand[3]
Now we have to generate 10 numbers which can be done using the arange() function. But if we want to generate random numbers between 0 and 1 , we can use the rand function of the Random module in Numpy. The rand() function takes the dimension of the ndim array to be generated.
Following code snippet shows the above concept:
From the above code snippet we can see that there are 2 examples: one with single dimension and one with 2 dimensions.
Thank you.
That's all for this post!!
Thank you for reading this post.
If you have any suggestions regarding the post contents or if you need some more details on any other topic, please post it in the comments section.
Your suggestions are too valuable so they should not be missed.
References:
1.https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.copy.html
2.https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.arange.html
3.https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.random.rand.html
Comments
Post a Comment