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.31-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee1a499b4d15a33edb715aa4aafff82a842dfc9e148c82bbf8584e407116db25 |
|
MD5 | 294c56cc1ae6b2eb7f07bbf3fe504f4c |
|
BLAKE2b-256 | 5a34ce6898d3ae1e906028ff1b1e0b21698fd377b71ab4290e09dac329da988d |