Skip to content

atleast_2d axis argument option #12336

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

Open
scottstanie opened this issue Nov 5, 2018 · 2 comments · May be fixed by #18386
Open

atleast_2d axis argument option #12336

scottstanie opened this issue Nov 5, 2018 · 2 comments · May be fixed by #18386

Comments

@scottstanie
Copy link

Would it be possible to add some option to the atleast_2d function so that you can specify where you'd like the newaxis to be placed? I'd like to be able to add the newaxis as the last dimension to force the array to a column vector.

My use case is that I want to have a column vector, but the input may appear as (N,k), or an (N,) array. If I use atleast_2d, then I would need to transpose the result if the original shape was (N,), but shouldn't transpose if the shape was already a column.

Reproducing code example:

import numpy as np
>>> A = np.empty((5, 3))
>>> rhs1 = np.arange(10).reshape((5, 2))
>>> rhs2 = np.arange(5)

>>> A[:, (0, 1)] = rhs1
>>> A[:, [2]] = rhs2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shape mismatch: value array of shape (5,) could not be broadcast to indexing result of shape (1,5)

# Other option:
>>> A[:, [2]] = np.atleast_2d(rhs2).T
>>> A[:, [0, 1]] = np.atleast_2d(rhs1).T
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shape mismatch: value array of shape (1,5) could not be broadcast to indexing result of shape (1,5)

# Ideally what I want:
>>> A[:, [0, 1]] = np.atleast_2d(rhs1, axis=-1)
>>> A[:, [2]] = np.atleast_2d(rhs2, axis=1)
@scottstanie
Copy link
Author

Though I'm seeing now that #7804 is trying to add a pos argument, which would be what I want- not sure how close that is to getting merged though

@peci1
Copy link

peci1 commented Mar 7, 2024

As a (not so nice) workaround, you can use:

A = np.atleast_2d(A.T).T

If A is 2D, it will get twice transposed, i.e. not touched at all. If A is 1D, transpose has no effect, so it makes it a row vector and then transposes to column vector.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants