1

I want such type of output

    [[ 1.  0.  0.]
     [ 0.  1.  0.]
     [ 0.  0.  1.]]

But I am getting This

    [[1. 0. 0.]
     [0. 1. 0.]
     [0. 0. 1.]]

My code is this :

import numpy
print(numpy.identity(size))
2
  • There are controls in np.set_print_options. But if you are a numpy beginner, I'd suggest focusing your learning elsewhere. Commented Aug 31, 2020 at 17:50
  • thispointer.com/… Commented Aug 31, 2020 at 18:00

2 Answers 2

1

This should do the trick:

numpy.set_printoptions(formatter={'all': lambda x: " {:.0f} ".format(x)})

If you want to add the decimal point replace " {:.0f} " by " {:.0f}. "

You can modify the number of spaces or formatting in general in the lambda function.

Sign up to request clarification or add additional context in comments.

2 Comments

actually it's adjusting spacing between the elements but it converts 1. to 1.0 and 0. to 0.0 which i don't want.
@Nitish770 I edited the post, depending wether you need the decimal point or not.
-1

use numpy.arrange(start, stop, size) as follow

import numpy as np

arr1 = np.array([[1,   2,   3,   4,   5], [6,  7,  8,  9,  0]])

arr2 = np.arange(1, 9, 1)

print(arr2)

output

[1 2 3 4 5 6 7 8]

1 Comment

I don't think he's interested in the numeric space, but rather the print display of a 2d array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.