Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views5 pages

Class1 Py

Uploaded by

ptl.neha786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Class1 Py

Uploaded by

ptl.neha786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

NumPy

NumPy is an open source mathematical and scientific computing library for Python programming
tasks. The name NumPy is shorthand for Numerical Python. The NumPy library offers a collection
of high-level mathematical functions including support for multi-dimensional arrays, masked
arrays and matrices.

NumPy Applications

The following are some common application areas where NumPy is extensively used:

• Data Analysis: In Data analysis, while handling data, we can create data (in the form of array
objects), filter the data, and perform various operations such as mean, finding the standard
deviations, etc.

• Machine Learning & AI: Popular machine learning tools like TensorFlow and PyTorch use NumPy
to manage input data, handle model parameters, and process the output values.

• Array Manipulation: NumPy allows you to create, resize, slice, index, stack, split, and combine
arrays.

• Finance & Economics: NumPy is used for financial analysis, including portfolio optimization, risk
assessment, time series analysis, and statistical modelling.

• Image & Signal Processing: NumPy helps process and analyze images and signals for various
applications.

• Data Visualization: NumPy independently does not create visualizations, but it works with
libraries like Matplotlib and Seaborn to generate charts and graphs from numerical data.

Why is NumPy Faster Than Lists?

NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and
manipulate them very efficiently.

This behavior is called locality of reference in computer science.

Analyze InfoTech, Janpriya Complex, Phase-II, Tedhi Puliya, Aliganj Lucknow Ph. No.9453193268,9839434821
E-mail. [email protected], Website: www.analyzeinfotech.in
pg. 1
This is the main Reason why NumPy is faster than lists. Also it is optimized to work with latest CPU
architectures.

Which Language is NumPy written in?

NumPy is a Python library and is written partially in Python, but most of the parts that require fast
computation are written in C or C++.

Create a NumPy ndarray Object

NumPy is used to work with arrays. The array object in NumPy is called ndarray.

We can create a NumPy ndarray object by using the array() function.

NumPy Creating Arrays

Create a NumPy ndarray Object

NumPy is used to work with arrays. The array object in NumPy is called ndarray.

We can create a NumPy ndarray object by using the array() function.

ExampleGet your own Python Server

import numpy as np

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

print(arr)

print(type(arr))

type(): This built-in Python function tells us the type of the object passed to it. Like in above code it
shows that arr is numpy.ndarray type.

Analyze InfoTech, Janpriya Complex, Phase-II, Tedhi Puliya, Aliganj Lucknow Ph. No.9453193268,9839434821
E-mail. [email protected], Website: www.analyzeinfotech.in
pg. 2
To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will
be converted into an ndarray:

Example

Use a tuple to create a NumPy array:

import numpy as np

arr = np.array((1, 2, 3, 4, 5))

print(arr)

Dimensions in Arrays

A dimension in arrays is one level of array depth (nested arrays).

nested array: are arrays that have arrays as their elements.

1-D Arrays

An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.

These are the most common and basic arrays.

Example

Create a 1-D array containing the values 1,2,3,4,5:

import numpy as np

ar= np.array([1, 2, 3, 4, 5])

print(arr)

print(arr[1,2])

2-D Arrays

An array that has 1-D arrays as its elements is called a 2-D array.

These are often used to represent matrix or 2nd order tensors.

Analyze InfoTech, Janpriya Complex, Phase-II, Tedhi Puliya, Aliganj Lucknow Ph. No.9453193268,9839434821
E-mail. [email protected], Website: www.analyzeinfotech.in
pg. 3
NumPy has a whole sub module dedicated towards matrix operations called numpy.mat

Example

Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:

import numpy as np

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

print(arr)

//indexing

print(arr[1,2,3])

3-D arrays

An array that has 2-D arrays (matrices) as its elements is called 3-D array.

These are often used to represent a 3rd order tensor.

example

ar2=np.array([[[1,2,3,4,5],[6,7,8,9,10]],[[11,12,13,14,15],[16,17,18,19,20]]])
print(ar2)
//indexing
print(ar3[1,2,3])

Slicing arrays
Slicing in python means taking elements from one given index to another given index.

We pass slice instead of index like this: [start:end].

We can also define the step, like this: [start:end:step].

Analyze InfoTech, Janpriya Complex, Phase-II, Tedhi Puliya, Aliganj Lucknow Ph. No.9453193268,9839434821
E-mail. [email protected], Website: www.analyzeinfotech.in
pg. 4
Example
import numpy as np
#find index number
#ar=np.array([1,2,3,4,5]) #[start:end:step]
#ar2=np.array([[[1,2,3,4,5],[6,7,8,9,10]],[[11,12,13,14,15],[16,17,18,19,20]]]) #2d array //
matrices
ar3=np.array([1,2,3,4,5,6,7,8,9,10])
print(ar3[1:8:2])

Analyze InfoTech, Janpriya Complex, Phase-II, Tedhi Puliya, Aliganj Lucknow Ph. No.9453193268,9839434821
E-mail. [email protected], Website: www.analyzeinfotech.in
pg. 5

You might also like