MITB Banner

The Most Important Numpy Functions You Should Know When Learning Python

Share

Data science in Python is mostly analytical, so learning it from scratch is a basic thing to start with. The beginner-friendly steps shown here are a kick-start for learning how to program from the basic codes.

One of the main benefits is its extensive set of libraries, a collection of routines and functions that help data scientists perform complex tasks quite effortlessly without writing a single line of code. One such important function is numerical Python aka NumPy which is a fundamental library, well known for high-performance multi-dimensional array and can be used for different mathematical functions like linear algebra, Fourier Transformations, etc. as well as logical operations.

This article takes a lowdown on understanding NumPy and its functions, including steps to create NumPy arrays, indexing, slicing, etc.

Understanding NumPy And How It Works

The easy way to install Python in your system is to download and install a package called Anaconda that contains pre-installed libraries.

Once Anaconda is installed, now you can easily install NumPy on your terminal:

conda install nump

Voila! NumPy is installed. Let’s start by launching Jupyter notebook.

Import NumPy on your Jupyter notebook:

Import numpy as np

Creating a one-dimensional array:

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

Creating higher dimensional array:

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

Shape of array2:

print ( array2.shape )  # Prints ( 2, 3 )

Changing an element in an array:

array1 [ 0 ] = 9

print ( array1 )  # Prints “ [ 9, 2, 3, 4, 5, 6 ] ”

Using arange( ) built-in function, let’s create NumPy array:

array1 = np.arange ( 0, 10 )  # This generates index value from 0 to 1

We can create Identity Matrix with the given code:

my_matrx = np . eye( 44 )  # here 4 is the number of columns/rows

Generating Random Numbers

An array of random numbers can be generated by using the functions rand(), randn() or randint().

For instance, if we need a one-dimensional array containing 6 objects which are uniformly distributed from 0 to 1, then:

my_rand = np.random.rand (6)

Arithmetic Operations On NumPy: (operating as each element to itself)

array1 = np.arange (1, 12)

array1 * array1

array1 – array1

array1 + array1

array1 / array1

Using Universal Functions In Each Element:

np.sqrt (array1)  # Returns square root

np.exp (array1)   # Returns the exponential

np.log (array1)    # Returns logarithm

np.std (array1)    # Returns standard deviation

The array can be indexed and sliced, noted that a slice must be specified for each dimension of the array:

Indexing:

array1 [ 2 ]  # Prints 3

array2 [ 1, 1 ] # Prints 5

array2 [ 0, : ]  # array ( [ 1, 2, 3 ] ); row 1

Slicing:

array1 ( [ 1 : 3 ] )  # Prints array ( [ 2, 3] )

array1 ( [ 1 : 3 ] ) = [ -2, -3 ]  # Prints array ( [ 1, -2, -3, 4, 5, 6 ] )

array1 [ : : ]   # array ( [ 1, -2, -3, 4, 5, 6 ] ); lower, upper, step all take the default values

array1 [ : 3 ]  # array ( [ 1, -2, -3 ] ); first three elements

array [ : : 2 ]  # array ( [ 1, -3, 5 ] ); here the step is 2

We can compute the functions in reverse order using negative numbers:

array1 [ -3 : ] # Prints array ( [4, 5, 6 ] ); the last three numbers

Importing And Exporting Of Files:

np.loadtxt (file.txt)   # imports from a text file

np.savetxt (‘file.txt’,arr,delimiter= ’  ’)   #writes to a text file

It is always helpful and good to have something extra in the bag. So besides using NumPy, here are some other libraries for Python that you can check out and keep yourself updated.

PS: The story was written using a keyboard.
Picture of Ambika Choudhury

Ambika Choudhury

A Technical Journalist who loves writing about Machine Learning and Artificial Intelligence. A lover of music, writing and learning something out of the box.
Related Posts

Download our Mobile App

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox
Recent Stories

Featured

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

AIM Conference Calendar

Immerse yourself in AI and business conferences tailored to your role, designed to elevate your performance and empower you to accomplish your organization’s vital objectives. Revel in intimate events that encapsulate the heart and soul of the AI Industry.

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed