English

Using the arrays created in Question 4 above, write NumPy commands for the following: a) Use NumPy. split() to split the array myarray2 into 5 arrays columnwise.

Advertisements
Advertisements

Question

Using the arrays created in Question 4 above, write NumPy commands for the following:

  1. Use NumPy. split() to split the array myarray2 into 5 arrays columnwise. Store your resulting arrays in myarray2A, myarray2B, myarray2C, myarray2D and myarray2E. Print the arrays myarray2A, myarray2B, myarray2C, myarray2D and myarray2E.
  2. Split the array zeros at array index 2, 5, 7, 8 and store the resulting arrays in zerosA, zerosB, zerosC and zerosD and print them.
  3. Concatenate the arrays myarray2A, myarray2B and myarray2C into an array having 3 rows and 3 columns.
Code Writing
Advertisements

Solution

a) 

myarray2A, myarray2B, myarray2C, myarray2D, myarray2E = np.split(myarray2, 5, axis=1)
print("myarray2A:")
print(myarray2A)
print("myarray2B:")
print(myarray2B)
print("myarray2C:")
print(myarray2C)
print("myarray2D:")
print(myarray2D)
print("myarray2E:")
print(myarray2E)

Output:

myarray2A:
[[ 4.]
 [24.]
 [44.]]
myarray2B:
[[ 8.]
 [28.]
 [48.]]
myarray2C:
[[12.]
 [32.]
 [52.]]
myarray2D:
[[16.]
 [36.]
 [56.]]
myarray2E:
[[20.]
 [40.]
 [60.]]

b)

zerosA, zerosB, zerosC, zerosD, zeroE = np.split(zeros, [2, 5, 7, 8])
print("zerosA:\n", zerosA)
print("zerosB:\n", zerosB)
print("zerosC:\n", zerosC)
print("zerosD:\n", zerosD)

Output:

zerosA:
[0. 0.]
zerosB:
[0. 0. 0.]
zerosC:
[0. 0.]
zerosD:
[0.]

c) 

reshaped = np.concatenate((myarray2A, myarray2B, myarray2C), axis=1).reshape(3, 3)
print("Concatenated and Reshaped Array:\n", reshaped)

Output:

Concatenated  and reshaped array:

[[4. 8. 12.]
[24. 28. 32.]
[44. 48. 52.]]
 
shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Introduction to NumPy - Exercise [Page 114]

APPEARS IN

NCERT Informatics Practices [English] Class 11
Chapter 6 Introduction to NumPy
Exercise | Q 8. | Page 114
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×