Speeding Up Python with NumPy
What is NumPy?
Basically, NumPy is a numerical powerhouse for Python. It adeptly solves complex problems and makes tasks easier to achieve. Additionally, it provides us with highly user-friendly command lines, making array manipulation a breeze. In short, NumPy is used to manipulate arrays effectively.
Why use NumPy ?? / Advantages of NumPy.
It is very difficult to manage multidimensional arrays in Python. Additionally, the compiler's task is also increased when we access 3 or more dimensions in an array.
There are numerous advantages of NumPy
occupies less memory
takes less time
easy to use
more functionality
Best way to learn about NumPy.
To learn NumPy, you should download Anaconda.
Why Anaconda?
Anaconda provides pre-installed libraries in Python, like NumPy, pandas, matplotlib, etc. The concept of cells in Jupyter Notebook helps us create code notes (a sort of code that helps us revise the concept more easily).
BASICS.
importing numpy
We can use any keyword to access NumPy functions. For example, if we use "xyz", we would write our commands as b = xyz.array(a).
repeating the array n no. of times
simply write (a*n)
Creating different arrays
an array where all the elements are "1"
an array where all the elements are "0"
an array where all the elements are the same and you can specify the value of that element
i.e. 4 elements of value "8"
More special arrays
np.arange()
returns evenly spaced values in a given interval.
where in bracket we have to specify ( start, stop, step )
where specifying stop is mandatory but we can skip start and step, they are assumed 0 and 1 respectively
np.linspace()
also evenly spaced values in the specified interval, but here we have to define the no. of values, not the step
where in the bracket we have to specify
(start, stop,num=50,endpoint = True ,restep=False, dtype = none,axis = 0)
specifying start and stop is mandatory rest all are assumed as specified above like num = 50
endpoint - whether you want to include the endpoint you have mentioned or not
num - no. of values you want in an array
restep - the interval between consecutive elements needed to be mentioned or not
np.identity(a)
generates the identity matrix of order "a"
np.eye(a,b)
generates a matrix of order axb where diagonal elements are "1"
np.random.rand()
generates an array of specified no. of elements where all the elements are random float values [ range = 0 --> 1 ]
np.random.randint()
provides the integer values in the given range
SLICING OF AN ARRAY
it means selecting a certain part of an array or in other words cutting out a part of an array the syntax used is
arr[x:y]
where,
arr = name of the array
x = starting index from where to select
y = the end position where we have to select
NOTE: we have to include x but not y
also in 2-D arrays
MATHEMATICAL OPERATIONS
In this topic, we will mainly look at different predefined functions provided by NumPy.
BASIC ARITHMETIC
a = [1,3,5,6] a = a+1 print (a) ------------> a = [2,4,6,7]
a = [x,y] b = [p,q] c = a+b -------------------> c = [x+p , y+q]
SOME FUNCTIONS
sum()
- returns the sum of elements of an array
mean()
- returns the mean of the elements of an array
min()
- return the minimum value present in the array
max()
- return the maximum value present in the array
argmin()
- return the index of the minimum value of an array
argmax()
- return the index of the maximum value of an array
LOGICAL OPERATORS
The logical operations in an array also are simplified by NumPy
Logical operators = "<", ">", "or", "and".
Logical operators are used for conditioning and thus return the boolean values such as True or False.
LOGICAL OPERATORS - AND / OR
In programming languages, True is commonly represented by the value 1, denoting its truth, while False is represented by 0, indicating its falsehood. This binary system simplifies logical calculations, allowing computers to process and handle conditions more efficiently.
AND OPERATOR
- when both the values are True i.e. 1 then and operator returns the value 1, if either of the values is False it returns False i.e. 0.
OR OPERATOR
when either of the values is True it returns true irrespective of the second value,
but returns False if both the values are False or 0.
BOOLEAN INDEXING
Boolean indexing in programming uses True/False values to filter elements from arrays. We apply conditions to access specific elements that meet our criteria, making code easily accessible and efficient.
For example, checking in an array whether the elements are smaller than a certain no. and then printing only those elements which satisfy the condition.
re-writing the code as .....
can be used for more complex functions such as...
MANIPULATING ARRAYS USING BOOLEAN INDEXING
array([ 4, 7, 9, 4, 15, 6, 14, 5, 18, 6])
converting certain indexes to the desired value
getting the index of the elements fulfilling the conditions
using the above-mentioned methods to perform something amazing
BROADCASTING
Dealing with unequal sizes or orders of arrays, in other words, when there are two arrays of different sizes or orders, performing mathematical operations requires certain rules. In this topic, we will learn those rules.
When there are arrays of different orders, such as one with m x n and another with n, operations can be performed simply.
Consider an array with m x n as m arrays with order n; then, the second array will perform the task with each individual array of the first array.
When there are arrays of different orders, such as one with m x n and another with m, operations can be performed simply by taking the transpose of the array with order m x n and converting it to an array of order n x m.
now we can perform all basic operations.....
Subscribe to my newsletter
Read articles from Chirag Sanadhya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Chirag Sanadhya
Chirag Sanadhya
Hey there! I'm Chirag, a student currently pursuing a BTech in Artificial Intelligence and Data Science. I'm really passionate about two things: data science and teaching. I want to be a great data scientist. I also love to teach and help others learn. It's my dream to be really good at both things. I'm always learning and growing, working hard to achieve my goals. I'm excited for the future and all the possibilities it holds. Join me on this journey as I explore the world of data and education. Let's make great things happen together!