0% found this document useful (0 votes)
43 views6 pages

????? ??????????

The document provides examples of using NumPy to work with arrays. It demonstrates how to create arrays filled with zeros or ones, slice arrays to extract elements, concatenate and merge arrays, sort arrays, and perform aggregations like finding minimum, maximum and mean values. Some key points covered include creating 1D and 2D arrays, slicing arrays to extract subsets of elements, concatenating arrays along different axes, sorting arrays along an axis, and using NumPy functions to calculate aggregations across array elements.

Uploaded by

Mithun B M
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)
43 views6 pages

????? ??????????

The document provides examples of using NumPy to work with arrays. It demonstrates how to create arrays filled with zeros or ones, slice arrays to extract elements, concatenate and merge arrays, sort arrays, and perform aggregations like finding minimum, maximum and mean values. Some key points covered include creating 1D and 2D arrays, slicing arrays to extract subsets of elements, concatenating arrays along different axes, sorting arrays along an axis, and using NumPy functions to calculate aggregations across array elements.

Uploaded by

Mithun B M
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/ 6

The Basics of NumPy Arrays

1)Create a Numpy array filled with all zeros[1d and 2d]

In [ ]:

import numpy as np
a=np.zeros(3)
print("1D:")
print(a)
b=np.zeros((3,4))
print("2D:")
print(b)

1D:
[0. 0. 0.]
2D:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

2)Create a Numpy array filled with all ones[1d and 2d]

In [ ]:
import numpy as np
a=np.ones(3)
print("1D:")
print(a)
b=np.ones((3,4))
print("2D:")
print(b)

1D:
[1. 1. 1.]
2D:
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]

3)Slice elements from index 4 to the end of the array:

In [ ]:

import numpy as np
x=np.arange(10)
print(x)
print(x[4])

[0 1 2 3 4 5 6 7 8 9]
4

4)Slice from the index 3 from the end to index 1 from the end

In [ ]:

print(x[3:-1])

[3 4 5 6 7 8]

5)write the necessary function to get below output

input-

arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[10, 20], [30, 40]])

output-[ 1 2 3 4 10 20 30 40]
In [ ]:

import numpy as np
arr1=np.array([[1,2],[3,4]])
arr2=np.array([[10,20],[30,40]])
print("arr1:",arr1)
print("arr2:",arr2)
np.concatenate([arr1,arr2],axis=None)

arr1: [[1 2]
[3 4]]
arr2: [[10 20]
[30 40]]

Out[ ]:

array([ 1, 2, 3, 4, 10, 20, 30, 40])

6)arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[10, 20], [30, 40]])

merge above arrays along axis=0 and axis=1

In [ ]:

arr1=np.array([[1,2],[3,4]])
arr2=np.array([[10,20],[30,40]])
a=np.concatenate([arr1,arr2],axis=1)
print(a)
b=np.concatenate([arr1,arr2],axis=0)
print(b)

[[ 1 2 10 20]
[ 3 4 30 40]]
[[ 1 2]
[ 3 4]
[10 20]
[30 40]]

7)Create a numpy array and find the Sum of All the Elements in the Array

In [ ]:

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


total = arr.sum()
print(total)

8)Create a numpy array and find the Sum of Array Elements Along the Axis=0 & axis=1

In [ ]:

arr = np.array([[1, 0, 0],[2, 1, 1]])


r= arr.sum(axis=1)
print(arr)
print("Sum of each row:", r)
arr = np.array([[1, 0, 0],[2, 1, 1]])
c= arr.sum(axis=0)
print(arr)
print("Sum of each column:",c)

[[1 0 0]
[2 1 1]]
Sum of each row: [1 4]
[[1 0 0]
[2 1 1]]
Sum of each column: [3 1 1]

9)Generate a linear sequence from 0.2 (included) until 2 (excluded) with a step size of 0.1, so there will be (2 – 0.2)/0.1 – 1 = 20 elements in the
sequence, which is the length of the resulting numpy array.

In [ ]:

a=np.arange(0.02,2,0.1)
print(a)

[0.02 0.12 0.22 0.32 0.42 0.52 0.62 0.72 0.82 0.92 1.02 1.12 1.22 1.32
1.42 1.52 1.62 1.72 1.82 1.92]
10)Convert the 1-D array with 12 elements into a 4*3 2-D array.

In [ ]:

arr=np.array([1, 2, 3, 4, 5, 6, 7, 8,9 ,10, 11, 12])


arr1=arr.reshape(4,3)
print("2-D array:")
print(arr1)

2-D array:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]

11)Convert the 1-D array with 12 elements into a 2 3 2 3-D array.

In [ ]:

arr=np.array([1, 2, 3, 4, 5, 6, 7, 8,9 ,10, 11, 12])


arr1=arr.reshape(2,3,2)
print("3-D array:")
print(arr1)

3-D array:
[[[ 1 2]
[ 3 4]
[ 5 6]]

[[ 7 8]
[ 9 10]
[11 12]]]

12)Assume the students are sitting in the form of 3x4 array. Write a program to create the array of students from the given array using slicing
concept from Numpy

Create slicing element from below index position:

1. 1nd Person in the 1st row


2. 3th Person in the 2nd row
3. 2rd Person in the 2st row

In [ ]:

students = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print("1st person in the 1st row:", students[0, 0])
print("3rd person in the 2nd row:", students[1, 2])
print("2nd person in the 2nd row:", students[1, 1])

1st person in the 1st row: 1


3rd person in the 2nd row: 7
2nd person in the 2nd row: 6

13)Split the 2-D array into three 2-D arrays.

In [ ]:
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])

newarr = np.array_split(arr, 3)

print(newarr)

[array([[1, 2],
[3, 4]]), array([[5, 6],
[7, 8]]), array([[ 9, 10],
[11, 12]])]

14)Split the 2-D array into three 2-D arrays along rows
In [ ]:

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])

newarr = np.array_split(arr, 3, axis=1)

print(newarr)

[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]

Aggregations: Min, Max, and Everything In Between


1) Write the Python code to print the maximum of 4,12,43.3,19,100

In [ ]:

arr=np.array([4,12,43.3,19,100])
np.max(arr)

Out[ ]:

100.0

2) Write the python code to print the minimum of 4,12,43.3,19,100

In [ ]:

arr=np.array([4,12,43.3,19,100])
np.min(arr)

Out[ ]:

4.0

3) Check whether your able to find the minimum from the given set of values :: 4,12,43.3,19, "HelloProgramming"

In [ ]:

arr= np.array([4,12,43,3,19,"HelloProgramming"])
min(arr)

Out[ ]:

'12'

4) Write the python code to print the word occurring 1st among these in dict:: "GoodMorning", "Evening", "algorithm", "programming"

In [ ]:

dict1 = ["GoodMorning", "Evening", "algorithm", "programming"]


print(min(dict1))

Evening

5) Write the python code to print the min and max values from the given list of tuple: [(2, 3), (4, 7), (8, 11), (3, 6)]
In [ ]:

list_of_tup=[(2,3),(4,7),(8,11),(3,6)]
print("Minimum of the elements at 0th index of the list of tuples:", min(list_of_tup[0]))
print("Maximum of the elements at 0th index of the list of tuples:", max(list_of_tup[0]))
print("Minimum of the elements at 1st index of the list of tuples:", min(list_of_tup[1]))
print("Maximum of the elements at 1st index of the list of tuples:", max(list_of_tup[1]))

Minimum of the elements at 0th index of the list of tuples: 2


Maximum of the elements at 0th index of the list of tuples: 3
Minimum of the elements at 1st index of the list of tuples: 4
Maximum of the elements at 1st index of the list of tuples: 7

SORTING
1)Create a list [[4,3,2],[2,1,4]], convert it to a numpy array and sort it along axis 1

In [ ]:

l= [[4,3,2],[2,1,4]]
arr = np.array(l)
print("List:-\n", l)
print("List converted to a numpy array:-\n", arr)
sortedarr = np.sort(arr, axis=1)
print("Sorted numpy array along axis = 1:-\n", sortedarr)

List:-
[[4, 3, 2], [2, 1, 4]]
List converted to a numpy array:-
[[4 3 2]
[2 1 4]]
Sorted numpy array along axis = 1:-
[[2 3 4]
[1 2 4]]

2)Implement a program to take fruits names from array of fruits. To sort the array in alphabetical manner and display their index position.

In [ ]:

print("Enter 8 fruit names\n")


fruits = list()
for i in range(0,8):
fruit = str(input())
fruits.append(fruit)
f_arr = np.array(fruits)
sortedfruits = np.sort(f_arr)
print("\nAlphabetically sorted fruits:-\n")
for i in range(0,8):
print("Fruit '"+sortedfruits[i]+"': index", i)

Enter 8 fruit names

apple
mango
orange
kiwi
papaya
strawbwrry
banana
blueberry

Alphabetically sorted fruits:-

Fruit 'apple': index 0


Fruit 'banana': index 1
Fruit 'blueberry': index 2
Fruit 'kiwi': index 3
Fruit 'mango': index 4
Fruit 'orange': index 5
Fruit 'papaya': index 6
Fruit 'strawbwrry': index 7

3) Write a NumPy program to partition a given array in a specified position and move all the smaller elements values to the left of the partition,
and the remaining values to the right, in arbitrary order (based on random choice).
In [ ]:

a = np.array([80, 55, 28, 40, -7, 64, 52, 39])


print("Array before partitioning:-")
print(a)
print("\nArray after partitioning at the 4th position:-")
print(np.partition(a, 4))

Array before partitioning:-


[80 55 28 40 -7 64 52 39]

Array after partitioning at the 4th position:-


[-7 39 28 40 52 55 64 80]

You might also like