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

Numpy Numerical Python_unit3

The document provides an overview of NumPy, a foundational package for numerical computing in Python, highlighting its features such as efficient multidimensional arrays, fast element-wise operations, and tools for data manipulation. It discusses key concepts like vectorization and broadcasting, along with practical examples of creating and manipulating arrays. Additionally, it emphasizes the performance benefits of using NumPy over traditional Python lists for large datasets and complex computations.

Uploaded by

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

Numpy Numerical Python_unit3

The document provides an overview of NumPy, a foundational package for numerical computing in Python, highlighting its features such as efficient multidimensional arrays, fast element-wise operations, and tools for data manipulation. It discusses key concepts like vectorization and broadcasting, along with practical examples of creating and manipulating arrays. Additionally, it emphasizes the performance benefits of using NumPy over traditional Python lists for large datasets and complex computations.

Uploaded by

nibaa85
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 69

Numpy

Numerical
Python

Unit 3
● NumPy Basics
○ Arrays and Vectorized
Computation
○ The NumPy nd array
■ Creating ndarrays
■ Data Types for nd
arrays-
○ Arithmetic with NumPy
Arrays

Index ○


Basic Indexing and
Slicing -
Boolean Indexing
○ Transposing Arrays and
Swapping Axes
● Universal Functions
○ Fast Element-Wise Array
Functions
● Mathematical and Statistical
Methods
● Sorting-Unique and Other Set
Logic.
Numpy

Numerical Python, is one of the most important


foundational packages for numerical computing in
Python.

Most computational packages providing scientific


functionality use NumPy’s array objects as the lingua
franca for data exchange.
Numpy- features
ndarray, an efficient multidimensional array providing fast array-oriented
arithmetic operations and flexible broadcasting capabilities.

Mathematical functions for fast operations on entire arrays of data without


having to write loops.

Tools for reading/writing array data to disk and working with memory-
mapped files.

Linear algebra, random number generation, and Fourier transform


capabilities.

A C API for connecting NumPy with libraries written in C, C++, or


FORTRAN.
Example to explain the performance
of Numpy

•To give you an idea of the performance difference, consider a


NumPy array of one million integers, and the equivalent Python list:

In [7]: import numpy as np


In [8]: my_arr = np.arange(1000000)
In [9]: my_list = list(range(1000000))
Example to explain the performance of Numpy
Now let’s multiply each sequence
by 2::
In [10]: %time for _ in In [11]: %time for _ in
range(10): my_arr2 = my_arr range(10): my_list2 = [x * 2
* 2 for x in my_list]
CPU times: user 20 ms, sys: CPU times: user 760 ms, sys:
50 ms, total: 70 ms 290 ms, total: 1.05 s
Wall time: 72.4 ms Wall time: 1.05 s

NumPy-based algorithms are generally 10 to 100 times faster (or


more) than their pure Python counterparts and use significantly less
memory.
Data analysis applications of Numpy

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 datasets Expressing conditional logic as array
expressions instead of loops with if-elifelse branches

Group-wise data manipulations (aggregation, transformation, function


application)
Key Concepts of Vectorized Operations
Vectorization: Performing operations on entire
arrays at once instead of looping through
individual elements.

Broadcasting: Applying operations on arrays of


different shapes without explicit iteration.

Element-wise Operations: Applying functions


directly to each element of an array.
1. Data Munging and Cleaning

import numpy as np

# Creating a NumPy array with NaN values


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

# Replacing NaN with the mean


arr[np.isnan(arr)] = np.nanmean(arr)
print(arr)
# Output: [1. 2. 3. 4. 5.] (mean replaces NaN)
Subsetting and Filtering

arr = np.array([10, 20, 30, 40, 50])


filtered = arr[arr > 25] # Select elements greater than 25
print(filtered)

# Output: [30 40 50]


Reason to use Numpy

NumPy internally stores data in a contiguous block of


memory, independent of other built-in Python objects.
NumPy’s library of algorithms written in the C language can
operate on this memory without any type checking or other
overhead.
NumPy arrays also use much less memory than built-in
Python sequences.

NumPy operations perform complex computations on entire


arrays without the need for Python for loops.
The NumPy ndarray: A Multidimensional Array
Object

N-dimensional array object, or ndarray, is a fast, flexible


container for large datasets in Python.

Arrays enable you to perform mathematical operations on


whole blocks of data using similar syntax to the equivalent
operations between scalar elements.

NumPy enables batch computations with similar syntax to


scalar values on built-in Python objects, first import NumPy
and generate a small array of random data:
Ex.small array of random data

In [12]: import numpy as np


# Generate some random data
In [13]: data = np.random.randn(2, 3)
In [14]: data
Out[14]:
array([[-0.2047, 0.4789, -0.5194],
[-0.5557, 1.9658, 1.3934]])
Ex.small array of random data
• np.random.randn(2, 3) creates a 2D array with 2 rows and 3 columns.
• Values are sampled from the standard normal distribution (Gaussian distribution
with mean = 0 and std = 1).
• np.random (or numpy.random)
• This is a module within NumPy that provides functions for generating random
numbers and performing random sampling.
• 🔹 np.random.randn()
• randn stands for "random normal", i.e., it generates numbers from the standard
normal distribution.
• The standard normal distribution has:
• Mean (μ) = 0
• Standard deviation (σ) = 1
np.random.randn(d0, d1, ..., dn)
• Returns an array of shape (d0, d1, ..., dn) filled with random samples from the standard
normal distribution.
Ex.small array of random data
Mathematical operations with data:
In [15]: data * 10
Out[15]:
array([[ -2.0471, 4.7894, -5.1944],
[ -5.5573, 19.6578, 13.9341]])
In [16]: data + data
Out[16]: array([[-0.4094, 0.9579, -1.0389],
[-1.1115, 3.9316, 2.7868]])
Shape and dtype
An ndarray is a generic multidimensional container for
homogeneous data; that is, all of the elements must be
the same type.
a shape, a tuple indicating the size
of each dimension
Every array has a dtype, an object describing the
data type of the array

In [17]: data.shape In [18]: data.dtype

Out[17]: (2, 3) Out[18]:dtype('float64')


Creating ndarrays
● The easiest way to create an array is to use the array function.
● This accepts any sequence-like object (including other arrays)
and produces a new NumPy array containing the passed data.
● For example, a list is a good candidate for conversion:

In [19]: data1 = [6, 7.5, 8, 0, 1]


In [20]: arr1 = np.array(data1)
In [21]: arr1
Out[21]: array([ 6. , 7.5, 8. , 0. , 1. ])
Creating ndarrays
Nested sequences, like a list of equal-length lists, will be
converted into a multidimensional array:
In [22]: data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
In [23]: arr2 = np.array(data2)
In [24]: arr2
Out[24]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
Inspecting the ndim and shape attributes:

In [25]: arr2.ndim
Out[25]: 2
In [26]: arr2.shape
Out[26]: (2, 4)
Array creation functions

In addition to np.array, there are a number of other


functions for creating new arrays.

As examples, zeros and ones create values or 1s,


respectively, with a given length or shape.
empty creates an array without initializing its values
to any particular value.

To create a higher dimensional array with these


methods, pass a tuple for the shape:
Array creation functions-EXAMPLES
In [29]: np.zeros(10) In [31]: np.empty((2, 3,
2))
Out[29]: array([ 0., 0., 0., 0., 0., Out[31]:
0., 0., 0., 0., 0.]) array([[[ 0., 0.],
In [30]: np.zeros((3, 6)) [ 0., 0.],
[ 0., 0.]],
Out[30]: [[ 0., 0.],
array([[ 0., 0., 0., 0., 0., 0.], [ 0., 0.],
[ 0., 0., 0., 0., 0., 0.], [ 0., 0.]]])
[ 0., 0., 0., 0., 0., 0.]])

arange is an array-valued version of the built-in Python range function:


In [32]: np.arange(15)
Out[32]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
Array creation functions
What are dtypes in NumPy?

dtype stands for data type — it 🔍 Why is dtype important?


defines what kind of data is stored
Controls how data is stored in memory.
in a NumPy array and how much
Helps NumPy work efficiently with binary
memory each element uses.
data (like files from C, Fortran, databases,
sensors).
Ensures speed and compatibility with lower-
level languages like C and Fortran.
Data Types for ndarrays

The data type or dtype is a special object containing the information (or
metadata, data about data) the ndarray needs to interpret a chunk of
memory as a particular type of data:

In [33]: arr1 = np.array([1, 2, 3], dtype=np.float64)


In [34]: arr2 = np.array([1, 2, 3], dtype=np.int32)
In [35]: arr1.dtype
Out[35]: dtype('float64')
In [36]: arr2.dtype
Out[36]: dtype('int32')
Example 1: Checking and Understanding dtype

import numpy as np

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


print(arr.dtype)

Output:
int64 # This means: integer, 64 bits = 8 bytes per value
NumPy data types
Casting of data types using- astype
You can explicitly convert or cast an array from one dtype to another
using ndarray’s astype method:

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


In [38]: arr.dtype
Out[38]: dtype('int64')
In [39]: float_arr = arr.astype(np.float64)
In [40]: float_arr.dtype
Out[40]: dtype('float64')
Floating-point numbers to be of integer dtype, the decimal part will be truncated:

In [41]: arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])


In [42]: arr
Out[42]: array([ 3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
In [43]: arr.astype(np.int32)
Out[43]: array([ 3, -1, -2, 0, 12, 10], dtype=int32)
String to other dtype
If you have an array of strings representing numbers, you can use
astype to convert them to numeric form:
In [44]: numeric_strings = np.array(['1.25', '-9.6',
'42'], dtype=np.string_)
In [45]: numeric_strings.astype(float)
Out[45]: array([ 1.25, -9.6 , 42. ])
Arithmetic with NumPy Arrays
• Arrays are important because they enable you to express batch operations
on data without writing any for loops.
• NumPy users call this vectorization. Any arithmetic operations between
equal-size arrays applies the operation element-wise:

In [51]: arr = np.array([[1., In [53]: arr * arr In [54]: arr - arr


2., 3.], [4., 5., 6.]]) Out[53]: Out[54]:
In [52]: arr array([[ 1., 4., 9. ], array([[ 0., 0., 0.],
Out[52]: array([[ 1., 2., 3.], [ 16., 25., 36.]]) [ 0., 0., 0.]])
[ 4., 5.,
Arithmetic operations with scalars
Arithmetic operations with scalars propagate the scalar argument to each
element in the array:

In [55]: 1 / arr In [56]: arr ** 0.5

Out[55]: Out[56]:

array([[ 1. , 0.5 , 0.3333], array([[ 1. , 1.4142, 1.7321],

[ 0.25 , 0.2 , 0.1667]]) [ 2. , 2.2361, 2.4495]])


Comparisons between arrays of the same size yield boolean arrays:

In [57]: arr2 = np.array([[0., 4., 1.], [7., 2., 12.]])


In [58]: arr2
Out[58]: array([[ 0., 4., 1.],
[ 7., 2., 12.]])
In [59]: arr2 > arr
Out[59]: array([[False, True, False],
[ True, False, True]], dtype=bool)
Operations between differently sized arrays is called broadcasting
Basic Indexing and Slicing
Selecting a subset of your data or individual elements. One-
dimensional arrays are simple; on the surface they act
In [60]: arr = np.arange(10) In [63]: arr[5:8]
similarly to Python lists:
In [61]: arr Out[63]: array([5, 6, 7])
Out[61]: array([0, 1, 2, 3, 4, 5, 6, 7, In [64]: arr[5:8] = 12
8, 9]) In [65]: arr
In [62]: arr[5] Out[65]: array([ 0, 1, 2, 3, 4, 12,
Out[62]: 5 12, 12, 8, 9])
Broadcasting
● As you can see, if you assign 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 Python’s built-in 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 array.
● To give an example of this, I first create a slice of arr:

In [66]: arr_slice = arr[5:8]


In [67]: arr_slice
Broadcasting
Values in arr_slice, the mutations are reflected in the original array
arr:

In [68]: arr_slice[1] = 12345


In [69]: arr
Out[69]: array([ 0, 1, 2, 3, 4, 12, 12345, 12, 8, 9])
The “bare” slice [:] will assign to all values in an array:
In [70]: arr_slice[:] = 64
In [71]: arr
Out[71]: array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9])
For higher dimensional array
● With higher dimensional arrays, you have many more options.
● In two-dimensional array, the elements at each index are no longer
scalars but rather one-dimensional arrays:

In [72]: arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])


In [73]: arr2d[2]
Out[73]: array([7, 8, 9])

● pass a comma-separated list of indices to select individual


elements. So these are equivalent:
In [74]: arr2d[0][2] In [75]: arr2d[0, 2]
Out[74]: 3 Out[75]: 3
Indexing elements in a NumPy array
Slicing multidimensional array
In multidimensional arrays, if you omit later indices, the returned object
willZASTYUIOP[]\ YY GGd be a
lower dimensional ndarray consisting of all the data along the higher
dimensions. So in the 2 x2 x 3 array arr3d:
In [76]: arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
AWW`W
arr3d[0] is a 2x3 array:
Out[77]: array([[[ 1, 2, 3],
In [78]: arr3d[0]
[ 4, 5, 6]],
Out[78]:array([[1, 2,LL,.
[[ 7, 8, 9],
3],
[10, 11, 12]]])
[4, 5, 6]])
Copy method

Both scalar values and arrays can be assigned to arr3d[0]:

In [79]: old_values = arr3d[0].copy() In [82]: arr3d[0] = old_values

In [80]: arr3d[0] = 42 In [83]: arr3d

In [81]: arr3d Out[83]: array([[[ 1, 2, 3],

Out[81]: array([[[42, 42, 42], [ 4, 5, 6]],

[42, 42, 42]], [[ 7, 8, 9],

[[ 7, 8, 9], [10, 11, 12]]])

[10, 11, 12]]])


Indexing with slices

Like one-dimensional objects such as Python lists, ndarrays can be sliced


with the familiar syntax:

In [88]: arr
Out[88]: array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9])
In [89]: arr[1:6]
Out[89]: array([ 1, 2, 3, 4, 64])
Indexing with slices
Consider the two-dimensional array from before, arr2d. Slicing this array is a
bit
different: ● As you can see, it has sliced
In [90]: arr2d along axis 0, the first axis.
● A slice, therefore, selects a range
Out[90]: array([[1, 2, 3],
of elements along an axis.
[4, 5, 6], ● It can be helpful to read the
[7, 8, 9]]) expression arr2d[:2] as “select
the first two rows of arr2d.”
In [91]: arr2d[:2]
Out[91]: array([[1, 2, 3],
[4, 5, 6]])
Indexing with slices
In [92]: arr2d[:2, 1:]
pass multiple slices just like you can pass multiple indexes:
Out[92]: array([[2,
3],
[5, 6]])
● By mixing integer indexes and slices, you get lower dimensional slices.
● For example, select the second row but only the first two columns like so:
In [93]: arr2d[1, :2]
Out[93]: array([4, 5])
● Similarly, I can select the third column but only the first two rows like so:
In [94]: arr2d[:2, 2]
Out[94]: array([3, 6])
Two-dimensional array slicing
Note that a colon by itself means to take Assigning to a slice expression
the entire axis, so you can slice only assigns to the whole selection:
higher dimensional axes by doing: In [96]: arr2d[:2, 1:] = 0
In [95]: arr2d[:, :1] In [97]: arr2dOut[97]:
Out[95]: array([[1, 0, 0],
array([[1], [4, 0, 0],
[4], [7, 8, 9]])
[7]])
Two-
dimensional
array slicing
Boolean Indexing
example we have some data in an array and an array of names with
duplicates. Using here the randn function in numpy.random to generate
some random normally distributed data: In [101]: data
Out[101]:
In [98]: names = np.array(['Bob', 'Joe', 'Will',
array([[ 0.0929, 0.2817, 0.769 ,
'Bob', 'Will', 'Joe', 'Joe'])
1.2464],
In [99]: data = np.random.randn(7, 4)
[ 1.0072, -1.2962, 0.275 ,
In [100]: names 0.2289],
Out[100]: [ 1.3529, 0.8864, -2.0016, -
array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], 0.3718],
dtype='<U4') [ 1.669 , -0.4386, -0.5397,
0.477 ],
Boolean Indexing
● Suppose each name corresponds to a row in the data array and we
wanted to select all the rows with corresponding name 'Bob'.
● Like comparisons (such as ==) with arrays are also vectorized.
● Thus, comparing names with the string 'Bob' yields a boolean array:
In [102]: names == 'Bob'
Out[102]: array([ True, False, False, True, False, False, False], dtype=bool)
● This boolean array can be passed when indexing the array:
In [103]: data[names == 'Bob']
Out[103]: array([[ 0.0929, 0.2817, 0.769 , 1.2464],
[ 1.669 , -0.4386, -0.5397, 0.477 ]])
Boolean Indexing- mix and match boolean arrays

● The boolean array must be of the same length as the array axis it’s
indexing.
● You can even mix and match boolean arrays with slices or integers (or
sequences of integers.
● Boolean selection will not fail if the boolean array is not the correct
length.
● In these examples, select from the rows where names == 'Bob' and
In [105]: data[names == 'Bob', 3]
index the columns,
Out[105]: array([ 1.2464, 0.477 ])
In [104]: data[names == 'Bob', 2:]
Out[104]: array([[ 0.769 , 1.2464],
Boolean Indexing- mix and match boolean arrays

To select everything but 'Bob', you can either use != or negate the condition
using ~:
In [106]: names != 'Bob'
Out[106]: array([False, True, True, False, True, True, True], dtype=bool)
In [107]: data[~(names == 'Bob')]
Out[107]: array([[ 1.0072, -1.2962, 0.275 , 0.2289],
[ 1.3529, 0.8864, -2.0016, -0.3718],
[ 3.2489, -1.0212, -0.5771, 0.1241],
[ 0.3026, 0.5238, 0.0009, 1.3438],
[-0.7135, -0.8312, -2.3702, -1.8608]])
Boolean Indexing- mix and match boolean arrays

The ~ operator can be useful when you want to invert a general condition:
In [108]: cond = names == 'Bob'
In [109]: data[~cond]
Out[109]:
array([[ 1.0072, -1.2962, 0.275 , 0.2289],
[ 1.3529, 0.8864, -2.0016, -0.3718],
[ 3.2489, -1.0212, -0.5771, 0.1241],
[ 0.3026, 0.5238, 0.0009, 1.3438],
[-0.7135, -0.8312, -2.3702, -1.8608]])
Multiple boolean conditions
Selecting two of the three names to combine multiple boolean conditions,
use
boolean arithmetic operators like & (and) and | (or):
In [110]: mask = (names == 'Bob') | (names == 'Will')
In [111]: mask
Out[111]: array([ True, False, True, True, True, False, False], dtype=bool)
In [112]: data[mask]
Out[112]: array([[ 0.0929, 0.2817, 0.769 , 1.2464],
[ 1.3529, 0.8864, -2.0016, -0.3718],
[ 1.669 , -0.4386, -0.5397, 0.477 ],
[ 3.2489, -1.0212, -0.5771, 0.1241]])
Selecting data from an array by Boolean indexing always creates a copy of
Setting negative values in data to 0
Setting values with boolean arrays works in a common-sense way. To set all
of the
negative values in data to 0 we need only do:
In [113]: data[data < 0] = 0
In [114]: data
Out[114]:array([[ 0.0929, 0.2817, 0.769 , 1.2464 ],
[ 1.0072, 0. , 0.275 ,
0.2289 ],
[ 1.3529, 0.8864, 0. ,
0. ],
[ 1.669 , 0. , 0. ,
Transposing Arrays
Transposing is a special form of reshaping that similarly returns a view on
the underlying data without copying anything.
Arrays have the transpose method and also the special T attribute:
In [126]: arr = np.arange(15).reshape((3, 5))
In [128]: arr.T
In [127]: arr
Out[128]:
Out[127]:
array([[ 0, 5, 10],
array([[ 0, 1, 2, 3, 4 ],
[ 1, 6, 11],
[ 5, 6, 7, 8, 9 ],
[ 2, 7, 12],
[10, 11, 12, 13, 14]])
[ 3, 8, 13],
[ 4, 9, 14]])
Transposing Arrays
When doing matrix computations, you may do this very often—for example,
when
In [131]: np.dot(arr.T, arr)
computing the inner matrix product using np.dot:
Out[131]:
In [129]: arr = np.random.randn(6, 3)
array([[ 9.2291, 0.9394, 4.948 ],
In [130]: arr [ 0.9394, 3.7662, -
Out[130]: 1.3622],
[ 4.948 , -1.3622,
array([[-0.8608, 0.5601, -1.2659],
4.3437]])
[ 0.1198, -1.0635, 0.3329],
[-2.3594, -0.1995, -1.542 ],
[-0.9707, -1.307 , 0.2863],
[ 0.378 , -0.7539, 0.3313],
Higher dimensional arrays, transpose

For higher dimensional arrays, transpose will accept a tuple of axis numbers
to permute the axes (for extra mind bending):
In [132]: arr = np.arange(16).reshape((2, 2, 4))
In [133]: arr In [134]: arr.transpose((1, 0, 2))
Out[133]: Out[134]:
array([[[ 0, 1, 2, 3], array([[[ 0, 1, 2,
3],
[ 4, 5, 6, 7]], [ 8, 9, 10, 11]],
[[ 8, 9, 10, 11], [[ 4, 5, 6, 7],
[12, 13, 14, 15]]]) [12, 13, 14, 15]]])
Swapping Axes
Simple transposing with .T is a special case of swapping axes. ndarray has
the method swapaxes, which takes a pair of axis numbers and switches the
indicated axes to rearrange In [136]: arr.swapaxes(1, 2)
the data: Out[136]:
array([[[ 0, 4],
In [135]: arr
[ 1, 5],
Out[135]: [ 2, 6],
array([[[ 0, 1, 2, 3], [ 3, 7]],
[[ 8, 12],
[ 4, 5, 6, 7]],
[ 9, 13],
[[ 8, 9, 10, 11], [10, 14],
[12, 13, 14, 15]]]) [11, 15]]])
Universal Functions: Fast Element-Wise Array
Functions

A universal function, or ufunc, is a function that performs


element-wise operations on data in ndarrays.

You can think of them as fast vectorized wrappers for simple


functions that take one or more scalar values and produce
one or more scalar results.

Many ufuncs are simple element-wise transformations, like


sqrt or exp:
Universal Functions- unary ufuncs
In [137]: arr = np.arange(10)
In [138]: arr
Out[138]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [139]: np.sqrt(arr)
Out[139]: array([ 0. , 1. , 1.4142, 1.7321, 2. , 2.2361, 2.4495, 2.6458, 2.8284, 3. ])
In [140]: np.exp(arr)
Out[140]: array([ 1. , 2.7183, 7.3891, 20.0855, 54.5982, 148.4132, 403.4288,
1096.6332, 2980.958 , 8103.0839])
Universal Functions: binary ufuncs

In [141]: x = np.random.randn(8)
In [142]: y = np.random.randn(8)
In [143]: x
Out[143]: array([-0.0119, 1.0048, 1.3272, -0.9193, -1.5491, 0.0222, 0.7584, -0.6605])
In [144]: y
Out[144]: array([ 0.8626, -0.01 , 0.05 , 0.6702, 0.853 , -0.9559, -0.0235, -2.3042])
In [145]: np.maximum(x, y)
Out[145]: array([ 0.8626, 1.0048, 1.3272, 0.6702, 0.853 , 0.0222, 0.7584, -0.6605])
Here, numpy.maximum computed the element-wise maximum of the
Unary universal functions
Binary universal functions
Mathematical and Statistical Methods

A set of mathematical functions that compute


statistics about an entire array or about the data
along an axis are accessible as methods of the
array class.
You can use aggregations (often called reductions)
like sum, mean, and std (standard deviation) either
by calling the array instance method or using the
top-level NumPy function.
Mathematical and Statistical Methods

In [177]: arr = np.random.randn(5, 4) In [179]: arr.mean()


In [178]: arr Out[179]: 0.19607051119998253
Out[178]: In [180]: np.mean(arr)
array([[ 2.1695, -0.1149, 2.0037, 0.0296], Out[180]: 0.19607051119998253
[ 0.7953, 0.1181, -0.7485, 0.585 ], In [181]: arr.sum()
[ 0.1527, -1.5657, -0.5625, -0.0327], Out[181]: 3.9214102239996507
[-0.929 , -0.4826, -0.0363, 1.0954],
[ 0.9809, -0.5895, 1.5817, -0.5287]])
cumsum and cumprod
cumsum and cumprod do not ● In multidimensional arrays,
aggregate, instead producing an array accumulation functions like
of the intermediate results. cumsum return an array of
In [184]: arr = np.array([0, 1, 2, 3, 4, 5, 6, 7]) ● the same size, but with the
In [185]: arr.cumsum() partial aggregates computed
Out[185]: array([ 0, 1, 3, 6, 10, 15, 21, 28]) along the indicated axis
● according to each lower
dimensional slice:
cumsum and cumprod
In [186]: arr = np.array([[0, 1, 2], [3, 4, In [188]: arr.cumsum(axis=0)
Out[188]: array([[ 0, 1, 2],
5], [6, 7, 8]])
[ 3, 5, 7],
In [187]: arr
[ 9, 12, 15]])
Out[187]: array([[0, 1, 2], In [189]: arr.cumprod(axis=1)
[3, 4, 5], Out[189]: array([[ 0, 0, 0
[6, 7, 8]]) ],
[ 3, 12,
60 ],
[ 6, 42,
336 ]])
Basic array statistical methods
Sorting

Like Python’s built-in list type, NumPy arrays can be sorted in-place with the sort
method:
In [195]: arr = np.random.randn(6)
In [196]: arr
Out[196]: array([ 0.6095, -0.4938, 1.24 , -0.1357, 1.43 , -0.8469])
In [197]: arr.sort()
In [198]: arr
Out[198]: array([-0.8469, -0.4938, -0.1357, 0.6095, 1.24 , 1.43 ])
Sorting
Sorting each one-dimensional section of values in a multidimensional array
inplace along an axis by passing the axis number to sort:

In [199]: arr = np.random.randn(5, In [201]: arr.sort(1)


3) In [202]: arr
In [200]: arr Out[202]:
Out[200]: array([[-0.2555, 0.6033, 1.2636],
array([[ 0.6033, 1.2636, -0.2555], [-0.9616, -0.4457,
[-0.4457, 0.4684, -0.9616], 0.4684],
[-1.8245, 0.6254, 1.0229], [-1.8245, 0.6254,
[ 1.1074, 0.0909, -0.3501], 1.0229],
Unique and Other Set Logic

NumPy has some basic set operations for one-dimensional ndarrays. A


commonly
used one is np.unique, which returns the sorted unique values in an array:
In [206]: names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
Contrast np.unique with the
In [207]: np.unique(names)
pure Python alternative:
Out[207]: array(['Bob', 'Joe', 'Will'], dtype='<U4')
In [210]: sorted(set(names))
In [208]: ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])
Out[210]: ['Bob', 'Joe', 'Will']
In [209]: np.unique(ints)
Out[209]: array([1, 2, 3, 4])
Array set operations

You might also like