Skip to main content

Cheatsheet for NumPy: Essential and Lesser-Known Functions

Numpy python
(Photo by Chris Liverani on Unsplash)

Numpy (stands for — Numerical Python) is a library available in Python programming language, supporting matrix data structures and multidimensional array objects. This the most basic scientific computing library that we need to learn, to begin our journey in the field of data science.

Numpy can compute basic mathematical calculations to make the process of creating advanced machine learning and artificial intelligence applications easier (by using comprehensive mathematical functions available within the library). Numpy allows us to carry out various complex mathematical calculations effortlessly along with several top-up libraries (like matplotlib, pandas, scikit-learn, etc.) built over it.

This library is a great tool for every data science professional to handle and analyze the data efficiently. Moreover, it is much easier to perform mathematical operations with numpy arrays in comparison to python’s list.

Numpy library has various functions available in it. In this article, we will learn some essential and lesser-known functions of this library and how to implement them efficiently.

Note: In this article, we will be using Google Colaboratory to execute our codes.

Importing numpy

Numpy can be simply imported in the notebook by using the following code:

import numpy as np

Here, numpy is written as np to save time while coding, and also it is a de facto in the data science community.

Now, let’s get started with numpy functions!

Creation of n-dimensional array using numpy

An array is a data structure in the numpy library, which is just like a list which can store values, but the differences are that we can specify the data type of elements of an array ( dtype function) and arrays are faster and take less memory to store data, allowing the code to be optimized even further.

To create a single-dimensional array we can use the following code:

import numpy as np
array = np.array([1,2,3,4,5])

The process for creating a multi-dimensional array is similar, we just have to add more values in []brackets:

array = np.array([[1.1,2.2,3.0,4.6,5.0],[6.4,7.3,8.5,9.1,10.2])

numpy.linsapce() function

This numpy.linspace() function is used to create an array of evenly spaced numbers in a given interval. We can also determine the number of samples we want to generate (however, it is an optional parameter default value is set to fifty samples). Another optional parameter we can add to this function is restep which if True will return the space i.e. spacing between the samples along with the list. The function is: numpy.linspace(start, stop) . Let’s apply this function in an example:

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,10,dtype = int, retstep=True)
print(x)
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y,color = 'orange')
plt.show()

As we can see here, even to calculate mathematical functions we are using numpy library. We used the linspace() function to generate equally spaced values and used that array to plot sine function plot.

sine function graph| linspace() function numpy
(Image by Author) sine function graph using linspace() function to generate values


Function for random sampling

Here, the numpy.random function helps us calculate random values in various ways like generating random values in a given shape, generating an array by randomly selecting values from a given 1D array, or randomly permute a sequence of a given array or a range.

  • numpy.random.rand(): With this function, we can create an array of uniformly distributed values over given input shape in a range [0,1) (i.e. ‘1’ is excluded). For example:
np.random.rand(3,4)

As we can see in this example, an array of shape (3,4) is generated with all values lying in a range of [0,1).

Numpy random function
(Image by AuthorGenerating random values
  • numpy.random.choice(): This random function returns an array of random samples from a given input array. Other optional parameters that we can define are- size i.e. the output shape of the array, replace i.e. whether we want repeated values in our output array and p i.e. probability for each given sample of the input array. Check out the following example:
np.random.choice([1,2,3,4],(1,2,3),replace=True,p=[0.2,0.1,0.4,0.3])

Here, we have given the following input parameters to the functions- an input array with four elements, shape of output array ( 1 in the above code is the numbers of the arrays we want as output and 2,3 is output shape), repetition of values is True and probability for each sample (where the sum of values should be equal to one).

Random sampling | numpy python
  • np.random.permutation(): This function returns an array with a randomly permutated sequence (in case of input array) or a permuted range (in case of single-input).
arr = np.random.permutation(5) print('Permutation of a range: ' + str(arr)) arr_ = np.random.permutation([1,2,3,4,5,6,7,8,9]) print('Permutation of elements of input array: ' + str(arr_))

In the first case, we have returned a permuted array over an input range and in the second case, we have returned a permuted array over an input array.

Random permutation using numpy

The functions available in the numpy.random are not only limited to these, but you can also find the complete exhaustive list of functions here: numpy documentation page.

Indexing and slicing of an array

To access and modify the objects of an array, we use indexing and slicing methods. Index values of the first element in the array of length n, start from 0 value and index for the last element of the array will be n-1 .

a = [1,2,3,4,5,6]

b = a[3]

#output = 4

In the above example, this indexing method will return the fourth element of the array a.

For basic slicing of the array (i.e. splitting the array, in simple words), we use [start:stop:step_size] notation.

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

arr[1:7:2]

#output array([1, 3, 5])

Advanced indexing and slicing: For a multi-dimensional array, we can index and slice the array by giving input of specific rows and columns values( in [rows,column] format). For better understanding check the following example:

x = np.array([[ 0,  1,  2],

[ 3,  4,  5],

[ 6,  7,  8]])

x[0:2,1:2]

Here, we have chosen the index of the first two rows (i.e. 0:2 in code) and a single column with index 1 (i.e. 1:2 in code).

Advanced indexing and slicing numpy

numpy.ravel() and numpy.flatten() functions

These functions return a 1D flattened form of the input array.

arr = np.array([[1,2], [3,4],[5,6]])

x = arr.flatten()

print(x)

y = arr.ravel()

print(y)

The output of the above code | numpy

You may observe that the output of both functions is the same! Now you might wonder what is the difference between the two functions as their output result is the same. It’s simple in numpy.flatten() a copy of the original array is created while in numpy.ravel() the original array is changed. Moreover, numpy.ravel() function is faster than numpy.flatten() as it does not occupy any memory.

numpy.isclose() function

This function is used to check whether two arrays are equal elements wise within tolerance and returns a boolean array. .isclosefunction array can be used to assert(verify) your code.

def inv(arr):

 arr = np.array(arr)

 inverse = np.linalg.inv(arr)

 return inverse

assert np.all(np.isclose(inv(np.array([[6, 1, 1], [4, -2, 5], [2, 8, 7]])).tolist(),np.array([[0.17647058823529413,-0.0032679738562091526, -0.02287581699346405],[0.05882352941176469, -0.130718954248366, 0.0849673202614379],[-0.1176470588235294, 0.1503267973856209, 0.0522875816993464]])))

print("Sample Tests passed", '\U0001F44D')

In this above, example we are finding the inverse of a given matrix using another numpy function numpy.linalg.inv() . After that we are verifying are result using assertfunction and we have used numpy.isclose() function to check the output values if they are close to the true values. The assert function will only work if all the values are True otherwise it will give an assertion error.

Implementation of .isclose() function | numpy 


Stack arrays in numpy

There are two functions available in numpy for stacking different arrays.

  • numpy.hstack(): this function stacks the arrays column-wise (i.e. horizontally), similar to the concatenation of arrays along the second axis (except 1D array, where it concatenates along the first axis). For this function, the input arrays should be of the same shape (except 1D arrays, which can be of any length).

a = np.array([[1,2],[3,4],[5,6]])

b = np.array([[7,8],[9,10],[11,12]])

np.hstack((a,b))

Horizontal stacking of arrays using .hstack() function

numpy.vstack(): this function stacks the arrays row-wise (i.e. vertically), similar to the concatenation of arrays along the first axis after 1-D arrays of shape (N,) have been reshaped to (1, N). For this function, the input arrays should be of the same shape (1D arrays must have the same length).

a = np.array([[1,2],[3,4],[5,6]])

b = np.array([[7,8],[9,10],[11,12]])

np.vstack((a,b))

Vertical stacking of arrays using .vstack() function | Numpy

Statistics functions of numpy

Numpy library has some useful functions for finding insights and analyzing the data statistically. We can calculate mean, median, variance, standard deviation, compute histogram over a set of data, and much more.

  • numpy.mean(): with this function, we can calculate the arithmetic mean of a given array where we can also specify the axis.

arr = a = np.array([[1, 2], [3, 4]])

np.mean(a,axis=1)

#output:

array([1.5, 3.5])

  • numpy.histogram(): this function helps us compute the histogram over a set of data. Here, we have to input a flattened array of data over which we want to compute the histogram, we can also define the number of bins (i.e. number of equal-width bins in a given range (optional)), and range of upper limit and limit of the bins (optional).

arr = np.array([1,2,3,2,2,3,4,5])

np.histogram(arr, bins= [1,2,3,4,5])

Computing histogram using numpy

You can also visualize this histogram values on a plot using the matplotlib library.


You can find other numpy statistics functions here: numpy documentation page.

Conclusion

I hope with this article you must have learned some essential and new functions of this library. I would recommend you to try implementing these functions on your own for a better understanding.

Implementing these skills in daily use will definitely benefit you as a data science professional!

If you have any questions or comments, please post them in the comment section.

If you want to learn how to visualize the data and find the insights from it visually, then check out our another article here.


Resources:
https://numpy.org/
https://numpy.org/doc/stable/

Comments

  1. Histogram is very useful garph that show data in very flexible way.
    If you want to know How to make a histogram in google sheets. then you are at right place.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Wonderful article, Thank you for sharing amazing blog write-ups.

    You can also check out another blog on Cryptography and Network Security

    ReplyDelete
  5. This blog has the relevant data according to the topic and precise to the topic.
    Data Science Training in Chennai
    Data Science Training in Bangalore

    ReplyDelete
  6. Thanks for sharing this blog. Keep sharing more blogs with us.
    Python Course in Hyderabad

    ReplyDelete
  7. Artificial intelligence is the simulation of human intelligence processes by machines, especially computer systems. Specific applications of AI include expert systems, natural language processing, speech recognition and machine vision having tal dilian.

    ReplyDelete
  8. This is really a good source of information, I will often follow it to know more information and expand my knowledge, I think everyone should know it, thanks Best python string methods service provider.

    ReplyDelete
  9. AI really is future, I loved reading this blog. The ideas are very appealing and new, everything mentions is going to bring new changes. Thank you so much for such awesome content.

    AI Solutions Development

    ReplyDelete
  10. The article is very informative, they have given us good clarity to master yourself in Data science domain. Skillslash academy provides top notch data science course with real work experience and makes sure you get certified directly with the companies you work for.

    Data Science Course In Bangalore

    ReplyDelete
  11. I am really very happy to visit your blog. Directly I am found which I truly need. please visit our website for more
    information Data visualization Service in USA

    ReplyDelete
  12. LenguaePro translation agency in Fairfield ct is an experienced translation and interpretation company with two branches in the United States and Brazil. LenguaePro incorporates a well-structured team that provides outstanding service and can handle any project accurately and quickly. We do business with companies of all sizes and different disciplines.
    Professional translation in Connecticut
    Spanish Translation in Bridgeport

    ReplyDelete
  13. Hello!
    I read your blog. I found it very informative. I feel the blog aligns perfectly with our services. We are providing data science courses with real-work experience which is ideal for those who wish to have a career transition or start a fresh career path in data science along with a 100% job assurance commitment, in Delhi. Visit our site to know more.

    ReplyDelete
  14. Hey admin, nice article. Nice to read your article. We hope to receive such valuable articles from you in the future. Thank you for sharing with everyone.

    RedSensors: 3D LiDAR and Laser Sensor Technology

    ReplyDelete
  15. Mybusiness Visual is the leading Powerpoint Presentation Companies in Chennai whose created customized PowerPoint Presentation with huge range of clients accross the world and they worked with VGI presentation team for delivering the Outsource Powerpoint Presentation Support.

    ReplyDelete
  16. Mybusiness Visual is the leading Powerpoint Presentation Companies in Chennai whose created customized PowerPoint Presentation with huge range of clients accross the world and they worked with Ex McKinsey Presentation Specialist for delivering the Powerpoint Production Overnight Support.

    ReplyDelete
  17. Thank you for sharing Tips and tricks for coding. If anyone finds expert app developers contact an award-winning mobile app development company Moon Technolabs and get the best app development solutions. Thank you Again!

    ReplyDelete
  18. Thanks for sharing the useful information about the Numpy cheatsheet. If you are looking for the Mobile App Development Company In India then you can go with Lucid Outsourcing Solutions. They have team of experts who have in-hand experience in the latest technology.

    ReplyDelete
  19. Thanks for sharing this article.
    Vintage Finance made new possibilities for personal loan for cibil defaulters in Delhi. Personal loan for CIBIL defaulter

    ReplyDelete
  20. Nice Blog... Very Informative.

    Dhikala Forest Lodge Online Booking A wildlife adventure in India is synonymous with Corbett National Park. It’s the most famous wildlife park in India and is known for its rich flora and fauna.

    ReplyDelete
  21. Vintage Finance is RBI registered firm and a Non-Banking Finance Company (NBFC).
    Personal loan providers in Delhi
    Personal loan from private finance in Noida

    ReplyDelete
  22. Sarv specializes in 3D wheel Alignment Machines for passenger Car &LCV workshops. Using the latest 3D camera technology, Sarv machines provide accurate readings.
    3D Wheel Alignment Machine
    Automatic Tyre Inflator

    ReplyDelete
  23. Are you looking for Venetian Blinds Singapore ? If your answer is yes, you have landed on the appropriate website. Seawall coverings offer the best quality windows blinds in Singapore.

    ReplyDelete
  24. It was very well written and easy to understand. I especially enjoyed your insights on the topic.
    Python Course in Nagpur

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. Cloud storage server
    Get Storage Server with high data capacity to store all your data. Customize data storage server at best price with 24/7 Server Support.

    ReplyDelete
  27. Embark on a transformative journey into the dynamic realm of data science with APTRON's comprehensive Data Science Course in Gurgaon. At APTRON, we pride ourselves on offering a cutting-edge curriculum designed to equip you with the skills and knowledge essential for a successful career in data science.

    ReplyDelete
  28. APTRON's Python Institute in Gurgaon offers a tailored learning journey that equips you with the tools and knowledge needed to thrive in the ever-evolving field of technology. Offering a comprehensive platform to master the intricacies of Python programming.

    ReplyDelete
  29. Dedicated Server in Miami comes with high-security standards and allows you to extract maximum performance. With our managed server solution in Miami, you get servers that are fast and are always running at peak efficiency. Outperform your competition with Lease Packet’s Dedicated Server Miami NOW

    ReplyDelete
  30. Explore top-notch Docker consulting services to optimize containerization strategies. Unlock efficiency and seamless deployment for your business success.

    ReplyDelete

Post a Comment

Popular posts from this blog

Everything You Need to Know About Google Foobar Challenge

Recently, while searching a keyword “headless chrome” on Google I got an unusual pop-up on my window, with a message: "Curious developers are known to seek interesting problems. Solve one from Google?" I was surprised to see Google sending me a challenge to solve and I accepted it immediately! Clicking on “I want to play” landed me on Google’s Foobar page. It was Google Foobar Challenge! What exactly is Google Foobar Challenge? Google Foobar challenge is a secret hiring process by the company to recruit top programmers and developers around the world. And it is known that several developers at Google are hired by this process. The challenge consists of five levels with a total of nine questions , with the level of difficulty increasing at each level. What to do after getting the challenge? After selecting “I want to play” option you land on Foobar’s website which has a Unix-like shell interface, including some standard Unix commands like help, cd, ls, cat and etcetera .

9 Techniques to Write Your Code Efficiently

(Photo by Oskar Yildiz on Unsplash ) It’s really easy to write efficient and faster code . Efficient code, not just only improves the functionality of the code but it can also reduce the time and space complexity of the programming. Speed is one of the major factors in deciding the quality of the code , for instance, your code might be producing the required result but it takes some time to execute then it will not be considered a quality code. An alternative approach to the same problem producing faster results will be considered better. The code should be clean i.e. comprehensible and readable so that it can be reused (saving the efforts of rewriting the whole program from scratch), adding new features, and making the process of debugging more easier. In this article, I will cover some simple tips and techniques which we can easily apply to make our code more elegant and efficient. "There is always more than one method to solve the problem." How to write code efficie

Complete Data Visualization Guide: Python

“A picture is worth a thousand words” -Fred R. Barnard  Data visualization is a visual (or graphic) representation of data to find useful insights (i.e. trends and patterns) in the data and making the process of data analysis easier and simpler. Aim of the data visualization is to make a quick and clear understanding of data in the first glance and make it visually presentable to comprehend the information. In Python, several comprehensive libraries are available for creating high quality, attractive, interactive, and informative statistical graphics (2D and 3D).

Followers