If you are venturing into data science or machine learning, you will most likely encounter the Python library NumPy. NumPy is an open-source library that is widely used in scientific computing and data analysis. It provides robust support for multi-dimensional arrays and matrices, and a wide range of mathematical functions to operate on them. Here are some of the most commonly used functions that can help you get started with NumPy.
Arrays are at the core of NumPy. Creating arrays in NumPy is straightforward, and there are several ways to do it. The most common way of creating arrays is by using the array()
function, which takes in a list or tuple and returns an array. For example, to create a 1-dimensional array with three elements, you can use the following code:
import numpy as np
my_array = np.array([1, 2, 3])
If you want to create a two-dimensional array with three rows and two columns, you can use a nested list:
my_2d_array = np.array([[1, 2], [3, 4], [5, 6]])
NumPy provides a wide range of mathematical functions to operate on arrays. These functions are optimized for numerical operations and are much faster than their Python counterparts. Some of the most commonly used mathematical functions include:
np.add()
: Adds two arrays element-wisenp.subtract()
: Subtracts two arrays element-wisenp.multiply()
: Multiplies two arrays element-wisenp.divide()
: Divides two arrays element-wiseHere is an example of how to use the add()
function to add two arrays:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.add(a, b)
print(c)
# output:
[5, 7, 9]
NumPy provides several functions for manipulating arrays. Here are some commonly used functions:
np.reshape()
: Reshapes an array into a new shapenp.concatenate()
: Joins two or more arraysnp.transpose()
: Transposes an arrayHere is an example of how to use the reshape()
function to reshape an array:
a = np.array([1, 2, 3, 4, 5, 6])
b = np.reshape(a, (2, 3))
print(b)
# output:
[[1, 2, 3],
[4, 5, 6]]
Here is an example of how to use the concatenate()
function to join two arrays:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.concatenate((a, b))
print(c)
# output:
[1, 2, 3, 4, 5, 6]
Here is an example of how to use the transpose()
function to transpose an array:
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.transpose(a)
print(b)
# output:
[[1, 3, 5],
[2, 4, 6]]
NumPy provides functions to extract individual or range of elements based on index and conditions:
Here are few examples,
Creating Multidimensional array
array = np.random.randint(20, size=(4, 5)) #4 rows, 5 columns
print(array)
# output:
[[11, 1, 2, 10, 17],
[10, 11, 14, 3, 4],
[19, 12, 5, 7, 13],
[18, 15, 2, 9, 7]]
To fetch first row, first element:
print(array[0][0])
# output:
11
To extract a subset of an array,
print(array[0:3][:2])
# output:
[[11, 1],
[10, 11],
[19, 12]]
To extract one column from an array,
print(array[:,:1])
# output:
[[11],
[10],
[19],
[18],]
To extract one row from an array,
print(array[2:3,:])
# output:
[[19, 12, 5, 7, 13]]
To extract elements based on values’
print(array[array>15])
# output:
[17, 19, 18]
NumPy provides functions for performing set operations like union and intersect:
np.union1d()
: Creates a array of unique elements in two arrays.np.intersect1d()
: Finds the intersection between two array.Here is an example for both
a = np.array([1, 2, 0])
b = np.array([1, 3, 4])
print(np.union1d(a, b))
# output:
[0, 1, 2, 3, 4]
print(np.intersect1d(a, b))
# output:
[1]
NumPy provides functions for finding and manipulating dates:
np.datetime64()
: We can get the date in any format.np.is_busday()
: To find the business day. By default, it considers saturday and sunday as not business days, however, it is customizable.Here is an example for both
today = np.datetime64("today")
print(today)
# output:
2023-03-19
print(np.is_busday(np.datetime64('today')))
# output:
False
#How to generate a range of dates:
np.arange('2023-01', '2023-02', dtype='datetime64[W]') #W - Week, replace with D
# output:
['2022-12-29' '2023-01-05' '2023-01-12' '2023-01-19']
NumPy provides several functions for creating distribution. Here are some commonly used distribution functions:
np.random.normal()
: Creates a normal distribution based on mu(mean) and sigma(variance)np.random.uniform()
: Creates a uniform distributionnp.random.binomial
: Creates a binomial distribution based on n(trials) and p(probability).Here is an example of how to use the normal()
function to create a normal distribution using an array:
import numpy as np
import matplotlib.pyplot as plt
mu, sigma = 0, 0.1 #mean and variance
z = np.random.normal(mu, sigma, 100000)
plt.hist(z)
plt.show()
Normal Distribution
Here is an example of how to use the uniform()
function to create a uniform distribution using an array:
z = np.random.uniform(0, 10, 100000)
plt.hist(z)
plt.show()
Uniform Distribution
Here is an example of how to use the binomial()
function to create a binomial distribution using an array:
n, p = 10, 0.5
z = np.random.binomial(n, p, 1000000)
plt.hist(z)
plt.show()
Binomial Distribution
These are just a few of the many functions that NumPy provides. With these functions, you can perform a wide range of operations on arrays and matrices. NumPy is a powerful library that can help you streamline your data analysis and machine learning workflows.
Don’t forget to subscribe to our newsletter to stay up-to-date with the latest news and updates on data science tools and techniques!
...
Feedback is welcomed 💬