0% found this document useful (0 votes)
12 views

What is NumPy

Uploaded by

mayankrustagi92
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

What is NumPy

Uploaded by

mayankrustagi92
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

What is NumPy?

NumPy stands for Numerical Python, is an open-source Python


library that provides support for large, multi-dimensional arrays and
matrices. It also have a collection of high-level mathematical functions
to operate on arrays. It was created by Travis Oliphant in 2005.NumPy
is a general-purpose array-processing package.
It provides a high-performance multidimensional array object and tools
for working with these arrays. It is the fundamental package for
scientific computing with Python.

Features of NumPy

NumPy has various features which make them popular over lists.
Some of these important features include:
 A powerful N-dimensional array object
 Sophisticated (broadcasting) functions
 Tools for integrating C/C++ and Fortran code
 Useful linear algebra, Fourier transform, and random number
capabilities

Arrays in NumPy

NumPy’s main object is the homogeneous multidimensional array.

 It is a table of elements (usually numbers), all of the same type,


indexed by a tuple of positive integers.
 In NumPy, dimensions are called axes. The number of axes is rank.
 NumPy’s array class is called ndarray. It is also known by the
alias array.

NumPy Array Creation

There are various ways of Numpy array creation in Python. They are as
follows:

1. Create NumPy Array with List and Tuple


You can create an array from a regular Python list or tuple using the
array() function. The type of the resulting array is deduced from the
type of the elements in the sequences. Let’s see this implementation:
2. Create Using arange() Function
arange(): This function returns evenly spaced values within a given
interval. Step size is specified.

3. Create Using linspace() Function

linspace(): It returns evenly spaced values within a given interval.


Scalars:

Scalar operations on Numpy arrays include performing addition or


subtraction, or multiplication on each element of a Numpy array. Let us see
the following examples:

 Addition Operation on Numpy Arrays


 Subtraction Operation on Numpy Arrays
 Multiplication Operation on Numpy Arrays
 Division Operation on Numpy Arrays

Addition Operation on Numpy Arrays


The Addition Operation is adding a value to each element of a NumPy array.
Let us see an example
Subtraction Operation on Numpy Arrays
The Subtraction operation is subtracting a value from each element of a
NumPy array. Let us see an example:

Multiplication Operation on Numpy Arrays


The Multiplication operation is multiplying a value to each element of a NumPy
array. Let us see an example:
Division Operation on Numpy Arrays
The Division operation is to divide a value from each element of
a NumPy array. Let us see an example:

Indexing using index arrays


Indexing can be done in numpy by using an array as an index. In case
of slice, a view or shallow copy of the array is returned but in index
array a copy of the original array is returned. Numpy arrays can be
indexed with other arrays or any other sequence with the exception of
tuples. The last element is indexed by -1 second last by -2 and so on.
Types of Indexing
There are two types of indexing :
Basic Slicing and indexing :
Consider the syntax x[obj] where x is the array and obj is the index.
Slice object is the index in case of basic slicing. Basic slicing occurs
when obj is :
 a slice object that is of the form start : stop : step
 an integer
 or a tuple of slice objects and integers
All arrays generated by basic slicing are always view of the original
array.

Advanced indexing :

Advanced indexing is triggered when obj is -


 an ndarray of type integer or Boolean
 or a tuple with at least one sequence object
 is a non tuple sequence object

Advanced indexing returns a copy of data rather than a view of it. Advanced
indexing is of two types integer and Boolean.
Purely integer indexing :
When integers are used for indexing. Each element of first dimension is paired
with the element of the second dimension. So the index of the elements in this
case are (0,0),(1,0),(2,1) and the corresponding elements are selected.
Python

Boolean Array Indexing:


This indexing has some boolean expression as the index. Those
elements are returned which satisfy that Boolean expression. It is used
for filtering the desired element values.
Data Visualization using Matplotlib in Python
Data visualization is a crucial aspect of data analysis, enabling data
scientists and analysts to present complex data in a more
understandable and insightful manner. One of the most popular
libraries for data visualization in Python is Matplotlib.

Matplotlib:
Matplotlib is a powerful plotting library in Python used for creating static,
animated, and interactive visualizations. Matplotlib’s primary purpose is to
provide users with the tools and functionality to represent data graphically,
making it easier to analyze and understand. It was originally developed by John D.
Hunter in 2003 and is now maintained by a large community of developers.

Key Features of Matplotlib:


1. Versatility: Matplotlib can generate a wide range of plots, including line
plots, scatter plots, bar plots, histograms, pie charts, and more.

2. Customization: It offers extensive customization options to control


every aspect of the plot, such as line styles, colors, markers, labels, and
annotations.

3. Integration with NumPy: Matplotlib integrates seamlessly with


NumPy, making it easy to plot data arrays directly.

5. Extensible: Matplotlib is highly extensible, with a large ecosystem of


add-on toolkits and extensions like Seaborn, Pandas plotting functions, and
Basemap for geographical plotting.

6. Cross-Platform: It is platform-independent and can run on various


operating systems, including Windows, macOS, and Linux.

7. Interactive Plots: Matplotlib supports interactive plotting through the


use of widgets and event handling, enabling users to explore data
dynamically

Data Visualization With Pyplot in Matplotlib:


Matplotlib provides a module called pyplot, which offers a
MATLAB-like interface for creating plots and charts. It simplifies the
process of generating various types of visualizations by offering a
collection of functions that handle common plotting tasks. Each function
in Pyplot modifies a figure in a specific way, such as:

plot() Creates line plots with customizable styles.

bar() Creates bar charts for comparing categories.

hist() Draws histograms to show data distribution.

xlabel(), ylabel() Sets labels for the X and Y axes.

1. Line Chart
Line chart is one of the basic plots and can be created using
the plot() function. It is used to represent a relationship between two
data X and Y on a different axis.
2. Bar Chart
The bar plots can be plotted horizontally or vertically. A bar chart
describes the comparisons between the discrete categories. It can be
created using the bar() method.

You might also like