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

Numpy Notes

python concepts

Uploaded by

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

Numpy Notes

python concepts

Uploaded by

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

Features of Numpy -

● Multidimensional Array Support -


At the core of NumPy is the ndarray object, which supports efficient multidimensional arrays
of homogeneous data.

● Broadcasting -
NumPy supports broadcasting, allowing arithmetic operations on arrays of different shapes.

● Mathematical and Statistical Functions -


Offers a comprehensive set of mathematical functions (e.g., sin, cos, exp) and statistical
tools (e.g., mean, median, std).

● High Performance -
NumPy is implemented in C, making it much faster than Python lists for numerical
computations.

● Linear Algebra Support -


Provides functions for linear algebra, such as matrix multiplication, eigenvalues, and matrix
inversion.

● Random Number Generation -


Includes utilities for generating random numbers for simulations and modeling.

● Advanced Indexing and Slicing -


Enables powerful ways to access and modify data using boolean masks, fancy indexing, and
slices.

● Broadcasting and Vectorization -


Vectorized operations avoid explicit loops, making code more concise and efficient.

● Shape Manipulation -
Supports reshaping and manipulating the dimensions of arrays with methods like reshape,
ravel, and transpose.

● Compatibility with Other Libraries -


Integrates seamlessly with libraries like Pandas, Scikit-learn, TensorFlow, and Matplotlib.

1
Numpy Basics
Sr. Function Description Sample code to use function Output of the sample code
no. Name

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


list or iterable

2 np.arange Creates an array of evenly arr = np.arange(0, 10, 2) [0 2 4 6 8]


spaced values within a
specified range

Parameters - (start, end,


size)

3 np.linspace Creates an array of evenly arr = np.linspace(0, 1, 5) [0. 0.25 0.5 0.75 1. ]
spaced values over a
specified interval.

Parameters - (start, end,


no. of sample)

4 np.random.rand Generates an array of arr = np.random.rand(3, 2) Random 3x2 array with


random numbers values in [0, 1). For example
uniformly distributed -
between 0 and 1.
[[0.1 0.5]
Parameters - Shape of the [0.15 0.11 ]
output array [0.8 0.2 ]]

5 np.zeros Creates an array filled arr = np.zeros((3, 4)) 3x4 array of zeros
with zeros.
[[0. 0. 0. 0.]
Parameters - Shape of the [0. 0. 0. 0.]
array [0. 0. 0. 0.]]

6 np.ones Creates an array filled arr = np.ones((2, 3)) 2x3 array of ones
with ones.
[[1. 1. 1.]
Parameters - Shape of the [1. 1. 1.]]
array

7 np.reshape Reshapes an array without arr = np.array([1, 2, 3, 4, 5, 6]) [ [1 2 3]


changing its data. reshaped = arr.reshape(2, 3) [4 5 6] ]
print(reshaped)
Parameter - Desired shape
of the output array

2
8 np.sum Computes the sum of the arr = np.array([[1, 2], [3, 4]])
array elements. print(np.sum(arr)) 10

print(np.sum(arr, axis=1)) [3 7] - # if axis=1 is provided

9 np.dot Computes the dot product a = np.array([1, 2]) 11


of two arrays. b = np.array([3, 4])
print(np.dot(a, b)) Explanation - (1*3) + (2*4) =
Parameters - 11
a,b - Input arrays.

10 np.argmax Returns the indices of the arr = np.array([1, 3, 2]) 1


maximum values along an print(np.argmax(arr))
axis.

11 np.where Returns indices where a arr = np.array([1, 2, 3, 4]) (array([2, 3]), )


condition is true. indices = np.where(arr > 2)
print(indices)
Parameter - any condition

3
Numpy Advanced
Sr. Function Name Description Sample code to use function Output of the sample code
no.

1 np.concatenate Joins arrays along an a = np.array([[1, 2], [3, 4]]) [[1 2]


existing axis. b = np.array([[5, 6]]) [3 4]
result = np.concatenate((a, b), [5 6]]
axis=0)
print(result)

2 np.split Splits an array into arr = np.array([1, 2, 3, 4, 5, 6]) [array([1, 2]), array([3, 4]),
multiple sub-arrays. result = np.split(arr, 3) array([5, 6])]
print(result)
Parameters - Input
array to split and
number of sections to
split into.

3 np.tile Constructs an array by arr = np.array([1, 2, 3]) [[1 2 3 1 2 3 1 2 3]


repeating another result = np.tile(arr, (2, 3)) [1 2 3 1 2 3 1 2 3]]
array. print(result)

Parameters - Input
array and no. of
repetitions along each
axis

4 np.flatten Returns a copy of an arr = np.array([[1, 2], [3, 4]]) [1 2 3 4]


array collapsed into result = arr.flatten()
one dimension. print(result)

5 np.meshgrid Creates a coordinate x = np.array([1, 2, 3]) [[1 2 3]


matrix from two or y = np.array([4, 5]) [1 2 3]]
more coordinate X, Y = np.meshgrid(x, y)
vectors. print(X) [[4 4 4]
print(Y) [5 5 5]]
Parameters - 1-D
arrays representing
the grid coordinates.

6 np.linalg.inv Computes the inverse mat = np.array([[1, 2], [3, 4]]) [[-2. 1. ]
of a matrix. inv_mat = np.linalg.inv(mat) [ 1.5 -0.5]]
print(inv_mat)
Parameters - Square
matrix

4
7 np.linalg.eig Computes the mat = np.array([[1, 2], [2, 3]]) Eigenvalues: [-0.23606798
eigenvalues and right eigenvalues, eigenvectors = 4.23606798]
eigenvectors of a np.linalg.eig(mat)
square array. Eigenvectors:
print("Eigenvalues:", [[-0.85065081
Parameters - Square eigenvalues) -0.52573111]
Matrix [ 0.52573111 -0.85065081]]
print("Eigenvectors:\n",
eigenvectors)

8 np.dot Computes the dot a = np.array([[1, 2], [3, 4]]) [[19 22]
product of two arrays. b = np.array([[5, 6], [7, 8]]) [43 50]]
result = np.dot(a, b)
Parameters - print(result)
a, b - Input arrays.

9 np.cov Estimates the data = np.array([[1, 2, 3], [4, 5, [[1. 1.]


covariance matrix of 6]]) [1. 1.]]
the input array. cov_matrix = np.cov(data)
print(cov_matrix)

10 np.histogram Computes the data = np.array([1, 2, 1, 2, 3, 4, 5, Histogram: [4 2 2]


histogram of a 6])
dataset. hist, bin_edges = Bin Edges:
np.histogram(data, bins=3) [1. 2.66666667
Parameters - Input 4.33333333 6. ]
array and no. of bins print("Histogram:", hist)

print("Bin Edges:", bin_edges)

5
Mathematical Methods in Numpy
Sr. Function Description Sample code to use Output of the sample
no. Name function code

1 np.add Performs element-wise a = np.array([1, 2, 3]) [5 7 9]


addition of two arrays. b = np.array([4, 5, 6])
result = np.add(a, b)
Parameters - a,b Input print(result)
arrays

2 np.multiply Performs element-wise a = np.array([1, 2, 3]) [ 4 10 18]


multiplication. b = np.array([4, 5, 6])
result = np.multiply(a, b)
Parameters - a,b Input print(result)
arrays

3 np.power Raises elements of the array a = np.array([1, 2, 3]) [1 4 9]


to the power of result = np.power(a, 2)
corresponding elements. print(result)

Parameters - Input array


and exponent

4 np.sqrt Computes the non-negative a = np.array([1, 4, 9]) [1. 2. 3.]


square root of each element. result = np.sqrt(a)
print(result)
Parameters - Input element
or an array

5 np.exp Computes the exponential a = np.array([0, 1, 2]) [1. 2.71828183


of each element. result = np.exp(a) 7.3890561 ]
print(result)
Parameters - Input array

6 np.log Computes the natural a = np.array([1, 2.718, [0. 0.99989632


logarithm of each element. 7.389]) 1.99999241]
result = np.log(a)
Parameters - Input array print(result)
(Positive elements)

7 np.mean Computes the arithmetic arr = np.array([1, 2, 3, 4]) 2.5


mean. result = np.mean(arr)
print(result)
Parameters - Input array

6
8 np.median Computes the median of the arr = np.array([1, 3, 2, 4]) 2.5
array elements. result = np.median(arr)
print(result)
Parameters - Input array

9 np.var Computes the variance of arr = np.array([1, 2, 3, 4]) 1.25


the array elements. result = np.var(arr)
print(result)
Parameters - Input array

10 np.std Computes the standard arr = np.array([1, 2, 3, 4]) 1.118033988749895


deviation. result = np.std(arr)
print(result)
Parameters - Input array

11 np.percentile Computes the nth arr = np.array([1, 2, 3, 4, 5]) 3.0


percentile of the array. result = np.percentile(arr,
50)
Parameters - Input array print(result)
and Percentile to compute
(0-100)

12 np.corrcoef Computes the Pearson x = np.array([1, 2, 3]) [[1. 1.]


correlation coefficient y = np.array([4, 5, 6]) [1. 1.]]
matrix. result = np.corrcoef(x, y)
print(result)
Parameters - Input arrays

13 np.cov Computes the covariance data = np.array([[1, 2, 3], [4, [[1. 1.]
matrix. 5, 6]]) [1. 1.]]
result = np.cov(data)
Parameters - Input array print(result)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy