#!/usr/bin/env python

"""
Example: combining multiple inductive conformal regressors
"""

# Authors: Henrik Linusson

from sklearn.tree import DecisionTreeRegressor
from sklearn.datasets import load_diabetes

from nonconformist.ensemble import *
from nonconformist.regression import IcpRegressor, RegressorNc
from nonconformist.regression import absolute_error, absolute_error_inverse

data = load_diabetes()

# -----------------------------------------------------------------------------
# Setup training, calibration and test indices
# -----------------------------------------------------------------------------
idx = np.random.permutation(data.target.size)
train = idx[:int(2 * idx.size / 3)]
test = idx[int(2 * idx.size / 3):]

# -----------------------------------------------------------------------------
# Train and calibrate
# -----------------------------------------------------------------------------
rscp = SubSamplingCp(IcpRegressor,
                     RegressorNc,
                     nc_class_params={'model_class': DecisionTreeRegressor,
                                      'err_func': absolute_error,
                                      'inverse_err_func': absolute_error_inverse})
rscp.fit(data.data[train, :], data.target[train])

ccp = CrossCp(IcpRegressor,
              RegressorNc,
              nc_class_params={'model_class': DecisionTreeRegressor,
                               'err_func': absolute_error,
                               'inverse_err_func': absolute_error_inverse})
ccp.fit(data.data[train, :], data.target[train])

bcp = BootstrapCp(IcpRegressor,
                  RegressorNc,
                  nc_class_params={'model_class': DecisionTreeRegressor,
                                   'err_func': absolute_error,
                                   'inverse_err_func': absolute_error_inverse})
bcp.fit(data.data[train, :], data.target[train])

# -----------------------------------------------------------------------------
# Predict
# -----------------------------------------------------------------------------
import pandas

prediction = rscp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max','Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('RSCP')
print(df)

prediction = ccp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max','Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('CCP')
print(df)

prediction = bcp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max','Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('BCP')
print(df)
2ce45f14fdb67a0f11a067a7b22d5f06beb07204
<mask token>

<mask token>
rscp.fit(data.data[train, :], data.target[train])
<mask token>
ccp.fit(data.data[train, :], data.target[train])
<mask token>
bcp.fit(data.data[train, :], data.target[train])
<mask token>
print('RSCP')
print(df)
<mask token>
print('CCP')
print(df)
<mask token>
print('BCP')
print(df)

<mask token>
data = load_diabetes()
idx = np.random.permutation(data.target.size)
train = idx[:int(2 * idx.size / 3)]
test = idx[int(2 * idx.size / 3):]
rscp = SubSamplingCp(IcpRegressor, RegressorNc, nc_class_params={
    'model_class': DecisionTreeRegressor, 'err_func': absolute_error,
    'inverse_err_func': absolute_error_inverse})
rscp.fit(data.data[train, :], data.target[train])
ccp = CrossCp(IcpRegressor, RegressorNc, nc_class_params={'model_class':
    DecisionTreeRegressor, 'err_func': absolute_error, 'inverse_err_func':
    absolute_error_inverse})
ccp.fit(data.data[train, :], data.target[train])
bcp = BootstrapCp(IcpRegressor, RegressorNc, nc_class_params={'model_class':
    DecisionTreeRegressor, 'err_func': absolute_error, 'inverse_err_func':
    absolute_error_inverse})
bcp.fit(data.data[train, :], data.target[train])
<mask token>
prediction = rscp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max', 'Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('RSCP')
print(df)
prediction = ccp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max', 'Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('CCP')
print(df)
prediction = bcp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max', 'Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('BCP')
print(df)

<mask token>
from sklearn.tree import DecisionTreeRegressor
from sklearn.datasets import load_diabetes
from nonconformist.ensemble import *
from nonconformist.regression import IcpRegressor, RegressorNc
from nonconformist.regression import absolute_error, absolute_error_inverse
data = load_diabetes()
idx = np.random.permutation(data.target.size)
train = idx[:int(2 * idx.size / 3)]
test = idx[int(2 * idx.size / 3):]
rscp = SubSamplingCp(IcpRegressor, RegressorNc, nc_class_params={
    'model_class': DecisionTreeRegressor, 'err_func': absolute_error,
    'inverse_err_func': absolute_error_inverse})
rscp.fit(data.data[train, :], data.target[train])
ccp = CrossCp(IcpRegressor, RegressorNc, nc_class_params={'model_class':
    DecisionTreeRegressor, 'err_func': absolute_error, 'inverse_err_func':
    absolute_error_inverse})
ccp.fit(data.data[train, :], data.target[train])
bcp = BootstrapCp(IcpRegressor, RegressorNc, nc_class_params={'model_class':
    DecisionTreeRegressor, 'err_func': absolute_error, 'inverse_err_func':
    absolute_error_inverse})
bcp.fit(data.data[train, :], data.target[train])
import pandas
prediction = rscp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max', 'Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('RSCP')
print(df)
prediction = ccp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max', 'Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('CCP')
print(df)
prediction = bcp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max', 'Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('BCP')
print(df)

#!/usr/bin/env python

"""
Example: combining multiple inductive conformal regressors
"""

# Authors: Henrik Linusson

from sklearn.tree import DecisionTreeRegressor
from sklearn.datasets import load_diabetes

from nonconformist.ensemble import *
from nonconformist.regression import IcpRegressor, RegressorNc
from nonconformist.regression import absolute_error, absolute_error_inverse

data = load_diabetes()

# -----------------------------------------------------------------------------
# Setup training, calibration and test indices
# -----------------------------------------------------------------------------
idx = np.random.permutation(data.target.size)
train = idx[:int(2 * idx.size / 3)]
test = idx[int(2 * idx.size / 3):]

# -----------------------------------------------------------------------------
# Train and calibrate
# -----------------------------------------------------------------------------
rscp = SubSamplingCp(IcpRegressor,
                     RegressorNc,
                     nc_class_params={'model_class': DecisionTreeRegressor,
                                      'err_func': absolute_error,
                                      'inverse_err_func': absolute_error_inverse})
rscp.fit(data.data[train, :], data.target[train])

ccp = CrossCp(IcpRegressor,
              RegressorNc,
              nc_class_params={'model_class': DecisionTreeRegressor,
                               'err_func': absolute_error,
                               'inverse_err_func': absolute_error_inverse})
ccp.fit(data.data[train, :], data.target[train])

bcp = BootstrapCp(IcpRegressor,
                  RegressorNc,
                  nc_class_params={'model_class': DecisionTreeRegressor,
                                   'err_func': absolute_error,
                                   'inverse_err_func': absolute_error_inverse})
bcp.fit(data.data[train, :], data.target[train])

# -----------------------------------------------------------------------------
# Predict
# -----------------------------------------------------------------------------
import pandas

prediction = rscp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max','Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('RSCP')
print(df)

prediction = ccp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max','Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('CCP')
print(df)

prediction = bcp.predict(data.data[test, :], significance=0.1)
header = np.array(['min', 'max','Truth'])
table = np.vstack([prediction.T, data.target[test]]).T
df = pandas.DataFrame(np.vstack([header, table]))
print('BCP')
print(df)