-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
164 lines (157 loc) · 4.01 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>朴素贝叶斯</title>
</head>
<body>
<script>
var StatisticalDesc = function(arr , num){
var newArr = new Array;
for(var i in arr){
newArr[i] = arr[i]
}
var arrLen = newArr.length;
var total = 0,
mean = 0, //均值
variance = 0, //方差
sd = 0, // 标准差
mode = 0, //众数
median = 0; //中位数
// 计算均值
for(var i in newArr ){
total += newArr[i]
}
mean = total / arrLen;
/*
计算方差
注意估计方差的方法
http://baike.baidu.com/item/%E6%96%B9%E5%B7%AE
*/
for(var i in newArr){
variance += (newArr[i]-mean)*(newArr[i]-mean)
}
/*
关于除以n还是除以n-1
如是总体(即估算总体方差),根号内除以n
如是抽样(即估算样本方差),根号内除以(n-1)
*/
variance = variance.toFixed(6) / (arrLen-1)
// 计算标准差
sd = Math.sqrt(variance)
// 计算中位数
newArr.sort(function(a,b){
return a-b
})
if(arrLen % 2 == 0){
median = (newArr[arrLen/2] + newArr[arrLen/2-1]) / 2;
}else{
median = newArr[Math.floor(arrLen/2)]
}
// 计算众数
var maxTimes = 0;
for(var i in newArr){
var temp = this.calcRepeat(newArr,newArr[i]);
if(maxTimes < temp){
maxTimes = temp;
mode = newArr[i];
}
}
if(maxTimes == 1){
mode = 'none'
}
// 保留小数位
if(num) {
sd = sd.toFixed(num)
mean = mean.toFixed(num)
variance = variance.toFixed(num)
};
return {mean:mean,variance : variance , sd:sd , median:median, mode:mode};
}
StatisticalDesc.prototype.calcRepeat = function(arr , value){
var times = 0;
for(var i in arr ){
if(arr[i] == value){
times ++
}
}
return times;
}
var Bayes = function(data,obj){
this.data = data;
this.maleArr = [];
this.femaleArr = [];
this.obj = obj;
this.type_1 = this.data[0].type;
this.type_2 = '';
}
Bayes.prototype.init = function(){
this.class();
var p_male = this.calcP(this.maleArr)
var p_female = this.calcP(this.femaleArr)
if(p_male > p_female){
console.log('猜测为'+this.type_1,parseInt(p_male/p_female))
return '猜测为'+this.type_1+' 概率倍数:' + parseInt(p_male/p_female)
}else{
console.log('猜测为'+this.type_2,parseInt(p_female/p_male))
return '猜测为'+ this.type_2 +' 概率倍数:' + parseInt(p_female/p_male)
}
}
// 把数据先分类
Bayes.prototype.class = function(){
for(var i in this.data){
if(this.data[i].type == this.type_1){
this.maleArr.push( this.data[i] )
}else{
this.femaleArr.push( this.data[i] )
this.type_2 = this.data[i].type
}
}
}
// 计算每部分的概率(正态分布)
Bayes.prototype.norDis = function( x , mean , variance){
var deno = Math.sqrt(2 * Math.PI * variance );
var exp = -(x-mean)*(x-mean)/(2* variance )
return Math.pow(Math.E , exp)/deno
}
// 计算总的概率
Bayes.prototype.calcP = function(arr,obj){
var allArr = [];
for(var i in arr[0]){
if( i != 'type'){
allArr[i] = [];
}
}
for(var i in arr){
for(k in arr[0]){
if( k != 'type'){
allArr[k].push(arr[i][k])
}
}
}
var p_temp = 1;
for(var i in allArr){
var temp_statDesc = new StatisticalDesc(allArr[i] , 3);
p_temp *= this.norDis(this.obj[i],temp_statDesc.mean,temp_statDesc.variance)
}
var p_type = arr.length / this.data.length
var p_guess = p_temp * p_type
return p_guess;
}
window.onload = function(){
var data = [
{type:'男',height:6 , weight:180 , foot: 12 },
{type:'男',height:5.92 , weight:190 , foot: 11 },
{type:'男',height:5.58 , weight:170 , foot: 12 },
{type:'男',height:5.92 , weight:165 , foot: 10 },
{type:'女',height:5 , weight:100 , foot: 6 },
{type:'女',height:5.5 , weight:150 , foot: 8 },
{type:'女',height:5.42 , weight:130 , foot: 7 },
{type:'女',height:5.75 , weight:150 , foot: 9},
]
var bayes = new Bayes(data,{height:6,weight:130,foot:8})
document.write(bayes.init())
}
</script>
</body>
</html>