Why the dtype changed differently after convert two lists with same type to numpy array?
up vote
0
down vote
favorite
When I convert two lists of numpy arrays to numpy arrays of numpy arrays, something confused happened.
The first list X_s changed to a numpy array with shape of (1980, 384, 448, 1), which is good for training, but the second list X_l chaned to a numpy arrays with shape of (2013,).
I check their dtype, and the first become float64 while the second become object of numpy array.
Why this happened?
print(len(X_s)) # 1980
print(len(X_l)) # 2013
print(X_s[0].dtype) # float64
print(X_l[0].dtype) # float64
print(X_s[0].shape) # (384, 448, 1)
print(X_l[0].shape) # (384, 448, 1)
for i in range(len(X_l)):
X_l[i] = np.array(X_l[i], dtype = np.float64)
for i in range(len(X_s)):
X_s[i] = np.array(X_s[i], dtype = np.float64)
X_s = np.array(X_s)
X_l = np.array(X_l)
print(type(X_s[0])) # <class 'numpy.ndarray'>
print(type(X_l[0])) # <class 'numpy.ndarray'>
print(X_s.dtype) # flaot64
print(X_l.dtype) # object
print(X_s.shape) # (1980, 384, 448, 1)
print(X_l.shape) # (2013,)
After added two for loops to make sure the elements are in uniform type, nothing changed.
python arrays list numpy types
add a comment |
up vote
0
down vote
favorite
When I convert two lists of numpy arrays to numpy arrays of numpy arrays, something confused happened.
The first list X_s changed to a numpy array with shape of (1980, 384, 448, 1), which is good for training, but the second list X_l chaned to a numpy arrays with shape of (2013,).
I check their dtype, and the first become float64 while the second become object of numpy array.
Why this happened?
print(len(X_s)) # 1980
print(len(X_l)) # 2013
print(X_s[0].dtype) # float64
print(X_l[0].dtype) # float64
print(X_s[0].shape) # (384, 448, 1)
print(X_l[0].shape) # (384, 448, 1)
for i in range(len(X_l)):
X_l[i] = np.array(X_l[i], dtype = np.float64)
for i in range(len(X_s)):
X_s[i] = np.array(X_s[i], dtype = np.float64)
X_s = np.array(X_s)
X_l = np.array(X_l)
print(type(X_s[0])) # <class 'numpy.ndarray'>
print(type(X_l[0])) # <class 'numpy.ndarray'>
print(X_s.dtype) # flaot64
print(X_l.dtype) # object
print(X_s.shape) # (1980, 384, 448, 1)
print(X_l.shape) # (2013,)
After added two for loops to make sure the elements are in uniform type, nothing changed.
python arrays list numpy types
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
When I convert two lists of numpy arrays to numpy arrays of numpy arrays, something confused happened.
The first list X_s changed to a numpy array with shape of (1980, 384, 448, 1), which is good for training, but the second list X_l chaned to a numpy arrays with shape of (2013,).
I check their dtype, and the first become float64 while the second become object of numpy array.
Why this happened?
print(len(X_s)) # 1980
print(len(X_l)) # 2013
print(X_s[0].dtype) # float64
print(X_l[0].dtype) # float64
print(X_s[0].shape) # (384, 448, 1)
print(X_l[0].shape) # (384, 448, 1)
for i in range(len(X_l)):
X_l[i] = np.array(X_l[i], dtype = np.float64)
for i in range(len(X_s)):
X_s[i] = np.array(X_s[i], dtype = np.float64)
X_s = np.array(X_s)
X_l = np.array(X_l)
print(type(X_s[0])) # <class 'numpy.ndarray'>
print(type(X_l[0])) # <class 'numpy.ndarray'>
print(X_s.dtype) # flaot64
print(X_l.dtype) # object
print(X_s.shape) # (1980, 384, 448, 1)
print(X_l.shape) # (2013,)
After added two for loops to make sure the elements are in uniform type, nothing changed.
python arrays list numpy types
When I convert two lists of numpy arrays to numpy arrays of numpy arrays, something confused happened.
The first list X_s changed to a numpy array with shape of (1980, 384, 448, 1), which is good for training, but the second list X_l chaned to a numpy arrays with shape of (2013,).
I check their dtype, and the first become float64 while the second become object of numpy array.
Why this happened?
print(len(X_s)) # 1980
print(len(X_l)) # 2013
print(X_s[0].dtype) # float64
print(X_l[0].dtype) # float64
print(X_s[0].shape) # (384, 448, 1)
print(X_l[0].shape) # (384, 448, 1)
for i in range(len(X_l)):
X_l[i] = np.array(X_l[i], dtype = np.float64)
for i in range(len(X_s)):
X_s[i] = np.array(X_s[i], dtype = np.float64)
X_s = np.array(X_s)
X_l = np.array(X_l)
print(type(X_s[0])) # <class 'numpy.ndarray'>
print(type(X_l[0])) # <class 'numpy.ndarray'>
print(X_s.dtype) # flaot64
print(X_l.dtype) # object
print(X_s.shape) # (1980, 384, 448, 1)
print(X_l.shape) # (2013,)
After added two for loops to make sure the elements are in uniform type, nothing changed.
python arrays list numpy types
python arrays list numpy types
edited Nov 10 at 10:09
asked Nov 10 at 9:37
Salmon
227
227
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
It looks very likely that the elements of the original X_l
list are not of uniform type. (You only show us the type of the first element but not the rest.)
When NumPy tries to convert that list to an array, it notices that and coerces everything to object
.
Demo:
In [10]: X_s = [np.array([1]), np.array([2])]
In [11]: X_l = [np.array([1]), 2]
In [12]: np.array(X_s)
Out[12]:
array([[1],
[2]])
In [13]: np.array(X_l)
Out[13]: array([array([1]), 2], dtype=object)
(This example is made up but consistent with your observations.)
I used a loop before the convert to make sure that the elements of the origin X_l list are in uniform type just now. But It didn't work.
– Salmon
Nov 10 at 10:01
After check the shape of every element in X_l, I found some element's shape is not (384, 448, 1).Thank you for your guidance.
– Salmon
Nov 10 at 10:16
add a comment |
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',
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%2f53237676%2fwhy-the-dtype-changed-differently-after-convert-two-lists-with-same-type-to-nump%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
up vote
1
down vote
It looks very likely that the elements of the original X_l
list are not of uniform type. (You only show us the type of the first element but not the rest.)
When NumPy tries to convert that list to an array, it notices that and coerces everything to object
.
Demo:
In [10]: X_s = [np.array([1]), np.array([2])]
In [11]: X_l = [np.array([1]), 2]
In [12]: np.array(X_s)
Out[12]:
array([[1],
[2]])
In [13]: np.array(X_l)
Out[13]: array([array([1]), 2], dtype=object)
(This example is made up but consistent with your observations.)
I used a loop before the convert to make sure that the elements of the origin X_l list are in uniform type just now. But It didn't work.
– Salmon
Nov 10 at 10:01
After check the shape of every element in X_l, I found some element's shape is not (384, 448, 1).Thank you for your guidance.
– Salmon
Nov 10 at 10:16
add a comment |
up vote
1
down vote
It looks very likely that the elements of the original X_l
list are not of uniform type. (You only show us the type of the first element but not the rest.)
When NumPy tries to convert that list to an array, it notices that and coerces everything to object
.
Demo:
In [10]: X_s = [np.array([1]), np.array([2])]
In [11]: X_l = [np.array([1]), 2]
In [12]: np.array(X_s)
Out[12]:
array([[1],
[2]])
In [13]: np.array(X_l)
Out[13]: array([array([1]), 2], dtype=object)
(This example is made up but consistent with your observations.)
I used a loop before the convert to make sure that the elements of the origin X_l list are in uniform type just now. But It didn't work.
– Salmon
Nov 10 at 10:01
After check the shape of every element in X_l, I found some element's shape is not (384, 448, 1).Thank you for your guidance.
– Salmon
Nov 10 at 10:16
add a comment |
up vote
1
down vote
up vote
1
down vote
It looks very likely that the elements of the original X_l
list are not of uniform type. (You only show us the type of the first element but not the rest.)
When NumPy tries to convert that list to an array, it notices that and coerces everything to object
.
Demo:
In [10]: X_s = [np.array([1]), np.array([2])]
In [11]: X_l = [np.array([1]), 2]
In [12]: np.array(X_s)
Out[12]:
array([[1],
[2]])
In [13]: np.array(X_l)
Out[13]: array([array([1]), 2], dtype=object)
(This example is made up but consistent with your observations.)
It looks very likely that the elements of the original X_l
list are not of uniform type. (You only show us the type of the first element but not the rest.)
When NumPy tries to convert that list to an array, it notices that and coerces everything to object
.
Demo:
In [10]: X_s = [np.array([1]), np.array([2])]
In [11]: X_l = [np.array([1]), 2]
In [12]: np.array(X_s)
Out[12]:
array([[1],
[2]])
In [13]: np.array(X_l)
Out[13]: array([array([1]), 2], dtype=object)
(This example is made up but consistent with your observations.)
answered Nov 10 at 9:43
NPE
346k60739870
346k60739870
I used a loop before the convert to make sure that the elements of the origin X_l list are in uniform type just now. But It didn't work.
– Salmon
Nov 10 at 10:01
After check the shape of every element in X_l, I found some element's shape is not (384, 448, 1).Thank you for your guidance.
– Salmon
Nov 10 at 10:16
add a comment |
I used a loop before the convert to make sure that the elements of the origin X_l list are in uniform type just now. But It didn't work.
– Salmon
Nov 10 at 10:01
After check the shape of every element in X_l, I found some element's shape is not (384, 448, 1).Thank you for your guidance.
– Salmon
Nov 10 at 10:16
I used a loop before the convert to make sure that the elements of the origin X_l list are in uniform type just now. But It didn't work.
– Salmon
Nov 10 at 10:01
I used a loop before the convert to make sure that the elements of the origin X_l list are in uniform type just now. But It didn't work.
– Salmon
Nov 10 at 10:01
After check the shape of every element in X_l, I found some element's shape is not (384, 448, 1).Thank you for your guidance.
– Salmon
Nov 10 at 10:16
After check the shape of every element in X_l, I found some element's shape is not (384, 448, 1).Thank you for your guidance.
– Salmon
Nov 10 at 10:16
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53237676%2fwhy-the-dtype-changed-differently-after-convert-two-lists-with-same-type-to-nump%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