• +91-9872993883
• +91-8283824812
• info@ris-ai.com

Python Operations on Numpy Arrays

Numpy is used for mathematical calculations . Numpy is faster than list the reason is that when we use numpy array,array stores less memory as compared to list and in numpy array what happens is values are stored in continous manner that is one after the another and in list what happens is that values are stored randomly without any sequence and it occupies larger space than array when used with numpy.

Numpy is used in many high level calculations which manually done is not possible and is time consuming so with the help of numpy many things have been sorted out efficiently. Before starting with complex things in numpy a person should be able to perform basic operations and above mentioned are basic operations before learning complex things in numpy. If we are clear with the basic then we can do any type of complex calculations gievn to us. In above operations we have performed:

1. Size of an array
2. Shape of an array
3. Reshape of an array
4. Transpose of an array
5. One dimonsional array
6. Two dimonsional array
7. Data type
8. Range of an array with arithmetic operations
9. Square root of an array
11. Subtracting one element
12. Multiplying an element with 5
13. Squaring an element with 3
14. Sorting an element
15. Concatenation of an element
16. deletion of an element
In [29]:
import numpy as np

In [30]:
a=np.array([1,2,3])
print ("array a is :", a)
print("size of an array is:",a.size)

array a is : [1 2 3]
size of an array is: 3

In [32]:
a=np.array([(1,2,3,4),(3,4,5,6)])
print("shape of an array:",a)
print("reshape of an array:",a.reshape(4,2))

shape of an array: [[1 2 3 4]
[3 4 5 6]]
reshape of an array: [[1 2]
[3 4]
[3 4]
[5 6]]

In [31]:
a = np.array([[1,2],[3,4]])
print ("transposed array a is:", a.T)

transposed array a is: [[1 3]
[2 4]]

In [69]:
b=np.array([5,6,9])
print("one dimensional array:",b.ndim)

one dimensional array: 1

In [86]:
a=np.array([[1,2],[3,4],[5,6]])
print("two dimensional array:",a.ndim)

two dimensional array: 2

In [72]:
print("types of data type used:",a.dtype)

types of data type used: int64

In [73]:
a=np.array([20,30,40,50])
b=np.arange(4)
print("range created:",b)

range created: [0 1 2 3]

In [74]:
c=a-b
print("final result after using c=a-b:",c)

final result after using c=a-b: [20 29 38 47]

In [75]:
print("result of square root:",b**2)

result of square root: [0 1 4 9]

In [76]:
a=np.array([1,2,3,4,5])
print("after adding 1 to each element:",a+1)

after adding 1 to each element: [2 3 4 5 6]

In [77]:
a=np.array([1,2,3,4,5])
print("after subtracting -1 from each element:",a-1)

after subtracting -1 from each element: [0 1 2 3 4]

In [78]:
a=np.array([1,2,3,4])
print("after multiplying 5 with each element:",a*5)

after multiplying 5 with each element: [ 5 10 15 20]

In [85]:
a=np.array([1,2,3,4,5])
print("after squaring each element with 3:",a**3)

after squaring each element with 3: [  1   8  27  64 125]

In [19]:
f=np.array([2,1,8,9,3,5,7,10,4])
print(" result of sorting an element:",np.sort(f))

 result of sorting an element: [ 1  2  3  4  5  7  8  9 10]

In [9]:
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
print("after concatenate of two elements:",np.concatenate((a,b)))

after concatenate of two elements: [1 2 3 4 5 6 7 8]

In [28]:
S=np.array([2,1,8,9,3,5,7,10,4])

print("after deletion of  an element:",np.delete (S,2))

after deletion of  an element: [ 2  1  9  3  5  7 10  4]

In [ ]:



Conclusion:

This guide provides you with several tools that you can use to manipulate arrays . Hope you are clear with all that has been provided in this article . Make sure you practice as much as possible.

Strengthen your foundations as a Data Scientist with the RIS AI .