anomaly 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 301e930624aaaf4c39f66458571f05b9e09cd06015c7d2682444a93d0424f2a7
4
- data.tar.gz: 38841ca328edf7405bf0ae7b631538485ca7e226416d03b6977591897847bb3c
3
+ metadata.gz: 25ddca87f47c228cbbd18c12951a42d739673989bc9d7ec462d22b6b77594e58
4
+ data.tar.gz: cf70dd6cb3b84a78875c1d0aafead1255f03cf222f00c3fa001b2cdffd5008c9
5
5
  SHA512:
6
- metadata.gz: 26ccb948b275b2a2787e1368abe909e7ef9f78793743cf14065b6be830861c1def870a49d862ba466995fbdea4a6a012479cc51fcc4c2f9ea99ff341ad4ecd87
7
- data.tar.gz: a4bc731756ce68306d681433cc251af40e06cf1bd1ac21edd21ebfd7b8ef45ab05cffe3e557b2b731354bb377ade915f6e0e0f8b9b4fd945217bac3a278240c0
6
+ metadata.gz: b3c2c7457ce5ad155ef78e13450777ebb44da4dd208ba85d232b67bccdd1c12563745d5764df36e9b4a78e0a28366da9061b4cd77c334d733e18c80c5d0a3ac7
7
+ data.tar.gz: 7b5c91f014e7861e52a7b5a28d7e9189f1c67b63e064c688e59d114bb0a6806aefc4b79d1494c72182b917eb337edb361d6573eb5926dc275fcccac825b2e86a
@@ -1,3 +1,7 @@
1
+ # 0.2.1 (2020-04-16)
2
+
3
+ - Added support for multiple predictions
4
+
1
5
  # 0.2.0
2
6
 
3
7
  - Switched to Ruby `sum` for performance
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2011-2019 Andrew Kane
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -29,11 +29,15 @@ weather_data = [
29
29
 
30
30
  The last column **must** be 0 for non-anomalies, 1 for anomalies. Non-anomalies are used to train the detector, and both anomalies and non-anomalies are used to find the best value of ε.
31
31
 
32
- To train the detector and test for anomalies, run:
32
+ Train the detector
33
33
 
34
34
  ```ruby
35
35
  detector = Anomaly::Detector.new(weather_data)
36
+ ```
37
+
38
+ Test for anomalies
36
39
 
40
+ ```ruby
37
41
  # 85°F, 42% humidity, 12.3 in. pressure
38
42
  detector.anomaly?([85, 42, 12.3])
39
43
  ```
@@ -55,20 +59,20 @@ detector = Anomaly::Detector.new(weather_data, eps: 0.01)
55
59
  You can easily persist the detector to a file or database - it’s very tiny.
56
60
 
57
61
  ```ruby
58
- dump = Marshal.dump(detector)
59
- File.binwrite("detector.dump", dump)
62
+ bin = Marshal.dump(detector)
63
+ File.binwrite("detector.bin", bin)
60
64
  ```
61
65
 
62
66
  Then read it later:
63
67
 
64
68
  ```ruby
65
- dump = File.binread("detector.dump")
66
- detector = Marshal.load(dump)
69
+ bin = File.binread("detector.bin")
70
+ detector = Marshal.load(bin)
67
71
  ```
68
72
 
69
73
  ## Credits
70
74
 
71
- A special thanks to [Andrew Ng](http://www.ml-class.org).
75
+ A special thanks to [Andrew Ng](https://www.coursera.org/learn/machine-learning).
72
76
 
73
77
  ## History
74
78
 
@@ -82,3 +86,12 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
82
86
  - Fix bugs and [submit pull requests](https://github.com/ankane/anomaly/pulls)
83
87
  - Write, clarify, or fix documentation
84
88
  - Suggest or add new features
89
+
90
+ To get started with development:
91
+
92
+ ```sh
93
+ git clone https://github.com/ankane/anomaly.git
94
+ cd anomaly
95
+ bundle install
96
+ bundle exec rake spec
97
+ ```
@@ -58,7 +58,7 @@ module Anomaly
58
58
  @mean = cols.map { |c| alt_mean(c) }
59
59
  @std = cols.each_with_index.map { |c, i| alt_std(c, @mean[i]) }
60
60
  end
61
- @std.map! { |std| (std == 0 || std.nan?) ? Float::MIN : std }
61
+ @std.map! { |std| (std == 0 || std.nan?) ? 1e-10 : std }
62
62
 
63
63
  if @eps == 0
64
64
  # Find the best eps.
@@ -74,17 +74,36 @@ module Anomaly
74
74
 
75
75
  # Limit the probability of features to [0,1]
76
76
  # to keep probabilities at same scale.
77
+ # Use log to prevent underflow
77
78
  def probability(x)
78
79
  raise "Train me first" unless trained?
79
- raise ArgumentError, "First argument must have #{@n} elements" if x.size != @n
80
- @n.times.map do |i|
81
- p = normal_pdf(x[i], @mean[i], @std[i])
82
- (p.nan? || p > 1) ? 1 : p
83
- end.reduce(1, :*)
80
+
81
+ singular = !x.first.is_a?(Array)
82
+ x = [x] if singular
83
+
84
+ y =
85
+ x.map do |xi|
86
+ prob = 0
87
+ @n.times.map do |i|
88
+ pi = normal_pdf(xi[i], @mean[i], @std[i])
89
+ prob += Math.log(pi > 1 ? 1 : pi)
90
+ end
91
+ Math.exp(prob)
92
+ end
93
+
94
+ singular ? y.first : y
84
95
  end
85
96
 
86
97
  def anomaly?(x, eps = @eps)
87
- probability(x) < eps
98
+ y = probability(x)
99
+
100
+ if y.is_a?(Array)
101
+ y.map do |yi|
102
+ yi < eps
103
+ end
104
+ else
105
+ y < eps
106
+ end
88
107
  end
89
108
 
90
109
  protected
@@ -1,3 +1,3 @@
1
1
  module Anomaly
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anomaly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-28 00:00:00.000000000 Z
11
+ date: 2020-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -87,6 +87,7 @@ extensions: []
87
87
  extra_rdoc_files: []
88
88
  files:
89
89
  - CHANGELOG.md
90
+ - LICENSE.txt
90
91
  - README.md
91
92
  - lib/anomaly.rb
92
93
  - lib/anomaly/detector.rb
@@ -110,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
111
  - !ruby/object:Gem::Version
111
112
  version: '0'
112
113
  requirements: []
113
- rubygems_version: 3.0.3
114
+ rubygems_version: 3.1.2
114
115
  signing_key:
115
116
  specification_version: 4
116
117
  summary: Easy-to-use anomaly detection