Skip to main content

Operations on Numpy arrays-Part 1

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

Comments

Popular posts from this blog

PyMuPDF vs PDFMiner

 As a developer , I was tasked to extract specific data from a PDF. Upon analysing it further, certain patterns were found based on keywords in the document. Since I was using Python language for the task I found 2 tools quite useful which are PyMuPDF and PDFMiner. These tools can then be used to extract the text from a page on which regular expression can be applied to further extract relevant data.     Next, we are going to take a deeper look into these tools, specifically focusing on the pros and cons of each.     PyMuPDF   Docs , PIP package Pros Simple and understandable API Extensive tools to work with text, images, and graphics Available as a PIP package (pip install PyMuPDF) Better support for a range of symbols comparer to PyPDF2   Cons Parsed text is not in sequence Dependency on other package-Fitz Text sequence information lost during extraction     PDFMiner   Docs ,  PIP package ...

Finding difference between 2 files in Python

In this post, we will take a look at how to compare two files using Python.   I was tasked to compare 2 files and then list the differences between them using Python. Initially, I started with filecmp module, but even with the function parameter ‘ shallow’ set to false, the Boolean result was not enough. Sure, it can act as an indicator to take some action, but it will not list the differences.   I was looking for something more visual, something like color coding and not like the git diff output, which is not very user-friendly. But, another Python internal module, difflib helped me to get the job done.   Inside Difflib, HtmlDiff is what I was looking for. The differences were highlighted with 3 different colors and also the line numbers were indicated in a table to locate the differences. The results are quite self-explanatory and it is easier to explain the differences to other people. Code for generating the above difference table: Note: File1...

Adding existing Anaconda environment to Jupyter notebook

In this post we are going to take a look at adding Anaconda environment to Jupyter notebook. Recently, I was working on a CSV file and wanted to work with Pandas package for tabular data manipulation using Python. The problem was even if I install Pandas package, I would have to install other Data Science package as needed. But, the Anaconda environment was already setup on my laptop, which I want to reuse.   Today, we will look into how to reuse the Anaconda environment within the Jupyter Notebook.   There are 4 basic steps to be followed for adding the environment: 1. Create a conda environment Go to Conda command prompt(Run in Admin mode) Run the following command: conda create –-name newenv O/P:   What if there is an existing conda environment? Go to Conda command prompt(No need for Admin mode) Run the following command: conda env list O/P: Since there was only one environment, only one entry was displayed. ‘*’ indicates the cur...