Skip to main content

Operations on Numpy arrays-Part 2

This post is the continuation of the Part 1 of the operations on the numpy arrays.In this post we will see some more functions and operations in detail.So without any further a due we will move forward with the post.


Hello All,

This post is the continuation of the first part on the operations on numpy arrays. If you have not read the first part of this post ,I request you to go through the first part using the following link.Operations on Numpy arrays-Part 1 

Let us move forward with the operations in this post. In the last post we had seen various functions like copy(), arange()  random.rand().

In this post we are going to see some of the operations related to managing the data type and details about the size of the numpy arrays.

First we are going to some commonly used data types within numpy.

Some of the most common data types used are np.float32, np.float64. np.int32, np.int64 and many more specialized data types which can be viewed in detail on the numpy website. Refer the references section for the links to the specific pages.

In the following code snippets we will create numpy arrays containing heterogeneous data and homogeneous data. Also we will check ways to change the data type of the array.

Firstly we will create an array of type integer.

Next we will create array of type float.

Moving on we will create an array containing heterogenous data(with strings and numbers).

Changing the data type of integer array to float.

One critical this to be observed is that when we created an integer array the data type of the array was int. When we created an array with same number and just one string, the data type changes.This is because there is a priority order for deciding the data type when certain type of data is available. The priority order is given as follows with the first data type having the highest priority and the last having the least priority: String > Float > Integer .


Next we are going to see how to check total bytes occupied by the numpy array using the nbytes attribute of the ndim array.

We can also check the size of each item inside the array using the itemsize attribute of the ndim array.

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/user/basics.types.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...