????? ??????????
????? ??????????
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.]]
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.]]
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]
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[ ]:
6)arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[10, 20], [30, 40]])
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 [ ]:
8)Create a numpy array and find the Sum of Array Elements Along the Axis=0 & axis=1
In [ ]:
[[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 [ ]:
2-D array:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
In [ ]:
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
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])
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]])
print(newarr)
[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
In [ ]:
arr=np.array([4,12,43.3,19,100])
np.max(arr)
Out[ ]:
100.0
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 [ ]:
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]))
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 [ ]:
apple
mango
orange
kiwi
papaya
strawbwrry
banana
blueberry
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 [ ]: