How do you calculate interquartile range (IQR) correctly using Python?
I am trying to understand the way to compute iqr (interquartile range).
according this, this and this, I tried 3 solutions to do this.
solution_1
a = numpy.array([1, 2, 3, 4, 5, 6, 7])
q1_a = numpy.percentile(a, 25)
q3_a = numpy.percentile(a, 75)
q3_a - q1_a
solution_2
from scipy.stats import iqr
iqr(a)
solution_3
q1_am = np.median(numpy.array([1, 2, 3, 4]))
q3_am = np.median(numpy.array([4, 5, 6, 7]))
q3_am - q1_am
3 of them give the same result 3 which is correct.
when I tried another set of numbers, things were going weird.
both solution_1 and 2 output 0.95 which is not correct.
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
q1_x = numpy.percentile(x, 25)
q3_x = numpy.percentile(x, 75)
q3_x - q1_x
solution_3 gives 1.2 which is correct
q1_xm = np.median(np.array([4.1, 6.2, 6.7,7.25]))
q3_xm = np.median(np.array([7.25,7.4, 7.9, 8.1]))
q3_xm - q1_xm
What am I missing with the solutions?
any clue would be appreciated.
python numpy scipy
add a comment |
I am trying to understand the way to compute iqr (interquartile range).
according this, this and this, I tried 3 solutions to do this.
solution_1
a = numpy.array([1, 2, 3, 4, 5, 6, 7])
q1_a = numpy.percentile(a, 25)
q3_a = numpy.percentile(a, 75)
q3_a - q1_a
solution_2
from scipy.stats import iqr
iqr(a)
solution_3
q1_am = np.median(numpy.array([1, 2, 3, 4]))
q3_am = np.median(numpy.array([4, 5, 6, 7]))
q3_am - q1_am
3 of them give the same result 3 which is correct.
when I tried another set of numbers, things were going weird.
both solution_1 and 2 output 0.95 which is not correct.
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
q1_x = numpy.percentile(x, 25)
q3_x = numpy.percentile(x, 75)
q3_x - q1_x
solution_3 gives 1.2 which is correct
q1_xm = np.median(np.array([4.1, 6.2, 6.7,7.25]))
q3_xm = np.median(np.array([7.25,7.4, 7.9, 8.1]))
q3_xm - q1_xm
What am I missing with the solutions?
any clue would be appreciated.
python numpy scipy
add a comment |
I am trying to understand the way to compute iqr (interquartile range).
according this, this and this, I tried 3 solutions to do this.
solution_1
a = numpy.array([1, 2, 3, 4, 5, 6, 7])
q1_a = numpy.percentile(a, 25)
q3_a = numpy.percentile(a, 75)
q3_a - q1_a
solution_2
from scipy.stats import iqr
iqr(a)
solution_3
q1_am = np.median(numpy.array([1, 2, 3, 4]))
q3_am = np.median(numpy.array([4, 5, 6, 7]))
q3_am - q1_am
3 of them give the same result 3 which is correct.
when I tried another set of numbers, things were going weird.
both solution_1 and 2 output 0.95 which is not correct.
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
q1_x = numpy.percentile(x, 25)
q3_x = numpy.percentile(x, 75)
q3_x - q1_x
solution_3 gives 1.2 which is correct
q1_xm = np.median(np.array([4.1, 6.2, 6.7,7.25]))
q3_xm = np.median(np.array([7.25,7.4, 7.9, 8.1]))
q3_xm - q1_xm
What am I missing with the solutions?
any clue would be appreciated.
python numpy scipy
I am trying to understand the way to compute iqr (interquartile range).
according this, this and this, I tried 3 solutions to do this.
solution_1
a = numpy.array([1, 2, 3, 4, 5, 6, 7])
q1_a = numpy.percentile(a, 25)
q3_a = numpy.percentile(a, 75)
q3_a - q1_a
solution_2
from scipy.stats import iqr
iqr(a)
solution_3
q1_am = np.median(numpy.array([1, 2, 3, 4]))
q3_am = np.median(numpy.array([4, 5, 6, 7]))
q3_am - q1_am
3 of them give the same result 3 which is correct.
when I tried another set of numbers, things were going weird.
both solution_1 and 2 output 0.95 which is not correct.
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
q1_x = numpy.percentile(x, 25)
q3_x = numpy.percentile(x, 75)
q3_x - q1_x
solution_3 gives 1.2 which is correct
q1_xm = np.median(np.array([4.1, 6.2, 6.7,7.25]))
q3_xm = np.median(np.array([7.25,7.4, 7.9, 8.1]))
q3_xm - q1_xm
What am I missing with the solutions?
any clue would be appreciated.
python numpy scipy
python numpy scipy
edited Nov 16 '18 at 12:50
tel
6,12811430
6,12811430
asked Nov 16 '18 at 11:54
czlsws
233
233
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You'll get your expected result with numpy.percentile
if you set interpolation=midpoint
:
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
q1_x = numpy.percentile(x, 25, interpolation='midpoint')
q3_x = numpy.percentile(x, 75, interpolation='midpoint')
print(q3_x - q1_x)
This outputs:
1.2000000000000002
Setting interpolation=midpoint
also makes scipy.stats.iqr
give the result you wanted:
from scipy.stats import iqr
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
print(iqr(x, rng=(25,75), interpolation='midpoint'))
which outputs:
1.2000000000000002
See the interpolation
parameter in the linked docs for more info on what the option actually does.
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%2f53337391%2fhow-do-you-calculate-interquartile-range-iqr-correctly-using-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
You'll get your expected result with numpy.percentile
if you set interpolation=midpoint
:
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
q1_x = numpy.percentile(x, 25, interpolation='midpoint')
q3_x = numpy.percentile(x, 75, interpolation='midpoint')
print(q3_x - q1_x)
This outputs:
1.2000000000000002
Setting interpolation=midpoint
also makes scipy.stats.iqr
give the result you wanted:
from scipy.stats import iqr
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
print(iqr(x, rng=(25,75), interpolation='midpoint'))
which outputs:
1.2000000000000002
See the interpolation
parameter in the linked docs for more info on what the option actually does.
add a comment |
You'll get your expected result with numpy.percentile
if you set interpolation=midpoint
:
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
q1_x = numpy.percentile(x, 25, interpolation='midpoint')
q3_x = numpy.percentile(x, 75, interpolation='midpoint')
print(q3_x - q1_x)
This outputs:
1.2000000000000002
Setting interpolation=midpoint
also makes scipy.stats.iqr
give the result you wanted:
from scipy.stats import iqr
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
print(iqr(x, rng=(25,75), interpolation='midpoint'))
which outputs:
1.2000000000000002
See the interpolation
parameter in the linked docs for more info on what the option actually does.
add a comment |
You'll get your expected result with numpy.percentile
if you set interpolation=midpoint
:
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
q1_x = numpy.percentile(x, 25, interpolation='midpoint')
q3_x = numpy.percentile(x, 75, interpolation='midpoint')
print(q3_x - q1_x)
This outputs:
1.2000000000000002
Setting interpolation=midpoint
also makes scipy.stats.iqr
give the result you wanted:
from scipy.stats import iqr
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
print(iqr(x, rng=(25,75), interpolation='midpoint'))
which outputs:
1.2000000000000002
See the interpolation
parameter in the linked docs for more info on what the option actually does.
You'll get your expected result with numpy.percentile
if you set interpolation=midpoint
:
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
q1_x = numpy.percentile(x, 25, interpolation='midpoint')
q3_x = numpy.percentile(x, 75, interpolation='midpoint')
print(q3_x - q1_x)
This outputs:
1.2000000000000002
Setting interpolation=midpoint
also makes scipy.stats.iqr
give the result you wanted:
from scipy.stats import iqr
x = numpy.array([4.1, 6.2, 6.7, 7.1, 7.4, 7.4, 7.9, 8.1])
print(iqr(x, rng=(25,75), interpolation='midpoint'))
which outputs:
1.2000000000000002
See the interpolation
parameter in the linked docs for more info on what the option actually does.
answered Nov 16 '18 at 12:44
tel
6,12811430
6,12811430
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.
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%2f53337391%2fhow-do-you-calculate-interquartile-range-iqr-correctly-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