Q. Using the arrays created in Question above,

Q. Create the following NumPy arrays:
(a) A 1-D array called zeros having 10 elements and all the elements are set to zero.
(b) A 1-D array called vowels having the elements ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’.
(c) A 2-D array called ones having 2 rows and 5 columns and all the elements are set to 1 and dtype as int.
(d) Use nested Python lists to create a 2-D array called myarray1 having 3 rows and 3 columns and store the following data: 2.7, -2, -190, 3.4, 99.910.6, 0, 13
(e) A 2-D array called myarray2 using arange() having 3 rows and 5 columns with start value = 4, step size 4 and dtype as float.

Write NumPy commands for the following:

a) 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.

b) 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.

c) Concatenate the arrays myarray2A, myarray2B and myarray2C into an array having 3 rows and 3 columns.


Answer :


(a)

To split the array myarray2 into 5 arrays column-wise using np.split() in NumPy, you can specify the axis parameter as 1 to split along the columns.

import numpy as np
myarray2 = np.arange(4, 4 + 3 * 5 * 4, 4, dtype=float).reshape(3, 5)
# Split myarray2 into 5 arrays column-wise
myarray2A, myarray2B, myarray2C, myarray2D, myarray2E = np.split(myarray2, 5, axis=1)
print("myarray2A:")
print(myarray2A)
print("\nmyarray2B:")
print(myarray2B)
print("\nmyarray2C:")
print(myarray2C)
print("\nmyarray2D:")
print(myarray2D)
print("\nmyarray2E:")
print(myarray2E)

(b)

To split the array zeros at specific indices using np.split() in NumPy, you can provide a list of indices where you want to split the array.

zeros = np.zeros((10,))

# Split the array zeros at indices 2, 5, 7, 8
zerosA, zerosB, zerosC, zerosD = np.split(zeros, [2, 5, 7, 8])

print("zerosA:")
print(zerosA)
print("\nzerosB:")
print(zerosB)
print("\nzerosC:")
print(zerosC)
print("\nzerosD:")
print(zerosD)

(c)

To concatenate arrays myarray2A, myarray2B, and myarray2C into a new array with 3 rows and 3 columns in NumPy, you can use the np.concatenate() function

concatenated_array = np.concatenate((myarray2A, myarray2B, myarray2C), axis=1)

3 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post