# Path: ramp/metrics.py
# class PositiveRate(ThresholdMetric):
#     """
#     Positive rate: (True positives + False positives) / Total count
#     """
# 
#     def score_with_threshold(self, result, threshold):
#         return ((self.tp(result, threshold) + self.fp(result, threshold)) /
#                                  result.y_test.count())
# 
# class Recall(ThresholdMetric):
#     """
#     Recall: True positives / (True positives + False negatives)
#     """
# 
#     def score_with_threshold(self, result, threshold):
#         return self.tp(result, threshold) / result.y_test.sum()


import urllib2
import pandas as pd
import sklearn
import ramp
import logging
from sklearn import decomposition
from ramp.features import *
from ramp.metrics import PositiveRate, Recall



logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# fetch and clean iris data from UCI
data = pd.read_csv(urllib2.urlopen(
    "http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"))
data = data.drop([149]) # bad line
columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']
data.columns = columns

# all features
features = [FillMissing(f, 0) for f in columns[:-1]]

# features, log transformed features, and interaction terms
expanded_features = (
    features +
    [Log(F(f) + 1) for f in features] +
    [
        F('sepal_width') ** 2,
        combo.Interactions(features),
    ]
)

reporters = [
    ramp.reporters.MetricReporter.factory(Recall(.4)),

# Path: ramp/metrics.py
# class PositiveRate(ThresholdMetric):
#     """
#     Positive rate: (True positives + False positives) / Total count
#     """
# 
#     def score_with_threshold(self, result, threshold):
#         return ((self.tp(result, threshold) + self.fp(result, threshold)) /
#                                  result.y_test.count())
# 
# class Recall(ThresholdMetric):
#     """
#     Recall: True positives / (True positives + False negatives)
#     """
# 
#     def score_with_threshold(self, result, threshold):
#         return self.tp(result, threshold) / result.y_test.sum()


# Path: examples/iris.py
import urllib2
import pandas as pd
import sklearn
import ramp
import logging
from sklearn import decomposition
from ramp.features import *
from ramp.metrics import PositiveRate, Recall




logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# fetch and clean iris data from UCI
data = pd.read_csv(urllib2.urlopen(
    "http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"))
data = data.drop([149]) # bad line
columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']
data.columns = columns

# all features
features = [FillMissing(f, 0) for f in columns[:-1]]

# features, log transformed features, and interaction terms
expanded_features = (
    features +
    [Log(F(f) + 1) for f in features] +
    [
        F('sepal_width') ** 2,
        combo.Interactions(features),
    ]
)

reporters = [
    ramp.reporters.MetricReporter.factory(Recall(.4)),

    ramp.reporters.DualThresholdMetricReporter.factory(Recall(), PositiveRate())