0% found this document useful (0 votes)
30 views9 pages

Numpy Arrays: Operations and Examples

Data Science

Uploaded by

abesalok123
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)
30 views9 pages

Numpy Arrays: Operations and Examples

Data Science

Uploaded by

abesalok123
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

Ex. No.

2 Numpy Arrays

Problem Statement:
Various operations on Numpy Arrays.
Aim:
To write a python program to perform various operations on numpy arrays.

Algorithm:
Step 1: Start the program.

Step 2: Create 1D, 2D, 3D, N Dimensional arrays using numpy.

Step 3: Perform various basic operations on numpy arrays.

Step 4: Stop the program.

Program:

1.​ Creation of Array: It is the collection of items of the same type.

# Creation of 1D Array
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
# Creation of 2D Array
import numpy as np
arr = [Link]([[1, 2, 3], [4, 5, 6]])
print(arr)
# Creation of 3D Array
import numpy as np
arr = [Link]([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
# Creation of N-Dimensional Array
import numpy as np
a = [Link]([1,2,3,4,5,6,7,8,9,10,11,12], ndmin = 3)
print(a)
# Creation of 1D Array using arange function
import numpy as np
a=[Link](0,10)
print(a)
print([Link])

# Other way to create 2D Array


import numpy as np
a=[Link](0,10).reshape(2,5)
print(a)
print([Link])

# Create 1D array with all elements as 0


import numpy as np
a=[Link](5, dtype=int)
print(a)
print(a[1])
print([Link])

# Create 2D array with all elements as 1


import numpy as np
a=[Link]((2,5),dtype=int)
print(a)

2.​ Accessing Array Elements:

# Using 1D Array
import numpy as np
arr = [Link]([1, 2, 3, 4])
print(arr[0])

# Using 2D Array
import numpy as np
arr = [Link]([[1,2,3,4,5], [6,7,8,9,10]])
print('5th element on 2nd row: ', arr[1, 4])
print('Last element from 2nd dim: ', arr[1, -1])

# Using 3D Array
import numpy as np
arr = [Link]([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])

3.​ Array Slicing :

# First Example
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[4:])

# Second Example
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[Link])
# Third Example
import numpy as np
arr = [Link]([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 2])

4.​ Dimension Shape and Data Type of an Array

# First Example
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print([Link])
print([Link])
print([Link])
# Second Example
import numpy as np
arr = [Link]([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print([Link])
print([Link])
print([Link])

5.​ Array Reshaping

# First Example
import numpy as np
arr = [Link]([1, 2, 3, 4, 5,6])
print([Link](3,2))

# Second Example
import numpy as np
arr = [Link]([1,2,3,4,5,6,7,8,9,10,11,12])
print([Link](2,3,2))

# Flatten Function Example


import numpy as np
a = [Link]([[40, 32, 67], [61, 79, 15]])
print('Original array:\n', a)
b = [Link]()
print('Flattened array:\n', b)
b[1] = 1000
print('Original array after making changes in Flattened array:\n', a)
print('Modified Flattened array:\n', b)

# Ravel Function Example


import numpy as np
x = [Link]([[40, 32, 67], [61, 79, 15]])
print('Original array:\n', x)
y = [Link]()
print('Flattened array:\n', y)
y[1] = 1000
print('Original array after making changes in Flattened array:\n', x)
print('Modified Flattened array:\n', y)

6.​ Random Value Generation using NumPy

# Random Number Generation using Numpy


import numpy as np
a=[Link](0,10)
print(a)

# 1D Array using Random Number Generation


import numpy as np
a=[Link](0,100,10)
print(a)
print([Link]())
print([Link]())
print([Link]())
print([Link]())
print([Link]())

# 2D Array using Random Number Generation


import numpy as np
a=[Link](0,100,(3,3))
print(a)

Output:

1.​ Creation of Array:

# Creation of 1D Array
[1 2 3 4 5]
# Creation of 2D Array
[[1 2 3]
[4 5 6]]
# Creation of 3D Array
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
# Creation of N-Dimensional Array
[[[ 1 2 3 4 5 6 7 8 9 10 11 12]]]
# Creation of 1D Array using arange function
[0 1 2 3 4 5 6 7 8 9]
1
# Other way to create 2D Array
[[0 1 2 3 4]
[5 6 7 8 9]]
2
# Create 1D array with all elements as 0
[0 0 0 0 0]
0
1
# Create 2D array with all elements as 1
[[1 1 1 1 1]
[1 1 1 1 1]]

2.​ Accessing Array Elements:

# Using 1D Array
1
# Using 2D Array
5th element on 2nd row: 10
Last element from 2nd dim: 10
# Using 3D Array
6
3.​ Array Slicing :

# First Example
[5 6 7]
# Second Example
[2 4]
# Third Example
[3 8]

4.​ Dimension Shape and Data Type of an Array

# First Example
1
(5,)
int32
# Second Example
3
(2, 2, 3)
int32

5.​ Array Reshaping

# First Example
[[1 2]
[3 4]
[5 6]]
# Second Example
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
# Flatten Function Example
Original array:
[[40 32 67]
[61 79 15]]
Flattened array:
[40 32 67 61 79 15]
Original array after making changes in Flattened array:
[[40 32 67]
[61 79 15]]
Modified Flattened array:
[ 40 1000 67 61 79 15]
# Ravel Function Example
Original array:
[[40 32 67]
[61 79 15]]
Flattened array:
[40 32 67 61 79 15]
Original array after making changes in Flattened array:
[[ 40 1000 67]
[ 61 79 15]]
Modified Flattened array:
[ 40 1000 67 61 79 15]

6.​ Random Value Generation using NumPy

# Random Number Generation using Numpy


4
# 1D Array using Random Number Generation
[93 36 51 64 24 67 38 86 70 5]
93
5
53.4
0
9
# 2D Array using Random Number Generation
[[54 56 5]
[89 28 9]
[80 81 88]]

Result:

Thus the above program has been implemented and verified successfully.

You might also like