[Java]数据分析--数据可视化
时间序列
需求:将⼀组字符顺序添加到时间序列中
实现:定义时间序列类TimeSeries,包含静态类Entry表⽰序列类中的各项,以及add,get,iterator,entry⽅法TimeSeries.java
1import java.util.ArrayList;
2import java.util.Iterator;
3import java.util.Map;
4import java.util.TreeMap;
5import urrent.TimeUnit;
6
7public class TimeSeries<T> implements Iterable<TimeSeries.Entry> {
8private final Map<Long,T> map = new TreeMap();
9
10public void add(long time, T event) {
11 map.put(time, event);
12try {
13 TimeUnit.MICROSECONDS.sleep(1); // 0.000001 sec delay
14 } catch(InterruptedException e) {
15 println(e);
16 }
17 }
18
19public T get(long time) {
(time);
21 }
22
23 ArrayList getList() {
24 ArrayList<Entry> list = new ArrayList();
25for (Entry entry : this) {
26 list.add(entry);
27 }
28return list;
29 }
30
31public int size() {
32return map.size();
33 }
34
35 @Override
36public Iterator iterator() {
37return new Iterator() { // anonymous inner class
38private final Iterator it = map.keySet().iterator();
39
40 @Override
41public boolean hasNext() {
42return it.hasNext();
43 }
44
45 @Override
46public Entry<T> next() {
47long time = (();
48 T event = (time);
49return new Entry(time, event);
50 }
51 };
52 }
53
54public static class Entry<T> {
55private final Long time;
java开发可视化界面56private final T event;
57
58public Entry(long time, T event) {
59this.time = time;
60this.event = event;
61 }
62
63public long getTime() {
64return time;
65 }
66
67public T getEvent() {
68return event;
69 }
70
71 @Override
72public String toString() {
73return String.format("(%d, %s)", time, event);
74 }
75 }
76 }
View Code
TimeSeriesTester.java
1import java.util.ArrayList;
2
3public class TimeSeriesTester {
4final static String[] EVENTS = {"It", "was", "the", "best", "of", "times"};
5
6public static void main(String[] args) {
7 TimeSeries<String> series = new TimeSeries();
8 load(series);
9
10for (TimeSeries.Entry<String> entry : series) {
11long time = Time();
12 String event = Event();
13 System.out.printf("%16d: %s%n", time, event);
14 }
15
16 ArrayList list = List();
17 System.out.printf("(3) = %s%n", (3));
18 }
19
20static void load(TimeSeries<String> series) {
21for (String event : EVENTS) {
22 series.add(System.currentTimeMillis(), event);
23 }
24 }
25 }
View Code
移动平均
需求:实现⼀个序列,其值是原始序列连续⽚段的平均值
实现:定义移动平均类MovingAverage,继承TimeSeries,同时实现根据原始序列和⽚段长度⽣成新序列的构造⽅法,tmp每次计算保存⼀个⽚段,sum计算⽚段元素之和,最后把平均值添加到序列中
注意
如原始序列有8个元素,⽚段长度为3,则新序列长度为 8-8%3=6
对于a=b=c,先执⾏b=c,再执⾏a=b
tmp不是每次全部更新,替换⼀个元素即可,求和后i⾃增
MovingAverage.java
1import java.util.Iterator;
2
3public class MovingAverage extends TimeSeries<Double> {
4private final TimeSeries parent;
5private final int length;
6
7public MovingAverage(TimeSeries parent, int length) {
8this.parent = parent;
9this.length = length;
10if (length > parent.size()) {
11throw new IllegalArgumentException("That's too long.");
12 }
13
14double[] tmp = new double[length]; // temp array to compute averages
15double sum = 0;
16int i=0;
17 Iterator it = parent.iterator();
18for (int j = 0; j < length; j++) {
19 sum += tmp[i++] = nextValue(it);
20 }
21this.add(System.currentTimeMillis(), sum/length);
22
23while (it.hasNext()) {
24 sum -= tmp[i%length];
25 sum += tmp[i++%length] = nextValue(it);
26this.add(System.currentTimeMillis(), sum/length);
27 }
28 }
29
30/* Returns the double value in the Entry currently located by it.
31*/
32private static double nextValue(Iterator it) {
33 TimeSeries.Entry<Double> entry = (TimeSeries.();
Event();
35 }
36 }
View Code
MovingAverageTester.java
1public class MovingAverageTester {
2static final double[] DATA = {20, 25, 21, 26, 28, 27, 29, 31};
3
4public static void main(String[] args) {
5 TimeSeries<Double> series = new TimeSeries();
6for (double x : DATA) {
7 series.add(System.currentTimeMillis(), x);
8 }
9 System.out.List());
10
11 TimeSeries<Double> ma3 = new MovingAverage(series, 3);
12 System.out.List());
13
14 TimeSeries<Double> ma5 = new MovingAverage(series, 5);
15 System.out.List());
16 }
17 }
View Code
指数分布
需求:模拟输出客服接电话的时间,两次电话之间的时间为指数分布,已知平均8⼩时接到120个电话实现:平均等待时间为4分钟,密度参数为0.25,⽣成随机数后代⼊指数分布公式,即得到时间
1import java.util.Random;
2
3public class ArrivalTimesTester {
4static final Random random = new Random();
5static final double LAMBDA = 0.25;
6
7static double time() {
8double p = Double();
9return -Math.log(1 - p)/LAMBDA;
10 }
11
12public static void main(String[] args) {
13double sum=0;
14for (int i = 0; i < 8; i++) {
15 System.out.println(time());
16 sum += time();
17 }
18 System.out.println("\n");
19 System.out.println(sum/8);
20 }
21 }
View Code
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论