How do I find the standard deviation from a text file using python?












-2















After finding the range, min, max and average from my text file of stored scores of my game, I would like to find the standard deviation from these scores but I am not sure how to go about it.



This is what I have so far:



file = open('stats.txt', 'a+')
file.write('n' + 'Score: ' + str(1))
file.close()

numbers =
with open('stats.txt') as fh:
count = 0
for line in fh:
count += 1
numbers.append(float(line.split()[1]))

file = open('maths.txt', 'w+')
file.write('Average Score: ' + str(sum(numbers)/len(numbers)) + "n")
file.write('Maximum Score: ' + str(max(numbers)) + "n")
file.write('Minimum Score: ' + str(min(numbers)) + "n")
maxn = max(numbers)
minn = min(numbers)
rangex = (maxn) - (minn)
file.write('Range of Scores: ' + str(rangex))
file.close()


What my text file looks like:



Score: 3
Score: 0
Score: 13
Score: 13
Score: 9
Score: 0
Score: 0
Score: 0
Score: 0
Score: 0
Score: 0
Score: 31
Score: 0
Score: 0
Score: 0
Score: 0
Score: -8
Score: 0
Score: 0


Thanks for helping










share|improve this question

























  • You could use numpy module and make use of the numpy.std() method.

    – toti08
    Nov 20 '18 at 9:46






  • 2





    It looka like you have everything you need. Try looking up the formula for standard deviation?

    – shadowtalker
    Nov 20 '18 at 9:46
















-2















After finding the range, min, max and average from my text file of stored scores of my game, I would like to find the standard deviation from these scores but I am not sure how to go about it.



This is what I have so far:



file = open('stats.txt', 'a+')
file.write('n' + 'Score: ' + str(1))
file.close()

numbers =
with open('stats.txt') as fh:
count = 0
for line in fh:
count += 1
numbers.append(float(line.split()[1]))

file = open('maths.txt', 'w+')
file.write('Average Score: ' + str(sum(numbers)/len(numbers)) + "n")
file.write('Maximum Score: ' + str(max(numbers)) + "n")
file.write('Minimum Score: ' + str(min(numbers)) + "n")
maxn = max(numbers)
minn = min(numbers)
rangex = (maxn) - (minn)
file.write('Range of Scores: ' + str(rangex))
file.close()


What my text file looks like:



Score: 3
Score: 0
Score: 13
Score: 13
Score: 9
Score: 0
Score: 0
Score: 0
Score: 0
Score: 0
Score: 0
Score: 31
Score: 0
Score: 0
Score: 0
Score: 0
Score: -8
Score: 0
Score: 0


Thanks for helping










share|improve this question

























  • You could use numpy module and make use of the numpy.std() method.

    – toti08
    Nov 20 '18 at 9:46






  • 2





    It looka like you have everything you need. Try looking up the formula for standard deviation?

    – shadowtalker
    Nov 20 '18 at 9:46














-2












-2








-2








After finding the range, min, max and average from my text file of stored scores of my game, I would like to find the standard deviation from these scores but I am not sure how to go about it.



This is what I have so far:



file = open('stats.txt', 'a+')
file.write('n' + 'Score: ' + str(1))
file.close()

numbers =
with open('stats.txt') as fh:
count = 0
for line in fh:
count += 1
numbers.append(float(line.split()[1]))

file = open('maths.txt', 'w+')
file.write('Average Score: ' + str(sum(numbers)/len(numbers)) + "n")
file.write('Maximum Score: ' + str(max(numbers)) + "n")
file.write('Minimum Score: ' + str(min(numbers)) + "n")
maxn = max(numbers)
minn = min(numbers)
rangex = (maxn) - (minn)
file.write('Range of Scores: ' + str(rangex))
file.close()


What my text file looks like:



Score: 3
Score: 0
Score: 13
Score: 13
Score: 9
Score: 0
Score: 0
Score: 0
Score: 0
Score: 0
Score: 0
Score: 31
Score: 0
Score: 0
Score: 0
Score: 0
Score: -8
Score: 0
Score: 0


Thanks for helping










share|improve this question
















After finding the range, min, max and average from my text file of stored scores of my game, I would like to find the standard deviation from these scores but I am not sure how to go about it.



This is what I have so far:



file = open('stats.txt', 'a+')
file.write('n' + 'Score: ' + str(1))
file.close()

numbers =
with open('stats.txt') as fh:
count = 0
for line in fh:
count += 1
numbers.append(float(line.split()[1]))

file = open('maths.txt', 'w+')
file.write('Average Score: ' + str(sum(numbers)/len(numbers)) + "n")
file.write('Maximum Score: ' + str(max(numbers)) + "n")
file.write('Minimum Score: ' + str(min(numbers)) + "n")
maxn = max(numbers)
minn = min(numbers)
rangex = (maxn) - (minn)
file.write('Range of Scores: ' + str(rangex))
file.close()


What my text file looks like:



Score: 3
Score: 0
Score: 13
Score: 13
Score: 9
Score: 0
Score: 0
Score: 0
Score: 0
Score: 0
Score: 0
Score: 31
Score: 0
Score: 0
Score: 0
Score: 0
Score: -8
Score: 0
Score: 0


Thanks for helping







python text-files standard-deviation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 9:42









Mayank Porwal

4,7272624




4,7272624










asked Nov 20 '18 at 9:41









Jay WJay W

11




11













  • You could use numpy module and make use of the numpy.std() method.

    – toti08
    Nov 20 '18 at 9:46






  • 2





    It looka like you have everything you need. Try looking up the formula for standard deviation?

    – shadowtalker
    Nov 20 '18 at 9:46



















  • You could use numpy module and make use of the numpy.std() method.

    – toti08
    Nov 20 '18 at 9:46






  • 2





    It looka like you have everything you need. Try looking up the formula for standard deviation?

    – shadowtalker
    Nov 20 '18 at 9:46

















You could use numpy module and make use of the numpy.std() method.

– toti08
Nov 20 '18 at 9:46





You could use numpy module and make use of the numpy.std() method.

– toti08
Nov 20 '18 at 9:46




2




2





It looka like you have everything you need. Try looking up the formula for standard deviation?

– shadowtalker
Nov 20 '18 at 9:46





It looka like you have everything you need. Try looking up the formula for standard deviation?

– shadowtalker
Nov 20 '18 at 9:46












2 Answers
2






active

oldest

votes


















0














You just need to use the standard deviation function from numpy.



Add to the beginning of your code:



import numpy as np


and then use:



file.write('Standard Deviation: ' + str(np.std(numbers)) + "n")





share|improve this answer































    0














    You can read the file and split on : to create a list:



    l = 

    In [400]: with open('stats.txt', 'r') as f:
    ...: for i in f:
    ...: l.append(int(i.split(':')[1].strip()))


    In [401]: l
    Out[401]: [3, 0, 13, 13, 9, 0, 0, 0, 0, 0, 0, 31, 0, 0, 0, 0, -8, 0, 0]

    In [403]: import statistics
    In [402]: statistics.stdev(l)
    Out[402]: 8.357158922932953


    OR you can use numpy also:



    In [404]: import numpy as np
    In [403]: np.std(l)
    Out[403]: 8.1342611825629





    share|improve this answer

























      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%2f53390118%2fhow-do-i-find-the-standard-deviation-from-a-text-file-using-python%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You just need to use the standard deviation function from numpy.



      Add to the beginning of your code:



      import numpy as np


      and then use:



      file.write('Standard Deviation: ' + str(np.std(numbers)) + "n")





      share|improve this answer




























        0














        You just need to use the standard deviation function from numpy.



        Add to the beginning of your code:



        import numpy as np


        and then use:



        file.write('Standard Deviation: ' + str(np.std(numbers)) + "n")





        share|improve this answer


























          0












          0








          0







          You just need to use the standard deviation function from numpy.



          Add to the beginning of your code:



          import numpy as np


          and then use:



          file.write('Standard Deviation: ' + str(np.std(numbers)) + "n")





          share|improve this answer













          You just need to use the standard deviation function from numpy.



          Add to the beginning of your code:



          import numpy as np


          and then use:



          file.write('Standard Deviation: ' + str(np.std(numbers)) + "n")






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 9:46









          Guilherme CostaGuilherme Costa

          636




          636

























              0














              You can read the file and split on : to create a list:



              l = 

              In [400]: with open('stats.txt', 'r') as f:
              ...: for i in f:
              ...: l.append(int(i.split(':')[1].strip()))


              In [401]: l
              Out[401]: [3, 0, 13, 13, 9, 0, 0, 0, 0, 0, 0, 31, 0, 0, 0, 0, -8, 0, 0]

              In [403]: import statistics
              In [402]: statistics.stdev(l)
              Out[402]: 8.357158922932953


              OR you can use numpy also:



              In [404]: import numpy as np
              In [403]: np.std(l)
              Out[403]: 8.1342611825629





              share|improve this answer






























                0














                You can read the file and split on : to create a list:



                l = 

                In [400]: with open('stats.txt', 'r') as f:
                ...: for i in f:
                ...: l.append(int(i.split(':')[1].strip()))


                In [401]: l
                Out[401]: [3, 0, 13, 13, 9, 0, 0, 0, 0, 0, 0, 31, 0, 0, 0, 0, -8, 0, 0]

                In [403]: import statistics
                In [402]: statistics.stdev(l)
                Out[402]: 8.357158922932953


                OR you can use numpy also:



                In [404]: import numpy as np
                In [403]: np.std(l)
                Out[403]: 8.1342611825629





                share|improve this answer




























                  0












                  0








                  0







                  You can read the file and split on : to create a list:



                  l = 

                  In [400]: with open('stats.txt', 'r') as f:
                  ...: for i in f:
                  ...: l.append(int(i.split(':')[1].strip()))


                  In [401]: l
                  Out[401]: [3, 0, 13, 13, 9, 0, 0, 0, 0, 0, 0, 31, 0, 0, 0, 0, -8, 0, 0]

                  In [403]: import statistics
                  In [402]: statistics.stdev(l)
                  Out[402]: 8.357158922932953


                  OR you can use numpy also:



                  In [404]: import numpy as np
                  In [403]: np.std(l)
                  Out[403]: 8.1342611825629





                  share|improve this answer















                  You can read the file and split on : to create a list:



                  l = 

                  In [400]: with open('stats.txt', 'r') as f:
                  ...: for i in f:
                  ...: l.append(int(i.split(':')[1].strip()))


                  In [401]: l
                  Out[401]: [3, 0, 13, 13, 9, 0, 0, 0, 0, 0, 0, 31, 0, 0, 0, 0, -8, 0, 0]

                  In [403]: import statistics
                  In [402]: statistics.stdev(l)
                  Out[402]: 8.357158922932953


                  OR you can use numpy also:



                  In [404]: import numpy as np
                  In [403]: np.std(l)
                  Out[403]: 8.1342611825629






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 '18 at 12:29

























                  answered Nov 20 '18 at 9:52









                  Mayank PorwalMayank Porwal

                  4,7272624




                  4,7272624






























                      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%2f53390118%2fhow-do-i-find-the-standard-deviation-from-a-text-file-using-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

                      Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                      Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                      A Topological Invariant for $pi_3(U(n))$