From a book that programmers can learn (Python): Statistical processing-deviation value

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".

Deviation value [How to find the deviation value on this reference site](http://www.suguru.jp/nyuushi/hensachi.html) was described, so I made it as a reference.

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

From a book that programmers can learn (Python): Statistical processing-deviation value
From a book that programmers can learn ... (Python): Pointer
From a book that programmers can learn (Python): Conditional search (maximum value)
From a book that programmers can learn ... (Python): About sorting
From a book that programmers can learn (Python): Decoding messages
From a book that programmers can learn (Python): Find the mode
From a book that programmers can learn ... (Python): Review of arrays
From a book that programmers can learn (Python): Class declaration (public / private, etc.)
From a book that programmers can learn ... Collect small problem parts
From a book that programmers can learn: Verification of rune checksum (fixed length)
From a book that programmers can learn ... Verification of rune checksum (variable length) Part 2
From a book that programmers can learn: Converting characters that represent numbers to integer types
From a book that makes the programmer's way of thinking interesting (Python)
8 services that even beginners can learn Python (from beginners to advanced users)
[Python] A program that finds a pair that can be divided by a specified value
"Python Kit" that calls a Python script from Swift
I made a Docker image that can call FBX SDK Python from Node.js
Programmer's way of thinking is from XX book (Python)
"A book that understands Flask from scratch" Reading memo
Programmer's way of thinking is from XX book (Python)
A memo that reads data from dashDB with Python & Spark
A Python program in "A book that gently teaches difficult programming"
python Condition extraction from a list that I often forget
A Python program that aggregates time usage from icalendar data
Extract the value closest to a value from a Python list element
[Python] Make a graph that can be moved around with Plotly
I made a package that can compare morphological analyzers with Python
Make a Kindle book that images mathematical formulas from TeX files
About psd-tools, a library that can process psd files in Python
[Python algorithm] A program that outputs Sudoku answers from a depth-first search
[python] I made a class that can write a file tree quickly
Call a Python function from p5.js.
Touch a Python object from Elixir
Extract lines that match the conditions from a text file with python
Try using APSW, a Python library that SQLite can get serious about
How to get a value from a parameter store in lambda (using python)
[Python] I made a utility that can access dict type like a path
I made a simple timer that can be started from the terminal
Build a Python virtual environment that anyone can understand September 2016 (pyenv + virutalenv)
I made a module PyNanaco that can charge nanaco credit with python