PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 7, 2022

[FIXED] How to calculate mean/variance/standard deviation per index of array?

 October 07, 2022     arrays, mean, numpy, python, statistics     No comments   

Issue

I have some data like [[0, 1, 2], [0.5, 1.5, 2.5], [0.3, 1.3, 2.3]].

I am using numpy and python and I wish to calculate the mean and standard deviation for my data, per index. So I wish to calculate the mean/std for (0, 0.5, 0.3) (e.g. index 0 of each subarray), (1, 1.5, 1.3) (e.g. index 1 of each subarray), and so on.

Any suggestions? (including how I can store the result and visualize it, maybe using graphing or matplotlib?)

Thank you so much, in advance. Any introduction to packages that might solve this problem would be really helpful, as well.


Solution

The various statistics functions all take an axis argument that will allow you to calculate the statistic over a column:

import numpy as np

a = np.array([[0, 1, 2], [0.5, 1.5, 2.5], [0.3, 1.3, 2.3]])

np.mean(a, axis=0)
# array([0.26666667, 1.26666667, 2.26666667])

np.std(a, axis=0)
# array([0.20548047, 0.20548047, 0.20548047])

np.var(a, axis=0)
# array([0.04222222, 0.04222222, 0.04222222])


Answered By - Mark
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing