README.md in anomaly-0.0.3 vs README.md in anomaly-0.1.0
- old
+ new
@@ -14,59 +14,55 @@
```sh
bundle install
```
-For max performance (~ 2x faster), also install the NArray gem:
+For max performance (trains ~3x faster for large datasets), also install the NArray gem:
```ruby
gem "narray"
```
Anomaly will automatically detect it and use it.
## How to Use
-Say we have weather data for sunny days and we're trying to detect days that aren't sunny. The data looks like:
+Say we have weather data and we want to predict if it's sunny. In this example, sunny days are non-anomalies, and days with other types of weather (rain, snow, etc.) are anomalies. The data looks like:
```ruby
-# Each row is a different day.
-# [temperature (°F), humidity (%), pressure (in)]
+# [temperature(°F), humidity(%), pressure(in), sunny?(y=0, n=1)]
weather_data = [
- [85, 68, 10.4],
- [88, 62, 12.1],
- [86, 64, 13.6],
+ [85, 68, 10.4, 0],
+ [88, 62, 12.1, 0],
+ [86, 64, 13.6, 0],
+ [88, 90, 11.1, 1],
...
]
```
-Train the detector with **only non-anomalies** (sunny days in our case).
+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 ε.
+To train the detector and test for anomalies, run:
+
```ruby
ad = Anomaly::Detector.new(weather_data)
+
+# 85°F, 42% humidity, 12.3 in. pressure
+ad.anomaly?([85, 42, 12.3])
+# => true
```
-That's it! Let's test for anomalies.
+Anomaly automatically finds the best value for ε, which you can access with:
```ruby
-# 79°F, 66% humidity, 12.3 in. pressure
-test_sample = [79, 66, 12.3]
-ad.probability(test_sample)
-# => 7.537174740907633e-08
+ad.eps
```
-**Super-important:** You must select a threshold for anomalies (which we denote with ε - "epsilon")
+If you already know you want ε = 0.01, initialize the detector with:
-Probabilities less than ε are considered anomalies. If ε is higher, more things are considered anomalies.
-
-``` ruby
-ad.anomaly?(test_sample, 1e-10)
-# => false
-ad.anomaly?(test_sample, 1e-5)
-# => true
+```ruby
+ad = Anomaly::Detector.new(weather_data, {:eps => 0.01})
```
-
-The wiki has [sample code](https://github.com/ankane/anomaly/wiki/Home) to help you find the best ε for your application.
### Persistence
You can easily persist the detector to a file or database - it's very tiny.