Continued from Last time In this book, it is introduced in C ++, but I wrote it as a python. (But I'm new to programming, if it helps someone.)
This time, I introduced a simple average value because it is statistical processing. The content was pretty easy, so I thought it would be a little more, and I would like to write a python that asks for the "deviation value".
test36.py
#!/usr/bin/env python
#coding:utf-8
Elem = [87,76,100,97,64,83,88,92,74,95]
###sum = 0
###for i in Elem:
### sum += i
###Find the difference from the average score
###ave = sum / len(Elem)
###print('Average score',ave)
###value = []
###for b in Elem:
### value.append(b - ave)
###values = []
###for c in value:
### values.append(c**2)
#Seeking dispersion
###sums = 0
###for d in values:
### sums += d
###Average value
M = sum(Elem) / len(Elem)
print('Average value',M)
###Distributed
V = sum((x - M)**2 for x in Elem) / len(Elem)
print('Distributed',V)
#Find the square root of sums
#total = int(sums) #Truncate the decimal point
#totals = str(total) #Integer value to string
#Frustrated trying to find the square root yourself
### len(totals) #Check the number of digits
### a = totals[:2] #>> 11
### b = totals[2:] #>> 74
### a = int(a)
### b = int(b)
import math #Use the library
totals = math.sqrt(V) #The square root found is the standard deviation
print('standard deviation',totals)
#Difference from the average score(value * 10)Multiply by 10 to standard deviation(totals)Divide by
goukei = []
###for g in value:
### goukei.append((g * 10)/totals)
#Deviation value= (Each score-Average value) /standard deviation* 10 + 50
for g in Elem:
goukei.append(((g - M)/totals)*10+50)
print('Deviation value',goukei)
kotae = [((g - M)/totals)*10+50 for g in Elem]
print('List comprehension',kotae)
・ ・ ・(Terminal execution)
>>> import test36
Mean 85.6
Variance 117.44000000000001
Standard deviation 10.836973747315254
Deviation value[51.291873573419736, 41.14143835369326, 63.287842469460124, 60.519541954989265, 30.068236295809825, 47.60080622079193, 52.214640411576696, 55.9057077642045, 39.295677379355, 58.67400827867536]
List comprehension[51.291873573419736, 41.14143835369326, 63.287842469460124, 60.519541954989265, 30.068236295809825, 47.60080622079193, 52.214640411576696, 55.90570776420455904677379355, 58.67400827867536]
>>>
Thanks to a lot of comments I was able to fix the code quite short. Thank you for pointing this out!
Recommended Posts