How do I find the standard deviation from a text file using python?
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
add a comment |
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
You could usenumpy
module and make use of thenumpy.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
add a comment |
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
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
python text-files standard-deviation
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 usenumpy
module and make use of thenumpy.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
add a comment |
You could usenumpy
module and make use of thenumpy.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
add a comment |
2 Answers
2
active
oldest
votes
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")
add a comment |
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
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',
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%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
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")
add a comment |
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")
add a comment |
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")
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")
answered Nov 20 '18 at 9:46
Guilherme CostaGuilherme Costa
636
636
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 20 '18 at 12:29
answered Nov 20 '18 at 9:52
Mayank PorwalMayank Porwal
4,7272624
4,7272624
add a comment |
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.
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%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
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
You could use
numpy
module and make use of thenumpy.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