Creating an array by adding to shape of existing array
I have a numpy array:
X = np.array([[1,0,1],
[1,1,1],
[0,1,0],
[1,0,1]])
which has a shape of (4,3)
I would like to change this shape into (4,4)
by adding 1 to the second dimension of the array, via:
X_b = np.ones((X.shape+(0,1)))
but what I get is:
ValueError: could not broadcast input array from shape (4,3) into shape (4,2,0,1)
What is the right way to do it?
Basically I want X_b
to have a shape of (4,4)
if X.shape = (4,3)
python arrays numpy shapes
add a comment |
I have a numpy array:
X = np.array([[1,0,1],
[1,1,1],
[0,1,0],
[1,0,1]])
which has a shape of (4,3)
I would like to change this shape into (4,4)
by adding 1 to the second dimension of the array, via:
X_b = np.ones((X.shape+(0,1)))
but what I get is:
ValueError: could not broadcast input array from shape (4,3) into shape (4,2,0,1)
What is the right way to do it?
Basically I want X_b
to have a shape of (4,4)
if X.shape = (4,3)
python arrays numpy shapes
add a comment |
I have a numpy array:
X = np.array([[1,0,1],
[1,1,1],
[0,1,0],
[1,0,1]])
which has a shape of (4,3)
I would like to change this shape into (4,4)
by adding 1 to the second dimension of the array, via:
X_b = np.ones((X.shape+(0,1)))
but what I get is:
ValueError: could not broadcast input array from shape (4,3) into shape (4,2,0,1)
What is the right way to do it?
Basically I want X_b
to have a shape of (4,4)
if X.shape = (4,3)
python arrays numpy shapes
I have a numpy array:
X = np.array([[1,0,1],
[1,1,1],
[0,1,0],
[1,0,1]])
which has a shape of (4,3)
I would like to change this shape into (4,4)
by adding 1 to the second dimension of the array, via:
X_b = np.ones((X.shape+(0,1)))
but what I get is:
ValueError: could not broadcast input array from shape (4,3) into shape (4,2,0,1)
What is the right way to do it?
Basically I want X_b
to have a shape of (4,4)
if X.shape = (4,3)
python arrays numpy shapes
python arrays numpy shapes
edited Nov 21 '18 at 20:35
tel
7,47621431
7,47621431
asked Nov 21 '18 at 20:11
oakcaoakca
392114
392114
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
To fix your code, do this instead:
X_b = np.ones(X.shape + np.array((0,1)))
The catch here is that X.shape
returns a plain Python tuple
. By adding (0,1)
you were actually performing tuple concatenation, instead of pairwise addition like you intended.
Of course, you could also just stick an extra column on to your existing array with append
:
X_b = np.append(X, [[1]]*X.shape[0], axis=1)
X_b[:,:-1] = X
and then useX_b
as input... probably doing the same thing. your answer looks good. Just waiting the minutes
– oakca
Nov 21 '18 at 20:17
@oakca assumingX_b
was created withnp.ones
, then yep. The one "gotcha" is thatX_b
will be of dtypefloat64
, which is the default fornp.ones
. Thus thedtype
s ofX
andX_b
may not match (eg ifX.dtype==int
)
– tel
Nov 21 '18 at 20:41
Ornp.hstack((X, np.ones((4,1), dtype=np.int)))
, which I find easier to visualize.
– xnx
Nov 21 '18 at 20:42
@xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, bothappend
andhstack
are thin (5-10 lines) wrappers aroundconcatenate
.
– tel
Nov 21 '18 at 20:48
Here you can find more about the thema stackoverflow.com/questions/8486294/…
– oakca
Nov 21 '18 at 21:03
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419788%2fcreating-an-array-by-adding-to-shape-of-existing-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To fix your code, do this instead:
X_b = np.ones(X.shape + np.array((0,1)))
The catch here is that X.shape
returns a plain Python tuple
. By adding (0,1)
you were actually performing tuple concatenation, instead of pairwise addition like you intended.
Of course, you could also just stick an extra column on to your existing array with append
:
X_b = np.append(X, [[1]]*X.shape[0], axis=1)
X_b[:,:-1] = X
and then useX_b
as input... probably doing the same thing. your answer looks good. Just waiting the minutes
– oakca
Nov 21 '18 at 20:17
@oakca assumingX_b
was created withnp.ones
, then yep. The one "gotcha" is thatX_b
will be of dtypefloat64
, which is the default fornp.ones
. Thus thedtype
s ofX
andX_b
may not match (eg ifX.dtype==int
)
– tel
Nov 21 '18 at 20:41
Ornp.hstack((X, np.ones((4,1), dtype=np.int)))
, which I find easier to visualize.
– xnx
Nov 21 '18 at 20:42
@xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, bothappend
andhstack
are thin (5-10 lines) wrappers aroundconcatenate
.
– tel
Nov 21 '18 at 20:48
Here you can find more about the thema stackoverflow.com/questions/8486294/…
– oakca
Nov 21 '18 at 21:03
|
show 3 more comments
To fix your code, do this instead:
X_b = np.ones(X.shape + np.array((0,1)))
The catch here is that X.shape
returns a plain Python tuple
. By adding (0,1)
you were actually performing tuple concatenation, instead of pairwise addition like you intended.
Of course, you could also just stick an extra column on to your existing array with append
:
X_b = np.append(X, [[1]]*X.shape[0], axis=1)
X_b[:,:-1] = X
and then useX_b
as input... probably doing the same thing. your answer looks good. Just waiting the minutes
– oakca
Nov 21 '18 at 20:17
@oakca assumingX_b
was created withnp.ones
, then yep. The one "gotcha" is thatX_b
will be of dtypefloat64
, which is the default fornp.ones
. Thus thedtype
s ofX
andX_b
may not match (eg ifX.dtype==int
)
– tel
Nov 21 '18 at 20:41
Ornp.hstack((X, np.ones((4,1), dtype=np.int)))
, which I find easier to visualize.
– xnx
Nov 21 '18 at 20:42
@xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, bothappend
andhstack
are thin (5-10 lines) wrappers aroundconcatenate
.
– tel
Nov 21 '18 at 20:48
Here you can find more about the thema stackoverflow.com/questions/8486294/…
– oakca
Nov 21 '18 at 21:03
|
show 3 more comments
To fix your code, do this instead:
X_b = np.ones(X.shape + np.array((0,1)))
The catch here is that X.shape
returns a plain Python tuple
. By adding (0,1)
you were actually performing tuple concatenation, instead of pairwise addition like you intended.
Of course, you could also just stick an extra column on to your existing array with append
:
X_b = np.append(X, [[1]]*X.shape[0], axis=1)
To fix your code, do this instead:
X_b = np.ones(X.shape + np.array((0,1)))
The catch here is that X.shape
returns a plain Python tuple
. By adding (0,1)
you were actually performing tuple concatenation, instead of pairwise addition like you intended.
Of course, you could also just stick an extra column on to your existing array with append
:
X_b = np.append(X, [[1]]*X.shape[0], axis=1)
edited Nov 21 '18 at 21:55
answered Nov 21 '18 at 20:13
teltel
7,47621431
7,47621431
X_b[:,:-1] = X
and then useX_b
as input... probably doing the same thing. your answer looks good. Just waiting the minutes
– oakca
Nov 21 '18 at 20:17
@oakca assumingX_b
was created withnp.ones
, then yep. The one "gotcha" is thatX_b
will be of dtypefloat64
, which is the default fornp.ones
. Thus thedtype
s ofX
andX_b
may not match (eg ifX.dtype==int
)
– tel
Nov 21 '18 at 20:41
Ornp.hstack((X, np.ones((4,1), dtype=np.int)))
, which I find easier to visualize.
– xnx
Nov 21 '18 at 20:42
@xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, bothappend
andhstack
are thin (5-10 lines) wrappers aroundconcatenate
.
– tel
Nov 21 '18 at 20:48
Here you can find more about the thema stackoverflow.com/questions/8486294/…
– oakca
Nov 21 '18 at 21:03
|
show 3 more comments
X_b[:,:-1] = X
and then useX_b
as input... probably doing the same thing. your answer looks good. Just waiting the minutes
– oakca
Nov 21 '18 at 20:17
@oakca assumingX_b
was created withnp.ones
, then yep. The one "gotcha" is thatX_b
will be of dtypefloat64
, which is the default fornp.ones
. Thus thedtype
s ofX
andX_b
may not match (eg ifX.dtype==int
)
– tel
Nov 21 '18 at 20:41
Ornp.hstack((X, np.ones((4,1), dtype=np.int)))
, which I find easier to visualize.
– xnx
Nov 21 '18 at 20:42
@xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, bothappend
andhstack
are thin (5-10 lines) wrappers aroundconcatenate
.
– tel
Nov 21 '18 at 20:48
Here you can find more about the thema stackoverflow.com/questions/8486294/…
– oakca
Nov 21 '18 at 21:03
X_b[:,:-1] = X
and then use X_b
as input... probably doing the same thing. your answer looks good. Just waiting the minutes– oakca
Nov 21 '18 at 20:17
X_b[:,:-1] = X
and then use X_b
as input... probably doing the same thing. your answer looks good. Just waiting the minutes– oakca
Nov 21 '18 at 20:17
@oakca assuming
X_b
was created with np.ones
, then yep. The one "gotcha" is that X_b
will be of dtype float64
, which is the default for np.ones
. Thus the dtype
s of X
and X_b
may not match (eg if X.dtype==int
)– tel
Nov 21 '18 at 20:41
@oakca assuming
X_b
was created with np.ones
, then yep. The one "gotcha" is that X_b
will be of dtype float64
, which is the default for np.ones
. Thus the dtype
s of X
and X_b
may not match (eg if X.dtype==int
)– tel
Nov 21 '18 at 20:41
Or
np.hstack((X, np.ones((4,1), dtype=np.int)))
, which I find easier to visualize.– xnx
Nov 21 '18 at 20:42
Or
np.hstack((X, np.ones((4,1), dtype=np.int)))
, which I find easier to visualize.– xnx
Nov 21 '18 at 20:42
@xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, both
append
and hstack
are thin (5-10 lines) wrappers around concatenate
.– tel
Nov 21 '18 at 20:48
@xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, both
append
and hstack
are thin (5-10 lines) wrappers around concatenate
.– tel
Nov 21 '18 at 20:48
Here you can find more about the thema stackoverflow.com/questions/8486294/…
– oakca
Nov 21 '18 at 21:03
Here you can find more about the thema stackoverflow.com/questions/8486294/…
– oakca
Nov 21 '18 at 21:03
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419788%2fcreating-an-array-by-adding-to-shape-of-existing-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown