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
Close
Hashes for metacountregressor-0.1.30-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 914a4755d27eaf88094ed3b327fd5e3269d12fe8a34ca42efc20b8a1c1503a6e |
|
MD5 | 203f00e51d60f8fdb9cdcc663e150893 |
|
BLAKE2b-256 | 7e6a32a29468b06ffb78f6ab2f48a174e6996e15cb19287b2db7ad4a2a3e189d |