0

New to python but with some experience with c# and trying to create a basic script for calculating the standard deviation of a set of randomly generated integers. The two histograms generated in the code below are different, though they should be the same to my knowledge as I never modified the variable 'incomes' between generating the two histograms.

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

npoints = 10000
incomes = np.random.normal(100.0, 50.0, npoints)

################
plt.hist(incomes, 50)
plt.show()
###############

incomestore = incomes

meanincomes = np.mean(incomes)
for i in range(incomes.size):
    incomestore[i] = (incomes[i] - meanincomes)**(2)
standardDeviation = np.sqrt(np.mean(incomestore))

print(standardDeviation)
###############
plt.hist(incomes, 50)
plt.show()
##############

The two histograms generated by the code are different when they should be the same (not a scaling error).

Tim
  • 2,510
  • 1
  • 22
  • 26
  • `incomestore = incomes` this line means they are the same object. Basically, objects are like physical things and variables (references) are like pieces of paper that have an address written on it. So you have two different pieces of paper, one is called `incomestore`, the other is called `incomes`, but both of them have the same location written on them. – h4z3 Aug 07 '19 at 09:03
  • If you want to make a copy, you can use a while slice: `incomestore = incomes[:]` Slice notation is `[start:end]` (or `[start:end:step]`). If you omit one value, it takes from the end - omitting first value means "take from first element", omitting the last means "take until the end". Result is always a copy, so `my_list[:]` is a shorthand for copying the list. – h4z3 Aug 07 '19 at 09:06

1 Answers1

2

incomestore and incomes refer to the same array. You have to make a copy

incomestore = incomes.copy()

But it is better, to use the vector abilities of numpy:

incomestore = (incomes - incomes.mean()) ** 2
standardDeviation = incomestore.mean() ** 0.5
Daniel
  • 42,087
  • 4
  • 55
  • 81