Write a NumPy program to create an array of ones of size n.
Example:
n = 10
A = [ 1 1 1 1 1 1 1 1 1 1]
import numpy as np
n = int(input("put the 1st number = "))
x = int(input("put the 2nd number = "))
a = np.array([])
for i in range(n):
a = np.append(a,x)
print(a)
import numpy as np
n = int(input("put the 1st number = "))
x = int(input("put the 2nd number = "))
a = np.array([])
a = np.ones(n) * x
print(a)
Write a NumPy program to create a zero vector of size n and update sixth value to 11.
Example:
n = 10
A = [ 0 0 0 0 0 0 0 0 0 0]
Update sixth value to 11 :
A = [ 0 0 0 0 0 0 11 0 0 0]
import numpy as np
n = int(input("put a number = "))
a = np.ones(n)*0 # a = np.zeros(n)
print(a)
for i in range(n):
a[6]= 11
print(a)
Write a NumPy program to create a array with values ranging from m to n.
Example:
m = 12
n = 30
Output:
[12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]
import numpy as np
m = int(input("put the 1st number = "))
n = int(input("put the 2nd number = "))
a = np.array([])
for i in range(n-m):
a = np.append(a,m+i) # a = np.arange(m,n)
print(a)
Write a NumPy program to reverse an array (first element becomes last).
Example:
Original array:
[12 13 14 15]
Reverse array:
[15 14 13 12]
import numpy as np
m = int(input("put the 1st number = "))
n = int(input("put the 2nd number = "))
a = np.arange(m,n)
a = a[::-1]
print(a)
Write a NumPy program to convert the values of Centigrade degrees into Fahrenheit degrees.
Centigrade values are stored into a NumPy array. Use the following equation:
C = (F - 32) x 5/9 (Convert Fahenheit into Centigrade)
Example:
A = [0, 12, 45.21 ,34, 99.91]
Expected Output:
Values in Fahrenheit degrees:
[ 0. 12. 45.21 34. 99.91]
Values in Centigrade degrees:
[-17.78 -11.11 7.3389 1.11 37.7278]
import numpy as np
A = np.array([0, 12, 45.21, 34, 99.91])
# A = np.multiply(A - 32,5/9)
A = (A - 32) * 5/9
print(A)
Write a NumPy program to test whether each element of a 1-D array is also present in a second array.
Example:
Array1: [ 0 10 20 40 60]
Array2: [0, 40]
Compare each element of array1 and array2
[ True False False True False]
import numpy as np
A = np.array([0, 10, 20, 40, 60])
B = np.array([0, 40])
c = np.array([]).astype(bool)
n = len(A)
for i in range(n):
if A[i] in B:
t = True
else:
t = False
c = np.append(c,t)
print(c)
import numpy as np
A = np.array([0, 10, 20, 40, 60])
B = np.array([0, 40])
x = np.in1d(A, B)
print(x)
Write a NumPy program to get the unique elements of an array.
Example:
Original array:
[10 10 20 20 30 30]
Unique elements of the above array:
[10 20 30]
import numpy as np
A = np.array([10, 10, 20, 20, 30, 30])
A = set(A)
B = np.array([])
for a in A:
B = np.append(B,a)
print(B)
import numpy as np
A = np.array([10, 10, 20, 20, 30, 30])
x = np.unique(A)
print(x)
Write a NumPy program to find the set difference of two arrays.
The set difference will return the sorted, unique values in array1 that are not in array2.
Example:
Array1: [ 0 10 20 40 60 80]
Array2: [10, 30, 40, 50, 70, 90]
Set difference between two arrays:
[ 0 20 60 80]
import numpy as np
A = np.array([ 0, 10, 20, 40, 60, 80])
B = np.array([10, 30, 40, 50, 70, 90])
x = np.setdiff1d(A, B)
print(x)
Exercise 9:
Write a NumPy program to find the set exclusive-or (xor) of two arrays.
Set exclusive-or will return the sorted, unique values that are in only one (not both)
of the input arrays.
Example:
Array1: [ 0 10 20 40 60 80]
Array2: [10, 30, 40, 50, 70]
Unique values that are in only one (not both) of the input arrays:
[ 0 20 30 50 60 70 80]
import numpy as np
A = np.array([ 0, 10, 20, 40, 60, 80])
B = [10, 30, 40, 50, 70]
A = set(A)
B = set(B)
C = A^B
C = list(C)
C = sorted(C)
C = np.array(C)
print(C)
import numpy as np
A = np.array([ 0, 10, 20, 40, 60, 80])
B = [10, 30, 40, 50, 70]
x = np.setxor1d(A, B)
print(x)
Write a NumPy program to find the union of two arrays.
Union will return the unique, sorted array of values that are in either of
the two input arrays.
Example:
Array1: [ 0 10 20 40 60 80]
Array2: [10, 30, 40, 50, 70]
Unique sorted array of values that are in either of the two input arrays:
[ 0 10 20 30 40 50 60 70 80]
import numpy as np
a = np.array([ 0, 10, 20, 40, 60, 80])
b = [10, 30, 40, 50, 70]
c = np.concatenate((a,b),axis = 0)
c = list(set(c))
c = sorted(c)
c = np.array(c)
print(c)
import numpy as np
a = np.array([ 0, 10, 20, 40, 60, 80])
b = [10, 30, 40, 50, 70]
x = np.union1d(a, b)
print(x)
import numpy as np
a = np.array([ 0, 10, 20, 40, 60, 80])
b = [10, 30, 40, 50, 70]
a = set(a)
b = set(b)
c = a|b
c = list(c)
c = sorted(c)
c = np.array(c)
print(c)
Write a NumPy program to construct an array by repeating n times.
Example:
array: [1, 2, 3, 4]
n = 3
Repeating 3 times
[1 2 3 4 1 2 3 4 1 2 3 4]
import numpy as np
a = [1, 2, 3, 4]
m = len(a)
n = 3
b = []
for i in range(n):
for j in range(m):
b.append(a[j])
b = np.array(b)
print(b)
import numpy as np
a = [1, 2, 3, 4]
a = np.tile(a, 3)
print(a)
Write a NumPy program to get the values and indices of the elements that are bigger than n in a given array.
Example:
n = 10
Original array: [0 10 20 20 30 40]
Values bigger than 10 : [20 20 30 40]
import numpy as np
a = np.array([0, 10, 20, 20, 30, 40])
n = int(input("put a number = "))
m = len(a)
b = np.array([])
for i in range(m):
if a[i]>n:
b = np.append(b,a[i])
print(b)
import numpy as np
a = np.array([0, 10, 20, 20, 30, 40])
n = int(input("put a number = "))
x = a[a>n]
print(x)