Python-NumPy
1.
Write
the command for the following:
(a) Print the NumPy version.
(b) Create a Null array X of size 10.
(c)
Create
a 1D Y array with values ranging from 10 to 49
2.
Create
an array starting from 0 with an interval of 2 till 20.
3.
Convert
a 1D array to a 2D array with 2 rows
4.
Create
3 X 3 identity matrix
5.
Create
a 3 X 3 matrix with values ranging from 0 to 8
6.
Create
a 3 X 3 NumPy array of all True’s
7.
Create
a 1D array of length 10 & all elements initialized with value 7.
8.
Create
a 2D array of 4 rows and 5 columns and all elements initialized with value 9.
9.
Write a
NumPy program to test whether none of the elements of a given array is zero
10.
Write a
NumPy program to create an array of 10 zeros, 10 ones, 10 fives.
11.
Write
Write a NumPy program to create a 10X 10 matrix, in which the elements on the
borders will be equal to 1 and inside 0.
12.
Write a
NumPy program to compute sum of all elements, sum of each column and sum of
each row of a given array
13.
Write a
Numpy program to convert a given array into a list and then convert it into a
list again
14.
Write a NumPy program to create a null vector
of size 10 and update sixth value to 11.
15.
Write a
NumPy program to create a 3X3 matrix with values ranging from 2 to 10
16.
Write a
NumPy program to access an array by column
17.
How can
we create a Boolean array?
18.
Write a
NumPy program to extract all odd numbers from an array.
19.
Write a
NumPy program to replace all odd numbers in an array with -1 without changing
it.
20.
Write a
NumPy program to convert a 1D array with 2 rows.
# Write a program to print all
odd numbers from NumPy array. For example, if an array a contains the following
values:
[10,
21, 4, 45, 66, 93] then result will be:
The odd numbers are: 21 45 93
#Program to print odd numbers
from a NumPy array
import numpy as np
a=np.array([10,21,4,45,66,93])
for num in a:
if num%2!=0:
print(num,"\t",end="")
#Write
a program to copy the content of an array A to another array B, replacing all
odd numbers of array A with -1 without altering the original array A. Also,
print both the arrays. For example, if the input array A is:
The
array A is [10 51 2 18 4 31 13 5 23 64 29]
The
result will be:
The
array B is [10 -1 2 18 4 -1 -1 -1 -1 64 -1]
import numpy as np
A=np.array([10,21,4,45,66,93])
B=np.zeros(A.size, dtype=int) # Create array B of integer
type with same size of A
ctr=0 #
to set the index position in array B
for num in A:
if num%2!=0: # if the number is odd
B[ctr]=-1
else:
B[ctr]=num
ctr=ctr+1
print("Tha Array A
is", A)
print("Tha Array B
is", B)
Output:
The
Array A is [10 21 4 45 66 93]
The
Array B is [10 -1 4 -1 66 -1]
# Python code to find mean of every numpy array in list
# Importing module
import numpy as np
# List Initialization
Input = [np.array([1, 2, 3]),
np.array([4,
5, 6]),
np.array([7,
8, 9])]
# Output list
initialization
Output = []
# using np.mean()
for i in range(len(Input)):
Output.append(np.mean(Input[i]))
# Printing output
print(Output)
#Write a Python
program to create an array of 5 integers and display the array items. Access
individual element through indexes.
array_num = [1,3,5,7,9]
for i in array_num:
print(i)
print("Access first three items individually")
print(array_num[0])
print(array_num[1])
print(array_num[2])
1
3
5
7
9
Access first three
items individually
1
3
5
# Write a Python program to append a new item to the end of the array.
array_num = [1, 3, 5, 7, 9]
print("Original array: "+str(array_num))
print("Append 11 at the end of the array:")
array_num.append(11)
print("New array: "+str(array_num))
Original array: [1, 3, 5, 7, 9]Append 11 at the end of the array:New array: [1, 3, 5, 7, 9, 11]
Original array: [1, 3, 5, 3, 7, 1, 9, 3]
Reverse the order of the items:
[3, 9, 1, 7, 3, 5, 3, 1]
Original array: [1, 3, 5, 3, 7, 9, 3]
Number of occurrences of the number 3 in the said array: 3
Original array: [1, 3, 5, 7, 9]
Insert new value 4 before 3:
New array: [1, 4, 3, 5, 7, 9]
Original array: [1, 3, 5, 7, 9]
Remove the third item form the array:
New array: [1, 3, 7, 9]
Original array: [1, 3, 5, 3, 7, 1, 9, 3]
Remove the first occurrence of 3 from the said array:
New array: [1, 5, 3, 7, 1, 9, 3]
x
Original array: [1, 3, 5, 3, 7, 1, 9, 3]
Reverse the order of the items:
[3, 9, 1, 7, 3, 5, 3, 1]
Original array: [1, 3, 5, 3, 7, 9, 3]
Number of occurrences of the number 3 in the said array: 3
Original array: [1, 3, 5, 7, 9]
Insert new value 4 before 3:
New array: [1, 4, 3, 5, 7, 9]
Original array: [1, 3, 5, 7, 9]
Remove the third item form the array:
New array: [1, 3, 7, 9]
Original array: [1, 3, 5, 3, 7, 1, 9, 3]
Remove the first occurrence of 3 from the said array:
New array: [1, 5, 3, 7, 1, 9, 3]
x
# Write a Python
program to reverse the order of the items in the array.
array_num = [1, 3, 5, 3, 7, 1, 9, 3]
print("Original array: "+str(array_num))
array_num.reverse()
print("Reverse the order of the items:")
print(str(array_num))
# Write a Python
program to get the number of occurrences of a specified element in an array.
array_num = [1, 3, 5, 3, 7, 9, 3]
print("Original array: "+str(array_num))
print("Number of occurrences of the number 3 in the said
array:
"+str(array_num.count(3)))
"+str(array_num.count(3)))
# Write a Python program to insert a new item before the second element in an existing array.
array_num = [1, 3, 5, 7, 9]
print("Original array: "+str(array_num))
print("Insert new value 4 before 3:")
array_num.insert(1, 4)
print("New array: "+str(array_num))
# Write a Python program to remove a specified item using the index from an array.
array_num =[1, 3, 5, 7, 9]
print("Original array: "+str(array_num))
print("Remove the third item form the array:")
array_num.pop(2)
print("New array: "+str(array_num))
# Write a Python program to remove the first occurrence of a specified element from an array
array_num =
[1, 3, 5, 3, 7, 1, 9, 3]
print("Original array: "+str(array_num))
print("Remove the first occurrence of 3 from the said
array:")
array_num.remove(3)
print("New array: "+str(array_num))
Comments
Post a Comment