w3resource

NumPy: Increase the number of items (10 edge elements) shown by the print statement

NumPy: Array Object Exercise-201 with Solution

Write a NumPy program to create a 90x30 array filled with random point numbers, increase the number of items (10 edge elements) shown by the print statement.

Sample Solution:

Python Code:

# Importing NumPy library
import numpy as np

# Generating a random array of integers between 0 and 9 with a shape of (90, 30)
nums = np.random.randint(10, size=(90, 30))

# Displaying the original array
print("Original array:")
print(nums)

# Increasing the number of edge items shown in the print statement to 10
np.set_printoptions(edgeitems=10)

# Displaying the array with increased number of edge items
print("\nIncrease the number of items (10 edge elements) shown by the print statement:")
print(nums) 

Sample Output:

Original array:
[[1 8 3 ... 9 5 8]
 [1 9 6 ... 6 5 0]
 [0 0 8 ... 6 9 8]
 ...
 [2 3 9 ... 4 0 1]
 [2 6 8 ... 8 3 4]
 [1 2 9 ... 7 4 8]]

Increase the number of items (10 edge elements) shown by the print statement:
[[1 8 3 0 1 2 8 0 0 5 ... 1 3 8 1 1 7 5 9 5 8]
 [1 9 6 1 0 7 7 3 8 6 ... 5 9 9 9 9 7 2 6 5 0]
 [0 0 8 1 4 2 1 3 2 9 ... 5 6 4 1 4 9 0 6 9 8]
 [7 2 7 1 7 2 4 7 2 0 ... 4 2 6 2 6 4 4 0 6 7]
 [3 6 0 0 2 1 6 6 2 9 ... 3 4 1 9 5 7 4 4 6 7]
 [9 5 2 6 9 2 7 7 9 6 ... 4 3 5 2 7 0 6 0 2 7]
 [5 5 4 1 9 2 5 3 5 2 ... 7 4 7 5 5 7 3 0 1 8]
 [5 9 9 9 6 3 5 0 9 4 ... 6 4 9 7 8 7 2 5 9 4]
 [6 1 0 8 9 8 0 6 6 3 ... 5 3 1 6 1 5 2 9 8 3]
 [3 0 3 1 4 1 4 1 9 6 ... 3 9 4 4 5 0 2 0 8 0]
 ...
 [3 9 5 0 3 8 5 3 1 1 ... 1 2 4 6 2 4 5 0 0 5]
 [7 9 7 0 1 3 0 1 8 4 ... 5 7 5 4 7 2 0 2 3 0]
 [4 2 6 4 7 2 3 9 7 9 ... 2 7 1 2 5 7 5 4 0 2]
 [7 8 9 7 3 3 1 2 8 5 ... 1 9 9 5 5 2 3 5 0 1]
 [3 7 6 8 3 2 5 0 5 4 ... 0 4 9 8 5 3 2 1 3 3]
 [3 3 8 4 0 4 8 4 7 0 ... 7 5 0 9 7 8 2 0 7 9]
 [8 4 4 7 3 8 2 0 8 9 ... 5 5 8 1 5 6 0 6 5 0]
 [2 3 9 9 9 8 6 5 4 4 ... 4 0 7 1 8 6 7 4 0 1]
 [2 6 8 7 3 3 8 4 1 6 ... 1 3 9 5 0 1 4 8 3 4]
 [1 2 9 5 1 2 9 9 5 4 ... 3 1 2 6 1 4 4 7 4 8]]

Explanation:

In the above code -

nums = np.random.randint(10, size=(90, 30)): This line creates a 90x30 NumPy array filled with random integers between 0 and 9, inclusive.

np.set_printoptions(edgeitems=10): This line sets the print options for NumPy arrays to display 10 edge items when printing the array.

print(nums): Finally print() function prints the 90x30 NumPy array according to the print options set in the previous step.

Python-Numpy Code Editor:

Previous: Write a NumPy program to remove a specific column from a given array.
Next: Write a NumPy program to calculate the arithmetic means of corresponding elements of two given arrays of same size.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.