Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Python Arrays

Uploaded by

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

Python Arrays

Uploaded by

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

Use of popular Python packages

NumPy
Arrays
NumPy
• NumPy is a Python library used for working with arrays.
• It also has functions for working in domain of linear algebra, functions fourier
transform, and matrices.
• It is an open source project and you can use it freely.
• NumPy stands for Numerical Python.

• Create a NumPy array: 1D array

• The array object in NumPy is called ndarray


• Create a NumPy array: 2D array

• Create a NumPy array: 3D array


Check Number of Dimensions?
• NumPy Arrays provides the ndim attribute that returns an integer that tells us how many
dimensions the array have.

Access Array Elements


• You can access an array element by referring to its index number.
Access 2-D Arrays
• Think of 2-D arrays like a table with rows and columns, where the dimension represents the
row and the index represents the column.
NumPy Array Slicing
• 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].

The result includes the start index, but excludes the end index.
Try this also
Slicing 2-D Arrays
❖ From the second element, slice elements from index 1 to index 4 (not
included):

❖ From both elements, return index 2:


From both elements, slice index 1 to index 4 (not included), this will return a 2-D array:

Checking the Data Type of an Array


Converting Data Type on Existing Arrays
The astype() function creates a copy of the array, and allows you to specify the data type
as a parameter.
NumPy Array Copy vs View

The copy SHOULD NOT be affected by the changes made to the original array, where
as the view SHOULD be affected by the changes made to the original array.
Shape of an Array

The shape of an array is the number of elements in each dimension.


NumPy arrays have an attribute called shape that returns a tuple with each index having
the number of corresponding elements.
NumPy Array Reshaping
Reshaping means changing the shape of an array.

Reshape From 1-D to 2-D


Flattening the arrays
We can use reshape(-1) to do this.
NumPy Array Iterating
• Iterating means going through elements one by one.
• we can do this using for loop of python.
To return the actual values, the scalars, we have to iterate the arrays in each
dimension.

Iterating Arrays Using nditer()


Appending values at the end of an NumPy array
For this task, we can use numpy.append() and numpy.concatenate(). This function
can help us to append a single value as well as multiple values at the end of the
array.

Appending a Single Value to a 1D Array


Appending Another Array at the End of a 1D Array
Appending Values at the End of the N-Dimensional Array
Matrix manipulation in Python
In python matrix can be implemented as 2D list or 2D Array. Forming matrix from
latter, gives the additional functionalities for performing various operations in matrix.
Eigen values of a matrix
np.eigvals() method

Output ??
Determinant of a matrix using NumPy
numpy.linalg.det() function.

Output:
Inverse of a matrix using NumPy
np.linalg.inv() function

You might also like