summaryrefslogtreecommitdiff
path: root/stats/gini_index.py
diff options
context:
space:
mode:
authorzhang <zch921005@126.com>2020-02-29 23:39:10 +0800
committerzhang <zch921005@126.com>2020-02-29 23:39:10 +0800
commitff2ef97b6da7030a8512276938ded20a86d04a83 (patch)
tree9aa9e5c837a74596d19504014f020d3fab541223 /stats/gini_index.py
parent1646efec6de1416b71abb605a4d0be03bcdbdc3e (diff)
gini系数计算
Diffstat (limited to 'stats/gini_index.py')
-rw-r--r--stats/gini_index.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/stats/gini_index.py b/stats/gini_index.py
new file mode 100644
index 0000000..8821429
--- /dev/null
+++ b/stats/gini_index.py
@@ -0,0 +1,26 @@
+import numpy as np
+
+
+def gini(x):
+ mean = np.mean(x)
+ mad = np.mean(np.abs(x - mean))
+ rmad = mad/mean
+ return rmad/2
+
+
+if __name__ == '__main__':
+
+ x = np.asarray([2, 2, 4, 4])
+ print(gini(x))
+ x = np.asarray([1, 1, 6, 4])
+ print(gini(x))
+
+ x = np.random.rand(500)
+ print(gini(x))
+ x = np.random.rand(500)+1
+ print(gini(x))
+ x = np.random.rand(500)+10
+ print(gini(x))
+ x = np.random.rand(500)+100
+ print(gini(x))
+