0% found this document useful (0 votes)
9 views38 pages

Numpy

NumPy is a powerful open-source library for numerical computing in Python, providing efficient multi-dimensional array operations and mathematical functions. It supports various functionalities such as data analysis, linear algebra, and array manipulation, including creation, reshaping, and arithmetic operations. The library's ndarray object allows for fast and flexible data handling, making it essential for scientific computing.

Uploaded by

surya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views38 pages

Numpy

NumPy is a powerful open-source library for numerical computing in Python, providing efficient multi-dimensional array operations and mathematical functions. It supports various functionalities such as data analysis, linear algebra, and array manipulation, including creation, reshaping, and arithmetic operations. The library's ndarray object allows for fast and flexible data handling, making it essential for scientific computing.

Uploaded by

surya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Numpy

Conten
t
Numpy
NumPy is short for Numerical Python, general-purpose array-processing package. It
provides a high-performance multidimensions
It is the fundamental package for scientific computing with Python.
It is open-source software. It contains various features including these important
ones:
❖ A ndarray, an efficient multi dimensional array providing fast array –oriented
arithmetic operations and flexible broadcasting capabilities
❖ Mathematical functions for fast operations on entire array without having loops.
❖ Tools for reading/writing array data to disk and working with memory-mapped
files.
❖ Useful linear algebra, Fourier transform, and random number capabilities.
❖ A C API for connecting NumPy with libraries written in C,C++, Fortran.
Numpy
Data analysis applications, the main areas of functionality:
Fast vectorized array operations for data munging and cleaning, subsetting
and filtering, transformation, and any other kinds of computations
Common array algorithms like sorting, unique, and set operations
Efficient descriptive statistics and aggregating/summarizing data
Data alignment and relational data manipulations for merging and joining
together heterogeneous data sets
Expressing conditional logic as array expressions instead of loops with
if-elif else branches
Group-wise data manipulations (aggregation,transformation, function
application).
Numpy ndarray: A multidimensional Array Object
1. 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 ndarray: A multidimensional Array
Object
Example :
# Python program to demonstrate basic # Printing shape of array
array characteristics print("Shape of array: ", [Link])
import numpy as np
# Creating array object # Printing size (total number of
arr = [Link]( [[ 1, 2, 3], elements) of array
[ 4, 2, 5]] ) print("Size of array: ", [Link])

# Printing type of arr object # Printing type of elements in array


print("Array is of type: ", type(arr)) print("Array stores elements of type: ",
[Link])
# Printing array dimensions (axes)
print("No. of dimensions: ", [Link])
Numpy ndarray: A multidimensional Array
Object
Output:
Array is of type:
No. of dimensions: 2
Shape of array: (2, 3)
Size of array: 6
Array stores elements of type: int64
Creating ndarrays
Array creation: There are various ways to create arrays in NumPy.
• For example, 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.
• Often, the elements of an array are originally unknown, but its size is
known. Hence, NumPy offers several functions to create arrays with initial
placeholder content. These minimize the necessity of growing arrays, an
expensive operation. For example: [Link], [Link], [Link], [Link],
etc.
• To create sequences of numbers, NumPy provides a function analogous to
range that returns arrays instead of lists.
Creating ndarrays
• arange: returns evenly spaced values within a given interval. step size is specified.
• linspace: returns evenly spaced values within a given interval. num no. of
elements are returned.
• Reshaping array: We can use reshape method to reshape an array. Consider an
array with shape (a1, a2, a3, …, aN). We can reshape and convert it into another
array with shape (b1, b2, b3, …, bM). The only required condition is: a1 x a2 x a3 …
x aN = b1 x b2 x b3 … x bM . (i.e original size of array remains unchanged.)
• Flatten array: We can use flatten method to get a copy of array collapsed
into one dimension. It accepts order argument. Default value is ‘C’ (for row-major
order). Use ‘F’ for column major order.
• Note: Type of array can be explicitly defined while creating array.
Creating ndarrays
To create a NumPy array, can use the function [Link]()
To create a simple array is pass a list to it.
Example:
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
Visualizing arrary in numpy
print(type(arr))
Output:
[1 2 3 4 5] <class '[Link]'>
Creating ndarrays
Besides creating an array from a sequence of elements can easily create an array filled with 0’s and 1’s:
Syntax: [Link](2) array([0., 0.])
>>>[Link]((3, 4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
# Create an empty array with 2 elements
>>>[Link]((2, 3, 4), dtype=np.int16)
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]], dtype=int16)
Creating ndarrays – to demonstrate array creation
import numpy as np # Create a constant value array of
# Creating array from list with type float complex type
a = [Link]([[1, 2, 4], [5, 8, 7]], dtype = 'float') d = [Link]((3, 3), 6, dtype = 'complex')
print ("Array created using passed list:\n", a) print ("\nAn array initialized with all 6s."
# Creating array from tuple
"Array type is complex:\n",
d)
b = [Link]((1 , 3, 2))
print ("\nArray created using passed tuple:\n", b) # Create an array with random values
# Creating a 3X4 array with all zeros e = [Link]((2, 2))
c = [Link]((3, 4)) print ("\nA random array:\n", e)
print ("\nAn array initialized with all zeros:\n",
c)
Creating ndarrays – to demonstrate array creation
# Create a sequence of integers #Reshaping 3X4 array to 2X2X3 array
# from 0 to 30 with steps of 5 arr = [Link]([[1, 2, 3, 4], [5,
2, 4, 2], [1, 2, 0, 1]])
f = [Link](0, 30, 5)
newarr = [Link](2, 2, 3)
print ("\nA sequential array with steps of 5:\n", f) print ("\nOriginal array:\n", arr)
print ("Reshaped array:\n", newarr)
# Create a sequence of 10 values in range 0 to 5 # Flatten array
g = [Link](0, 5, 10) arr = [Link]([[1, 2, 3], [4, 5, 6]])
flarr = [Link]()
print ("\nA sequential array with 10 values
between“0and 5:\n", g)
print ("\nOriginal array:\n", arr)
print ("Fattened array:\n", flarr)
Array Creation Functions
Function Description Function Description

array Convert input data (list, tuple, ones, ones_like Produce an array of all 1’s with the
array, or other sequence type) given shape and dtype. ones_like takes
to an ndarray either by inferring another array and produces a ones
a dtype or explicitly specifying a array of the same shape and dtype.
dtype. Copies the input data by
default
Asarray Convert input to ndarray, but zeros, zeros_like Like ones and ones_like but producing
do not copy if the input is arrays of 0’s instead
already an ndarray
Arange Like the built-in range but empty, empty_like Create new arrays by allocating new
returns an ndarray instead of a memory, but do not populate with any
list. values like Create new arrays by
allocating new memory, but do not
populate with any values like
eye, identity Create a square N x N identity
matrix (1’s on the diagonal and
0’s elsewhere)
Datatypes for ndarrays
The data type or dtype is a special object containing the information
the ndarray needs to interpret a chunk of memory as a particular type
of data:
Dtypes are part of what make NumPy so powerful and flexible. In most
cases they map directly onto an underlying machine representation,
which makes it easy to read and write binary streams of data to disk
and also to connect to code written in a low-level language.
The numerical dtypes are named the same way: a type name, like float
or int, followed by a number indicating the number of bits per element.
Datatypes for ndarrays
Example 3
Datatypes for [Link] numpy as np
Example 1 [Link]=[Link]([1.2,-2.1,3.5,4.8])
[Link] numpy as np [Link](np.int32)
2.d = [Link](np.int32) [Link](arr)
[Link](d) Output:
Output: array([1,-2,3,4],dype=int32)
int32 Note:
Example 2 -astype float to int the decimal part will
[Link] numpy as np be truncated
2.d = np.int32(i4) -astype is used to convert string to
[Link](d) numeric form
Output: -astype always create a new array( a
int32 copy of the data), even if newdtype is
same as old dtype.
Arithemetic with NumPy Arrays
Arrays are important because they enables to express batch operations on
data without any loops.
This is called as Vectorization. Any arithmetic operations between equal-size
arrays applies the operation element-wise
Example: Add the values in arr1 to the values in arr2:
import numpy as np
arr1 = [Link]([10, 11, 12, 13, 14, 15])
arr2 = [Link]([20, 21, 22, 23, 24, 25])
newarr = [Link](arr1, arr2)
print(newarr)
Output:
[30 32 34 36 38 40]8 40]
Arithemetic with NumPy Arrays
Example: Subtract the values in arr1 Example: Multiply the values in arr1 to
to the values in arr2: the values in arr2:
import numpy as np import numpy as np
arr1 =[Link]([10, 20, 30, 40, 50, 60]) arr1 =[Link]([10, 20, 30, 40, 50, 60])
arr2= [Link]([20, 21, 22, 23, 24, 25]) arr2= [Link]([20, 21, 22, 23, 24, 25])
newarr = [Link](arr1, arr2) newarr = [Link] (arr1, arr2)
print(newarr) print(newarr)
Output: Output:
[-10 -1 8 17 26 35] [200 420 660 920 1200 1500]
Arithmetic with NumPy Arrays
Reciprocal Function: Returns the reciprocal of all the array elements.
import numpy as np
a = [Link]([7,3,4,5,1])
[Link](a)
Output: array([0,
0, 0, 0, 1])
Power Function: first array as base and raises it to the power of the elements of
the second array.
import numpy as np
a = [Link]([7,3,4,5,1])
b = [Link]([3,4,5,6,7])
[Link](a,b)
Output:array([ 343, 81, 1024, 15625, 1])
Operations between different sized arrays called as Broadcasting
Basic Indexing and Slicing
• Array Indexing: Knowing the basics of array indexing is important for analysing
and manipulating the array object.
• NumPy offers many ways to do array indexing.
• Slicing: Just like lists in python, NumPy arrays can be sliced. As arrays can be
multidimensional, need to specify a slice for each dimension of the array.
• Integer array indexing: In this method, lists are passed for indexing for each
dimension. One to one mapping of corresponding elements is done to construct
a new arbitrary array.
• Boolean array indexing: This method is used when want to pick elements from
array which satisfy some condition.
Basic Indexing and Slicing
• One-dimensional arrays are simple; on the surface they act similarly to Python
lists:
>>>arr = [Link](10)
print(arr)
Output: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>>print (arr[5])
Output: 5
>>> arr[5:8]
print(arr)
Output: array([5, 6, 7])
Basic Indexing and Slicing
>>> arr[5:8] = 12
print(arr)
Output: array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
• Here assigning a scalar value to a slice, as in arr[5:8] = 12, the value is propagated
(or broadcasted henceforth) to the entire selection. An important first distinction
from lists is that array slices are views on the original array. This means that the
data is not copied, and any modifications to the view will be reflected in the
source.
>>>arr_slice = arr[5:8]
arr_slice[1] = 12345
print( arr)
Output array([ 0, 1, 2, 3, 4, 12, 12345, 12, 8, 9])
Basic Indexing and Slicing
arr_slice[:] = 64
print(arr)
Output:array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9])
In a two-dimensional array, the elements at each index are no longer scalars but
rather one-dimensional arrays:
>>>arr2d = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Print(arr2d[2] )
Output: array([7, 8, 9])
Thus, individual elements can be accessed recursively. But that is a bit too much
work, so can pass a comma-separated list of indices to select individual elements.
Basic Indexing and Slicing
So these are equivalent:
>>> arr2d[0][2]
Output: 3
>>> arr2d[0, 2]
Output: 3

Indexing elements in a NumPy array


Indexing with Slices
Like one-dimensional objects such as Python lists, ndarrays can be sliced using the
familiar syntax:
>>>arr[1:6]
print(arr)
Output:array([ 1, 2, 3, 4, 64])
Higher dimensional objects give more options to slice one or more axes and also
mix integers. Consider the 2D array arr2d. Slicing this array is a bit different:
>>> arr2d >>>arr2d[:2]
print(arr2d) Output:
array([[1, 2, 3],
Output: [4, 5, 6]])
array([[1, 2, 3],
[4, 5, 6], [7, 8, 9]])
Indexing with Slices
Multiple slices can be passed just like you can pass multiple indexes.
>>>arr2d[:2, 1:]
Output: array([[2, 3], [5, 6]])
>>>arr2d[1, :2]
Output: array([4, 5])
>>>arr2d[2, :1]
Output: array([7])
>>>arr2d[:, :1]
Output: array([[1], [4], [7]])
Boolean Indexing
• Boolean indexing: Boolean indexing helps to pick out arbitrary elements of an
array. Frequently this type of indexing is used to select the elements of an array
that satisfy some condition.
• Here the randn function in [Link] is used to generate some random
normally distributed data.
Example:
>>>names = [Link](['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe’])
data = randn(7, 4)
print(names)
Output: array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], dtype='|S4’)
Boolean Indexing
>>>data
Output:
array([[-0.048 , 0.5433, -0.2349, 1.2792],
[-0.268 , 0.5465, 0.0939, -2.0445],
[-0.047 , -2.026 , 0.7719, 0.3103],
[ 2.1452, 0.8799, -0.0523, 0.0672],
[-1.0023, -0.1698, 1.1503, 1.7289],
[ 0.1913, 0.4544, 0.4519, 0.5535],
[ 0.5994, 0.8174, -0.9297, -1.2564]])
Boolean Indexing
>>> names == 'Bob’
Output: array([ True, False, False, True, False, False, False], dtype=bool)
This boolean array can be passed when indexing the array:
>>>data[names == 'Bob’]
Output: array([[-0.048 , 0.5433, -0.2349, 1.2792],
[ 2.1452, 0.8799, -0.0523, 0.0672]])
The boolean array must be of the same length as the axis it’s indexing.
Boolean Indexing
To select everything but 'Bob', can either use != or negate the condition using -:
>>>names != 'Bob’
Output: array([False, True, True, False, True, True, True], dtype=bool)
>>> data[-(names == 'Bob’)]
Output: array([[-0.268 , 0.5465, 0.0939, -2.0445],
[-0.047 , -2.026 , 0.7719, 0.3103],
[-1.0023, -0.1698, 1.1503, 1.7289],
[ 0.1913, 0.4544, 0.4519, 0.5535],
[ 0.5994, 0.8174, -0.9297, -1.2564]])
Boolean Indexing
Selecting two of the three names to combine multiple boolean conditions, use
boolean arithmetic operators like & (and) and | (or):
>>>mask = (names == 'Bob') | (names == 'Will’)
>>>> mask
Output: array([True, False, True, True, True, False, False], dtype=bool)
>>>data[mask]
Output:array([[-0.048 , 0.5433, -0.2349, 1.2792],
[-0.047 , -2.026 , 0.7719, 0.3103],
[ 2.1452, 0.8799, -0.0523, 0.0672],
[-1.0023, -0.1698, 1.1503, 1.7289]])
Selecting data from an array by boolean indexing always creates a copy of the
data, even if the returned array is unchanged.
Fancy Indexing
Fancy indexing is a term adopted by NumPy to describe indexing using integer
arrays.
Suppose we had a 8 × 4 array:
>>>arr = [Link]((8, 4))
for i in range(8):
.....: arr[i] = I
Print(arr)
Output: array([[ 0., 0., 0., 0.], [ 1., 1., 1., 1.], [ 2., 2., 2., 2.], [ 3., 3., 3., 3.], [ 4., 4., 4.,
4.], [ 5., 5., 5., 5.], [ 6., 6., 6., 6.], [ 7., 7., 7., 7.]])
Fancy Indexing
To select out a subset of the rows in a particular order, can simply pass a list or
ndarray of integers specifying the desired order:
>>> arr[[4, 3, 0, 6]]
Output: array([[ 4., 4., 4., 4.],
[ 3., 3., 3., 3.],
[ 0., 0., 0., 0.],
[ 6., 6., 6., 6.]])
Using negative indices select rows from the end:
>>>arr[[-3, -5, -7]]
Output: array([[ 5., 5., 5., 5.],
[ 3., 3., 3., 3.],
[ 1., 1., 1., 1.]])
Universal Functions: Fast Element –wise Array Functions
ufuncs stands for
"Universal Functions"
and they are NumPy
functions that operate
on the ndarray object.
Universal Functions: Fast Element –wise Array
import numpy as np # hyperbolic sine of angles
# create an array of angles print('Sine hyperbolic of angles in the
array:')
angles = [Link]([0, 30, 45, 60, 90, 180]) sineh_value = [Link](radians)
# conversion of degree into radians print([Link](radians))
# using deg2rad function
# inverse sine hyperbolic
radians = np.deg2rad(angles)
print('Inverse Sine hyperbolic:')
# sine of angles print([Link](sineh_value))
print('Sine of angles in the array:')
sine_value = [Link](radians) # hypot function demonstration
base = 4
print([Link](radians))
height = 3
# inverse sine of sine values print('hypotenuse of right triangle is:')
print('Inverse Sine of sine values:') print([Link](base, height))
Universal Functions: Fast Element –wise
Array
Universal Functions: Fast Element –wise
Array
import numpy as np
# mean
# construct a weight array
weight = [Link]([50.7, 52.5, 50, 58, 55.63, 73.25, print('Mean weight of the students: ')
49.5, 45]) print([Link](weight))
# minimum and maximum
print('Minimum and maximum weight of the students: ') # median
print([Link](weight), [Link](weight)) print('Median weight of the students: ')
# range of weight i.e. max weight-min weight print([Link](weight))
print('Range of the weight of the students: ')
print([Link](weight))
# standard deviation
# percentile print('Standard deviation of weight of the students: ')
print('Weight below which 70 % student fall:’) print([Link](weight))
print([Link](weight, 70))

You might also like