NUMPY

NUMPY


Introduction to NumPy in Python

NumPy or Numerical Python is a general-purpose array processing python package for scientific computing. It consists of numerous powerful features inclusive of:

A robust multi-dimension array object with many useful functions.
Many tools for integrating other?programming languages?along with an enormous number of routines, including shape manipulation, logical, mathematical & many more to operate on?NumPy Array objects.
Alongside its obvious scientific usage, NumPy is also utilized as a generic multi-dimension data container.
A wide set of databases can also be integrated with NumPy.
What is NumPy in Python?

NumPy comes as a conventional package with the latest version of Anaconda ( Download from?https://www.anaconda.com/distribution/#download-section, for Windows, Mac & Linux )

If in case it’s not available, you can always download and install it using the below command in the Anaconda Power shell Prompt.

python -m pip install numpy

After you run the above command, you should see the command line output as “Successfully Installed”

NumPy is one of the most widely used packages in?Python, which lets you?create many derived columns utilizing the existing columns within the dataset.

For Example: if you want to calculate the deviation in monthly sales with respect to the average sales of the entire year.

?Popular Course in this category
Pandas and NumPy Tutorial (4 Courses, 5 Projects)
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (7,146 ratings)
Course Price
?4999??27999

View Course
Related Courses
Python Training Program (39 Courses, 13+ Projects)Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)Angular JS Training Program (9 Courses, 7 Projects)
Examples of NumPy in Python

Let’s discuss some more examples and how to achieve the same using NumPy:

The very first step would be to import the package within the code:

Import NumPy as np

Hit “Shift + Enter” to import the specified package

NumPy is aliased as “np”, which can be utilized to refer to NumPy for any further references

Example #1 – Creating NumPy Arrays

Let’s create a one-dimensional array with the name “a” and values as 1,2,3

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

This will utilize the “array” attribute out of the NumPy module (which we have aliased as “np” over here )

Use the “print” attribute to print the values of a variable/object.

print(a)

The output will print the one-dimensional array “a” as:

[1 2 3]

Use the “type” attribute to verify the type of any variable/object created explicitly.

type(a)

The output will print the object type of one-dimensional array “a” as:

NumPy.ndarray

Similarly, 2-d & 3-d arrays can be initiated using the below commands:

2-D NumPy Arrays

b = np.array([(1.5,2,3), (4,5,6)], dtype = float)

Here “dtype” explicitly specifies the data type of the 2-d array as “float.”

The output of print (b)and type(b)will be as follows:

3-D NumPy Arrays

c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype = float)

Here “dtype” explicitly specifies the data type of the 2-d array as “float.”

The output of print(c) and type(c)will be as follows:

Example #2 – Arithmetic Operation over NumPy Arrays

Let’s initialize one dimension arrays down below:

x = np.array ( [5,6,7] )
y = np.array ( [2,3,8] )

Subtraction

NumPy array subtraction operation follows the usual mathematical syntax as mentioned below. If we want to subtract array “y” from array “x”, then it’s written as:

Result = x - y

Use print(Result) to print the resultant array “Result.”

An alternative to the above approach is to make use of the “subtract” attribute from the NumPy Module & store the resultant array in “Result” like below:

Result = np.subtract(x,y)

Addition

NumPy array addition operation also follows a similar mathematical syntax as discussed earlier in the case of subtraction. If we want to add array “y” to “x”, then it’s written as:

Result = x + y

Use print(Result) to print the resultant array “Result.”

An alternative to the above approach is to make use of the “add” attribute from the NumPy Module & store the resultant array in “Result” like below:

Result = np.add(x,y)

Division

If we want to add array “x” by “y”, then it’s written as:

Result = x + y

Use print(Result) to print the resultant array “Result.”

An alternative to the above approach is to make use of the “divide” attribute from the NumPy Module & store the resultant array in “Result” like below:

Result = np.divide(x,y)

Multiplication

?If we want to multiply array “x” with “y”, then it’s written as:

Result = x * y

Use print(Result) to print the resultant array “Result.”

An alternative to the above approach is to make use of the “divide” attribute from the NumPy Module & store the resultant array in “Result” like below:

Result = np.multiply(x,y)

Exponentiation

exp(x)

Square root?

sqrt(x)

Sine & Cosine

sin(x)


cos(b)

Example #3 – Transforming NumPy Arrays

Operations such as subsetting, slicing, boolean indexing can be?applied to NumPy arrays.

Subsetting

Fetching a single element out of an array by using the indices. The index in NumPy arrays starts from 0.

a = np.array( [4,6,9] )

So if we want to fetch the very first element of array “a”, then it will be written as

a[0]

This will return the very first value, which is 4.

Let’s initialize a 2-D array.

a = np.array([(1,2,3), (4,5,6)], dtype = int)

If we want to fetch the 2nd value from the first row of a 2-D array, then it will be written as:

a[0][1]

This will return the value 2.

Slicing

NumPy Arrays can be sliced in multiple ways. Some of these are mentioned below:

a = np.array( [4,6,9] )

If we want to fetch the first two elements of an array, then it’s written as:

a[0:2]

Here the catch is that a[x:y]

x represents the index from where you need to fetch the elements.
Whereas y represents ” the number of elements in that array to be fetched.”

so the result of a[0:2] will be [4,6]

Boolean Indexing

It enables us to index a?NumPy array?based on a logical conditional. For example, return all the values less than 2 in an array.

a = np.array( [4,1,9] )

The same will be implemented as:

a[a<2]

The output of this logical indexing will be any value within the array “a” that is less than 2

so the result will be [1]

Conclusion

NumPy is quite a powerful library within python, quite widely utilized specifically in Data Explanatory analysis (to understand data better), Data Manipulation, and cleansing. To create KPI Metrices and much more.n        

要查看或添加评论,请登录

Darshika Srivastava的更多文章

  • BigQuery

    BigQuery

    BigQuery is a cloud-based data warehouse platform provided by Google. Categorized as a Platform as a Service (PaaS), it…

  • Google Cloud Platform

    Google Cloud Platform

    What is Google Cloud Platform used for (GCP)? Google Cloud Platform is a set of Computing, Networking, Storage, Big…

  • SQL Databases

    SQL Databases

    What are SQL Databases? SQL databases or relational database management systems (RDBMS) are the storage and management…

  • Data cleansing

    Data cleansing

    Data cleansing is the process of correcting inaccurate, missing, duplicate, or otherwise erroneous data in a data set…

  • Real-time optimization (RTO

    Real-time optimization (RTO

    What is Real-Time Optimization? Real-time optimization (RTO) is the process of optimizing a system or process in…

  • TM1

    TM1

    What is TM1? Table Manager 1 (TM1) is a multidimensional, in-memory online analytical processing (OLAP) database with a…

  • Marketing analytics

    Marketing analytics

    Marketing analytics is the practice of gathering and reviewing metrics to get a better understanding of whether your…

  • Loss forecasting

    Loss forecasting

    What is Loss Forecasting? Definition: Purpose: Importance: Key Factors in Loss Forecasting: Historical Data: Exposure…

  • LGD Model

    LGD Model

    Loss Given Default (LGD) models play a crucial role in credit risk measurement. These models estimate the potential…

  • CCAR ROLE

    CCAR ROLE

    What is the Opportunity? The CCAR and Capital Adequacy role will be responsible for supporting the company’s capital…

社区洞察

其他会员也浏览了