Python NumPy

Python Tutorial @ Home
Python Tutorial
Advance Python Tutorial

Python NumPy

What is Python NumPy
How to install Python NumPy
create a NumPy array
Add elements to an NumPy Array

Python Numpy 2

Updating single dimension numpy array

We can update the array using the index

import numpy as np
ar=np.array([1,2,3,4,5])
print(ar)
ar[0]=100
ar[1]=200
ar[2]=300
print(ar)

Output:

[1 2 3 4 5]
[100 200 300   4   5]

Inserting element in  single dimension numpy array

In NumPy, we can also use the insert() method to insert an element or column. The difference between the insert() and the append() method is that we can specify at which index we want to add an element when using the insert() method but the append() method adds a value to the end of the array.
Consider the example below:

import numpy as np
a=np.array([1, 2, 3])
print(a)
newArray =np.insert(a, 1, 90)
print(newArray)

Output:

[1 2 3]
[ 1 90  2  3]

Another example:

import numpy as np
a = np.array([1, 2, 3])
b = np.insert(a, 1, 90)
print(b)
c=np.insert(b,2,100)
print(c)

Output:

[ 1 90  2  3]
[  1  90 100   2   3]
>>> 

Delete an element

You can delete a NumPy array element using the delete() method of the NumPy module:

This is shown in the example below:

import numpy as np
a=np.array([1, 2, 3])
print(a)
b=np.delete(a, 1, axis = 0)
print(b)

Output:

[1 2 3]
[1 3]

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

shape:
The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it.

import numpy as np
ar=np.array([1,2,3,4,5])
print(ar)
print(type(ar))
print(ar.shape)

Output:

[1 2 3 4 5]
<class 'numpy.ndarray'>
(5,)

Visitor Counter

0183280