ARTS-第22周

ARTS (第22周)

开篇词

复杂的和简单的,各有各的好。

本周:

朴素贝叶斯算法

Algorithm 算法

朴素贝叶斯算法(下雨和上课的关系)

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
package algorithm.senior.bayes;

import algorithm.senior.bitmap.BitMapTest.BitMap;
import algorithm.util.RandomUtil;

//朴素贝叶斯demo
//下雨天和去不去上课
//用户和歌曲
public class BayesTest {

public static void main(String[] args) {
judgeRainClass(10);
ClassLoader c = null;

}

// 雨天上课预处理(随机)
public static BitMap[] preRainClass(int d) {
// int d = 10;// 天数
BitMap r = new BitMap(d);// 雨
BitMap c = new BitMap(d);// 上课
// 模拟函数
for (int i = 0; i < d; i++) {
int n = RandomUtil.randomNum(2);
if (n == 1) {
r.setTrue(i);
}
n = RandomUtil.randomNum(2);
if (n == 1) {
c.setTrue(i);
}
}
return new BitMap[] { r, c };
}

// 判断
public static void judgeRainClass(int d) {
BitMap[] p = preRainClass(d);

System.out.print("下雨:");
for (int i = 0; i < d; i++) {
String s = p[0].get(i) ? "1" : "0";
System.out.print(s + ",");
}
System.out.print("\n上课:");
for (int i = 0; i < d; i++) {
String s = p[1].get(i) ? "1" : "0";
System.out.print(s + ",");
}
System.out.println();
// 2个分类
String[] strType = { "下雨上课", "下雨不上课", "不下雨上课", "不下雨不上课" };
// 0下雨上课,1下雨不上课,2不下雨上课,3不下雨不上课
double[] t = new double[4];
// 总数 0下雨总数 1上课总数
int[] total = new int[2];
String[] strTotal = { "下雨总数", "上课数" };
// 计算
for (int i = 0; i < d; i++) {
boolean r = p[0].get(i);// 下雨了
boolean c = p[1].get(i);// 上课了
if (r) {
total[0]++;// 下雨数
if (c) {
total[1]++;// 上课
t[0]++;
} else {
t[1]++;
}
} else {
if (c) {
total[1]++;// 上课
t[2]++;
} else {
t[3]++;
}
}

}
System.out.println("总数:" + d);
for (int i = 0; i < strTotal.length; i++) {
System.out.println(strTotal[i] + ":" + total[i]);
}
System.out.println("=============");
for (int i = 0; i < strType.length; i++) {
System.out.print(strType[i] + ":" + (int) t[i]);
System.out.println("," + strType[i] + "占比:" + (t[i] / d));
}
System.out.println("=============");
System.out.println("下雨天的时候");
for (int i = 0; i < 2; i++) {
System.out.println(strType[i] + "占比:" + (t[i] / total[0]));
}
// 下雨天上课的概率
// 根据贝叶斯定理:
// P(A|B) = P(B|A) * P(A) / P(B)
// P(上课|下雨)= P(下雨|上课) * P(上课)/ P(下雨)
// =(下雨上课总数/上课总数) * (上课总数/总数)/ (下雨总数/总数)
double r = (t[0] / total[1]) * (total[1] / d) / (total[0] / d);
System.out.println("->"+r);
}
}

朴素贝叶斯算法(较动态的方式)

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
package algorithm.senior.bayes;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

import algorithm.util.RandomUtil;

public class MoodBayesTest {

public static void main(String[] args) {

test();
}

private static void test() {
String[] c = { "职业", "行为", "心情" };
String[] t1 = { "学生", "工人", "老师" };
String[] t2 = { "唱歌", "哭泣" };
String[] t3 = { "开心", "难过" };
String[][] v = { t1, t2, t3 };
Feature[] random = random(15, c, v);
for (Feature f : random) {
System.out.println(f.f);
}
String[] c1 = { "行为", "心情" };
String[] ck = { "唱歌", "开心" };
bayers(random, "职业", "学生", c1, ck);
}

// 随机
public static Feature[] random(int len, String[] c, String[][] v) {
Feature[] features = new Feature[len];
for (int j = 0; j < features.length; j++) {
features[j] = new Feature();
Map<String, String> m = features[j].f;
for (int i = 0; i < c.length; i++) {
String[] vs = v[i];
int idx = RandomUtil.randomNum(vs.length);
m.put(c[i], vs[idx]);
}

}
return features;
}

/**
* 给特征 求目标在一定条件(独立事件)情况下的几率
*
* @param features
* 特征
* @param t
* 目标条件
*
* @param tk
* 目标key
* @param ck
* 条件key
* @param c
* 目标条件
*/
@SuppressWarnings("null")
public static void bayers(Feature[] features, String tk, String t, String[] c, String[] ck) {
// P(A|B) = P(B|A) * P(A) / P(B)
// 互相独立事件概率为 P(A*B) = P(A)*P(B)
// 所以求出每个条件的概率 相乘即可

// 目标的总体数量
int tCount = 0;
// 样本总数
int max = features.length;
// 目标情况下的条件数量
int[] cCount = new int[ck.length];
// 总数的条件数量
int[] cCountInMax = new int[ck.length];
// 各个独立事件的概率
double[] r = new double[ck.length];
for (Feature feature : features) {
Map<String, String> map = feature.f;

for (int i = 0; i < ck.length; i++) {
String s = c[i];// 特征
if (ck[i].equals(map.get(s))) {
// 如果样本是 行为 职业 心情
// 如果是在唱歌和学生的情况下求心情为 开心的概率
// 那么这里就是
// 唱歌的数量
// 学生的数量
cCountInMax[i]++;
}
}
if (t.equals(map.get(tk))) {
tCount++;// 目标的数量
for (int i = 0; i < ck.length; i++) {
String s = c[i];// 特征
if (ck[i].equals(map.get(s))) {
// 如果样本是 行为 职业 心情
// 如果是在唱歌和学生的情况下求心情为 开心的概率
// 那么这里就是
// 开心情况下是唱歌的数量
// 开心情况下是学生的数量
cCount[i]++;
}
}
}
}




for (int i = 0; i < r.length; i++) {
// P(A|B) = P(B|A) * P(A) / P(B)
// A是目标 B是条件
double pba = ((double) cCount[i] / (double) tCount);
double pa = ((double) tCount / (double) max);
double pb = ((double) cCountInMax[i] / (double) max);
r[i] = pba * pa / pb;
//
r[i] = (cCount[i] / (double) tCount) * (tCount / (double) max) / (cCountInMax[i] / (double) max);
}

System.out.println("样本总数为" + max + "," + tk + "为" + t + "的数量为:" + tCount);
for (int i = 0; i < cCount.length; i++) {
System.out.print(tk + "为" + t + "," + c[i] + "为" + ck[i] + "的数量为:" + cCount[i]);
System.out.println(",概率为:" + r[i]);
}
for (int i = 0; i < r.length; i++) {
}
System.out.println(Arrays.toString(r));
double result = r[0];
for (int i = 1; i < r.length; i++) {
result *= r[i];
}
System.out.println("->" + result);

}



public static class Feature {
// 特征
Map<String, String> f = new HashMap<>();
}
}

Review 英文文章

https://spring.io/projects/spring-data

spring data的各个介绍

Tip 技巧

极客时间的专栏《数据结构与算法之美》

本周学习了该专栏中的46-47篇

朴素贝叶斯:利用朴素贝叶斯算法统计概率。(计算公式是一种很朴素的方式)。

向量空间:计算在一个空间内,各个向量之间的距离,来确定互相之间的关联性。

Share 分享

https://blog.csdn.net/ityouknow/article/details/99269228

华为鸿蒙OS

https://blog.csdn.net/ityouknow/article/details/98554737

谷歌的历程