简体   繁体   中英

Python code for calculate the square of the difference between

For each data point, calculate the square of the difference between it and the mean. Calculate the following quantities

  • e) The sum of the squared difference (hint: use for loop)
  • f) The variance - the average squared difference
  • g) The standard deviation - square root (SQRT function) of variance
  • h) The variance using the VAR function
  • i) The Standard Deviation using the STD function.
  • j) Is there any difference between the variances and standard deviations?

The date which is mentioned is data from excel and I import them in software and I calculated the mean value. This is what I have til now

import pandas as pd
ExcelSheet1 = pd.read_csv("C:\Sanja\E1StatsDATAsheet1.csv")
ExcelSheet2 = pd.read_csv("C:\Sanja\E1StatsDATAsheet2.csv")
print(ExcelSheet1)
print(ExcelSheet2)
print("Count for Sheet1 is:",ExcelSheet1.shape)
print("Count for Sheet2 is:",ExcelSheet2.shape)
Sum_ExcelSheet1 = ExcelSheet1.sum()
Sum_ExcelSheet2 = ExcelSheet2.sum()
print("Sum for Sheet1 is:",Sum_ExcelSheet1)
print("Sum for Sheet2 is:", Sum_ExcelSheet2)

import numpy
Mean_ExcelSheet1 = numpy.mean(ExcelSheet1)
Mean_ExcelSheet2 = numpy.mean(ExcelSheet2)
print("Mean for Sheet1 is:", Mean_ExcelSheet1)
print("Mean for Sheet2 is:", Mean_ExcelSheet2)

It is very hard to give you "the code" as I don't know what you need specifically, I'm afraid you will have to learn how to, fortunately basic python is easy.

To get you started, the read_csv should have populated an array with all the values from the file. You need a for loop to go over each element, do some calculation and store all the results. Here's some example code (this assumes you want to get the sqr of the diff of consecutive values in 1 csv file, things get much more complex if you want to diff between the files):

csv = [1,1,2,3,5,8,13,21,34]
ds = []
prev = None
for i in csv:
    if prev == None:
        prev = i
    else:
        diff = i - prev
        sqr = diff * diff
        ds.append(sqr)
        prev = i

print (ds)

Good luck in your coding journey!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM