-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH: Add shape
control parameter to set_printoptions
#27461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What is wrong with this? I don't think we are going to change something so basic to NumPy at this stage.
|
I'm using jupyter notebook. If you want to look at a complex statement like: Your need to do either: tmp = x[x > 0].sum(...).otherFunc1(...).otherFunc2(...)
print(tmp)
print(tmp.shape) or: print(x[x > 0].sum(...).otherFunc1(...).otherFunc2(...))
print(x[x > 0].sum(...).otherFunc1(...).otherFunc2(...).shape) I want to look at a large array summary and its shape in one line code. |
Maybe good to try to be concrete, since while writing this I completely changed my mind, to thinking this should be done without even an option to turn it on or off. In a jupyter notebook, what you get from
I chose this example with
For this case, the shape cannot be guessed from the data values ( To me, the above makes it seem reasonable to show the shape more generally when it cannot be directly inferred, i.e., also when data has been summarized. I think it would not be particularly hard to do: just an extra bit of logic in p.s. An alternative I thought of would be to allow customization of |
@mhvk This is what I want to say, sometime you are hard to know the shape. I had written myself version for printing, but I had to write it every new notebook. def summary(self: NDArray, include_dtype: bool = False, include_shape: bool = True):
summ = cp.array_repr(self) if hasattr(self, 'device') else np.array_repr(self)
summ_parts = [summ[:-1]]
if include_dtype:
summ_parts.append(f', dtype={self.dtype}')
if include_shape:
summ_parts.append(f', shape={self.shape}')
summ_parts.append(')')
print(''.join(summ_parts)) |
The actual implementation does something fairly similar - except that it also checks the result still fits on the last line. I think it would make sense to adjust it... See numpy/numpy/_core/arrayprint.py Lines 1564 to 1613 in 9c51621
|
I think this is common. I think it may make sense to focus attention on creating an html representation for arrays, which could probably much more naturally fit the shape somewhere. I realize that is a bigger job, but it would be a pretty major improvement! |
I found a solution to my needs, which is def summary(self: NDArray, include_dtype: bool = False, include_shape: bool = True):
summ = cp.array_repr(self) if hasattr(self, 'device') else np.array_repr(self)
summ_parts = [summ[:-1]]
if include_dtype:
summ_parts.append(f', dtype={self.dtype}')
if include_shape:
summ_parts.append(f', shape={self.shape}')
summ_parts.append(')')
return ''.join(summ_parts)
np.set_string_function(summary) > np.arange(1000000).reshape(2000, -1)
array([[ 0, 1, 2, ..., 497, 498, 499],
[ 500, 501, 502, ..., 997, 998, 999],
[ 1000, 1001, 1002, ..., 1497, 1498, 1499],
...,
[998500, 998501, 998502, ..., 998997, 998998, 998999],
[999000, 999001, 999002, ..., 999497, 999498, 999499],
[999500, 999501, 999502, ..., 999997, 999998, 999999]], shape=(2000, 500)) |
That work-around is great! But it still irritated me that the code doesn't "do the right thing" by default, so I made a quick PR that changes the behaviour - see #27482 |
Proposed new feature or change:
I want to be able to print large arrays and display the shape of the array at the same time.
Like this, I don't know what the shape of
x
, shape == (2, ???).The text was updated successfully, but these errors were encountered: