Normalise all elements in deep nested list Python












0















I have image data in the form of a deep nested list of ints:



len(train_data_imgs) = 3889       # number of images in set
len(train_data_imgs[0]) = 100 # height
len(train_data_imgs[0][0]) = 100 # width
len(train_data_imgs[0][0][0]) = 3 # these are ints - RGB pixel values


How can I iterate through these to normalise them between 0 and 1? Simply would require every number to be divided by 255.










share|improve this question























  • What's wrong with dividing every value by 255?

    – Yakov Dan
    Nov 26 '18 at 13:49











  • You have 116.670.000 values, just the iteration alone will take about 10 seconds. Are your images in a format that allows bulk operations, such as numpy types?

    – MisterMiyagi
    Nov 26 '18 at 13:52
















0















I have image data in the form of a deep nested list of ints:



len(train_data_imgs) = 3889       # number of images in set
len(train_data_imgs[0]) = 100 # height
len(train_data_imgs[0][0]) = 100 # width
len(train_data_imgs[0][0][0]) = 3 # these are ints - RGB pixel values


How can I iterate through these to normalise them between 0 and 1? Simply would require every number to be divided by 255.










share|improve this question























  • What's wrong with dividing every value by 255?

    – Yakov Dan
    Nov 26 '18 at 13:49











  • You have 116.670.000 values, just the iteration alone will take about 10 seconds. Are your images in a format that allows bulk operations, such as numpy types?

    – MisterMiyagi
    Nov 26 '18 at 13:52














0












0








0








I have image data in the form of a deep nested list of ints:



len(train_data_imgs) = 3889       # number of images in set
len(train_data_imgs[0]) = 100 # height
len(train_data_imgs[0][0]) = 100 # width
len(train_data_imgs[0][0][0]) = 3 # these are ints - RGB pixel values


How can I iterate through these to normalise them between 0 and 1? Simply would require every number to be divided by 255.










share|improve this question














I have image data in the form of a deep nested list of ints:



len(train_data_imgs) = 3889       # number of images in set
len(train_data_imgs[0]) = 100 # height
len(train_data_imgs[0][0]) = 100 # width
len(train_data_imgs[0][0][0]) = 3 # these are ints - RGB pixel values


How can I iterate through these to normalise them between 0 and 1? Simply would require every number to be divided by 255.







python python-3.x image






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 13:45









Seb SquireSeb Squire

62




62













  • What's wrong with dividing every value by 255?

    – Yakov Dan
    Nov 26 '18 at 13:49











  • You have 116.670.000 values, just the iteration alone will take about 10 seconds. Are your images in a format that allows bulk operations, such as numpy types?

    – MisterMiyagi
    Nov 26 '18 at 13:52



















  • What's wrong with dividing every value by 255?

    – Yakov Dan
    Nov 26 '18 at 13:49











  • You have 116.670.000 values, just the iteration alone will take about 10 seconds. Are your images in a format that allows bulk operations, such as numpy types?

    – MisterMiyagi
    Nov 26 '18 at 13:52

















What's wrong with dividing every value by 255?

– Yakov Dan
Nov 26 '18 at 13:49





What's wrong with dividing every value by 255?

– Yakov Dan
Nov 26 '18 at 13:49













You have 116.670.000 values, just the iteration alone will take about 10 seconds. Are your images in a format that allows bulk operations, such as numpy types?

– MisterMiyagi
Nov 26 '18 at 13:52





You have 116.670.000 values, just the iteration alone will take about 10 seconds. Are your images in a format that allows bulk operations, such as numpy types?

– MisterMiyagi
Nov 26 '18 at 13:52












1 Answer
1






active

oldest

votes


















1














Use NumPy package to do in a line:



# Assuming an image stored in a nested list | here NumPy array
lst = np.arange(27).reshape(3,3,3)
lst

array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])

lst = lst/255 # That's what you should look for
lst

array([[[0. , 0.00392157, 0.00784314],
[0.01176471, 0.01568627, 0.01960784],
[0.02352941, 0.02745098, 0.03137255]],

[[0.03529412, 0.03921569, 0.04313725],
[0.04705882, 0.05098039, 0.05490196],
[0.05882353, 0.0627451 , 0.06666667]],

[[0.07058824, 0.0745098 , 0.07843137],
[0.08235294, 0.08627451, 0.09019608],
[0.09411765, 0.09803922, 0.10196078]]])





share|improve this answer
























  • What's the advantage of using numpy in this case?

    – Yakov Dan
    Nov 26 '18 at 13:50






  • 1





    The same advantage which NumPy can have over normal python list ;) Actually you can go ahead with normal list too. But what's the problem in knowing something better, especially when it's images. :)

    – dataLeo
    Nov 26 '18 at 13:52






  • 2





    The question then is how to get the images into numpy format in the first place, and whether they must be extracted later on again.

    – MisterMiyagi
    Nov 26 '18 at 13:55













  • @MisterMiyagi True

    – dataLeo
    Nov 26 '18 at 13:56






  • 1





    There's nothing wrong with using a better tool! However, if you already have an image in memory stored as lists of lists, why would it be better to convert to a numpy array and then use numpy vs just iterating over the lists?

    – Yakov Dan
    Nov 26 '18 at 13:56











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482476%2fnormalise-all-elements-in-deep-nested-list-python%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









1














Use NumPy package to do in a line:



# Assuming an image stored in a nested list | here NumPy array
lst = np.arange(27).reshape(3,3,3)
lst

array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])

lst = lst/255 # That's what you should look for
lst

array([[[0. , 0.00392157, 0.00784314],
[0.01176471, 0.01568627, 0.01960784],
[0.02352941, 0.02745098, 0.03137255]],

[[0.03529412, 0.03921569, 0.04313725],
[0.04705882, 0.05098039, 0.05490196],
[0.05882353, 0.0627451 , 0.06666667]],

[[0.07058824, 0.0745098 , 0.07843137],
[0.08235294, 0.08627451, 0.09019608],
[0.09411765, 0.09803922, 0.10196078]]])





share|improve this answer
























  • What's the advantage of using numpy in this case?

    – Yakov Dan
    Nov 26 '18 at 13:50






  • 1





    The same advantage which NumPy can have over normal python list ;) Actually you can go ahead with normal list too. But what's the problem in knowing something better, especially when it's images. :)

    – dataLeo
    Nov 26 '18 at 13:52






  • 2





    The question then is how to get the images into numpy format in the first place, and whether they must be extracted later on again.

    – MisterMiyagi
    Nov 26 '18 at 13:55













  • @MisterMiyagi True

    – dataLeo
    Nov 26 '18 at 13:56






  • 1





    There's nothing wrong with using a better tool! However, if you already have an image in memory stored as lists of lists, why would it be better to convert to a numpy array and then use numpy vs just iterating over the lists?

    – Yakov Dan
    Nov 26 '18 at 13:56
















1














Use NumPy package to do in a line:



# Assuming an image stored in a nested list | here NumPy array
lst = np.arange(27).reshape(3,3,3)
lst

array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])

lst = lst/255 # That's what you should look for
lst

array([[[0. , 0.00392157, 0.00784314],
[0.01176471, 0.01568627, 0.01960784],
[0.02352941, 0.02745098, 0.03137255]],

[[0.03529412, 0.03921569, 0.04313725],
[0.04705882, 0.05098039, 0.05490196],
[0.05882353, 0.0627451 , 0.06666667]],

[[0.07058824, 0.0745098 , 0.07843137],
[0.08235294, 0.08627451, 0.09019608],
[0.09411765, 0.09803922, 0.10196078]]])





share|improve this answer
























  • What's the advantage of using numpy in this case?

    – Yakov Dan
    Nov 26 '18 at 13:50






  • 1





    The same advantage which NumPy can have over normal python list ;) Actually you can go ahead with normal list too. But what's the problem in knowing something better, especially when it's images. :)

    – dataLeo
    Nov 26 '18 at 13:52






  • 2





    The question then is how to get the images into numpy format in the first place, and whether they must be extracted later on again.

    – MisterMiyagi
    Nov 26 '18 at 13:55













  • @MisterMiyagi True

    – dataLeo
    Nov 26 '18 at 13:56






  • 1





    There's nothing wrong with using a better tool! However, if you already have an image in memory stored as lists of lists, why would it be better to convert to a numpy array and then use numpy vs just iterating over the lists?

    – Yakov Dan
    Nov 26 '18 at 13:56














1












1








1







Use NumPy package to do in a line:



# Assuming an image stored in a nested list | here NumPy array
lst = np.arange(27).reshape(3,3,3)
lst

array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])

lst = lst/255 # That's what you should look for
lst

array([[[0. , 0.00392157, 0.00784314],
[0.01176471, 0.01568627, 0.01960784],
[0.02352941, 0.02745098, 0.03137255]],

[[0.03529412, 0.03921569, 0.04313725],
[0.04705882, 0.05098039, 0.05490196],
[0.05882353, 0.0627451 , 0.06666667]],

[[0.07058824, 0.0745098 , 0.07843137],
[0.08235294, 0.08627451, 0.09019608],
[0.09411765, 0.09803922, 0.10196078]]])





share|improve this answer













Use NumPy package to do in a line:



# Assuming an image stored in a nested list | here NumPy array
lst = np.arange(27).reshape(3,3,3)
lst

array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])

lst = lst/255 # That's what you should look for
lst

array([[[0. , 0.00392157, 0.00784314],
[0.01176471, 0.01568627, 0.01960784],
[0.02352941, 0.02745098, 0.03137255]],

[[0.03529412, 0.03921569, 0.04313725],
[0.04705882, 0.05098039, 0.05490196],
[0.05882353, 0.0627451 , 0.06666667]],

[[0.07058824, 0.0745098 , 0.07843137],
[0.08235294, 0.08627451, 0.09019608],
[0.09411765, 0.09803922, 0.10196078]]])






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 13:49









dataLeodataLeo

6181419




6181419













  • What's the advantage of using numpy in this case?

    – Yakov Dan
    Nov 26 '18 at 13:50






  • 1





    The same advantage which NumPy can have over normal python list ;) Actually you can go ahead with normal list too. But what's the problem in knowing something better, especially when it's images. :)

    – dataLeo
    Nov 26 '18 at 13:52






  • 2





    The question then is how to get the images into numpy format in the first place, and whether they must be extracted later on again.

    – MisterMiyagi
    Nov 26 '18 at 13:55













  • @MisterMiyagi True

    – dataLeo
    Nov 26 '18 at 13:56






  • 1





    There's nothing wrong with using a better tool! However, if you already have an image in memory stored as lists of lists, why would it be better to convert to a numpy array and then use numpy vs just iterating over the lists?

    – Yakov Dan
    Nov 26 '18 at 13:56



















  • What's the advantage of using numpy in this case?

    – Yakov Dan
    Nov 26 '18 at 13:50






  • 1





    The same advantage which NumPy can have over normal python list ;) Actually you can go ahead with normal list too. But what's the problem in knowing something better, especially when it's images. :)

    – dataLeo
    Nov 26 '18 at 13:52






  • 2





    The question then is how to get the images into numpy format in the first place, and whether they must be extracted later on again.

    – MisterMiyagi
    Nov 26 '18 at 13:55













  • @MisterMiyagi True

    – dataLeo
    Nov 26 '18 at 13:56






  • 1





    There's nothing wrong with using a better tool! However, if you already have an image in memory stored as lists of lists, why would it be better to convert to a numpy array and then use numpy vs just iterating over the lists?

    – Yakov Dan
    Nov 26 '18 at 13:56

















What's the advantage of using numpy in this case?

– Yakov Dan
Nov 26 '18 at 13:50





What's the advantage of using numpy in this case?

– Yakov Dan
Nov 26 '18 at 13:50




1




1





The same advantage which NumPy can have over normal python list ;) Actually you can go ahead with normal list too. But what's the problem in knowing something better, especially when it's images. :)

– dataLeo
Nov 26 '18 at 13:52





The same advantage which NumPy can have over normal python list ;) Actually you can go ahead with normal list too. But what's the problem in knowing something better, especially when it's images. :)

– dataLeo
Nov 26 '18 at 13:52




2




2





The question then is how to get the images into numpy format in the first place, and whether they must be extracted later on again.

– MisterMiyagi
Nov 26 '18 at 13:55







The question then is how to get the images into numpy format in the first place, and whether they must be extracted later on again.

– MisterMiyagi
Nov 26 '18 at 13:55















@MisterMiyagi True

– dataLeo
Nov 26 '18 at 13:56





@MisterMiyagi True

– dataLeo
Nov 26 '18 at 13:56




1




1





There's nothing wrong with using a better tool! However, if you already have an image in memory stored as lists of lists, why would it be better to convert to a numpy array and then use numpy vs just iterating over the lists?

– Yakov Dan
Nov 26 '18 at 13:56





There's nothing wrong with using a better tool! However, if you already have an image in memory stored as lists of lists, why would it be better to convert to a numpy array and then use numpy vs just iterating over the lists?

– Yakov Dan
Nov 26 '18 at 13:56




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482476%2fnormalise-all-elements-in-deep-nested-list-python%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks