Skip to main content

A python package for count regression of rare events assisted by metaheuristics

Project description

The Below code demonstrates how to set up automatic optimization assisted by the harmony search algorithm. Referencs to the Differential Evolution and Simulated Annealing has been mentioned (change accordingly)
import pandas as pd
import numpy as np
   
from metacountregressor.solution import ObjectiveFunction
from metacountregressor.metaheuristics import (harmony_search_non_mp,
                                            differential_evolution_non_mp,
                                            simulated_annealing_non_mp)

Basic setup. Read in data, and select optimization algorithm

# Read data from CSV file
df = pd.read_csv(
"https://raw.githubusercontent.com/zahern/data/main/Ex-16-3.csv")
X = df
y = df['FREQ']  # Frequency of crashes
X['Offset'] = np.log(df['AADT']) # Explicitley define how to offset the data, no offset otherwise
# Drop Y, selected offset term and  ID as there are no panels
X = df.drop(columns=['FREQ', 'ID', 'AADT'])  

#some example argument, these are defualt so the following line is just for claritity
arguments = {'algorithm': 'hs', 'test_percentage': 0.2, 'test_complexity': 6, 'instance_number':1}
# Fit the model with metacountregressor
obj_fun = ObjectiveFunction(X, y, **arguments)
#replace with other metaheuristics if desired
results = harmony_search_non_mp(obj_fun)

Change the arguments.

Reduce down the list sizes where nescessary

#Solution Arguments
arguments = {
        'algorithm': 'hs',
        'test_percentage': 0.2,
        'test_complexity': 6, #or list based [0, 1, 2, 6]
        'instance_number': 'name',
        'is_multi': 1,
        'distribution': ['Normal', 'LnNormal', 'Triangular', 'Unifrom', 'Gamma', 'Lindley'],
        'Model': [0,1,2],  # or equivalently ['POS', 'NB', 'GP']
        'transformations': ['no', 'sqrt', 'archsinh', 'asfactor'],
    }
obj_fun = ObjectiveFunction(X, y, **arguments)
results = harmony_search_non_mp(obj_fun)

Initial Solution Configurement

 #Model Decisions, Specify for Intial Optimization
manual_fit_spec = {
    'fixed_terms': ['SINGLE', 'LENGTH'],
    'rdm_terms': ['AADT:normal'],
    'rdm_cor_terms': ['GRADEBR:uniform', 'CURVES:triangular'],
    'grouped_terms': [],
    'hetro_in_means': ['ACCESS:normal', 'MINRAD:normal'],
    'transformations': ['no', 'no', 'log', 'no', 'no', 'no', 'no'],
    'dispersion': 1
}

#Search Arguments
arguments = {
    'algorithm': 'hs',
    'test_percentage': 0.2,
    'test_complexity': 6,
    'instance_number': 'name',
    'Manual_Fit': manual_fit_spec
}
obj_fun = ObjectiveFunction(X, y, **arguments)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

metacountregressor-0.1.32-py3-none-any.whl (13.9 MB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page