Skip to content

Instantly share code, notes, and snippets.

View StephenFordham's full-sized avatar

Stephen Fordham StephenFordham

View GitHub Profile
@StephenFordham
StephenFordham / PRC_skillful_model.py
Created October 23, 2023 09:51
Precision-recall curve for a skillful model
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_recall_curve, auc
import seaborn as sns
X, y = make_classification(n_samples=1000, n_classes=2, random_state=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2)
model = LogisticRegression(solver='lbfgs')
@StephenFordham
StephenFordham / AUC_ROC_model.py
Last active October 23, 2023 08:26
AUC ROC Skillful model comparison
import matplotlib.pyplot as plt
# ROC and AUC modules
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve, roc_auc_score
import seaborn as sns
# generate 2 class dataset
X, y = make_classification(n_samples=1000, n_classes=2, weights=[0.5], random_state=1)
@StephenFordham
StephenFordham / init_constructor_decorator.py
Created June 9, 2022 18:08
init_constructor_decorator
# __init__ method with a decorator
def attr_validation(method):
def inner(ref, name, age, location):
for attr in [name, location]:
if not isinstance(attr, str):
raise TypeError('Name and Location attributes must be of type str')
if not isinstance(age, int):
raise TypeError('Age attributes must be of type int')
return method(ref, name, age, location)
return inner
@StephenFordham
StephenFordham / __setattr__part2.py
Created June 9, 2022 17:09
__setattr__catching_exceptions
e1 = Employees(46347832, 30, 'Bournemouth')
for key, value in e1.__dict__.items():
print('{}: {}'.format(key, value))
#Console output
TypeError: Only valid attributes of type string are accepted
@StephenFordham
StephenFordham / __setattr__magic_method.py
Created June 9, 2022 17:04
__setattr__magic_method
class Employees(object):
def __init__(self, name, age, location):
self._name = name
self._age = age
self._location = location
def __setattr__(self, key, value):
if key in ['_name', '_location']:
if not isinstance(value, str):
raise TypeError('Only valid attributes of type string are accepted')
class Employees(object):
def __init__(self, name, age, location):
self._name = name
self._age = age
self._location = location
def __call__(self, *args, **my_attr_dict):
if len(my_attr_dict) > 0:
try:
self._name = my_attr_dict['_name']
@StephenFordham
StephenFordham / Instantiating_Employees_objects.py
Created June 9, 2022 15:23
Instantiating an instance of the Employees class
class Employees(object):
def __init__(self, name, age, location):
self._name = name
self._age = age
self._location = location
e1 = Employees('stephen', 30, 'Bournemouth')
for key, value in e1.__dict__.items():
print('{}: {}'.format(key, value))
class StrAttrValidation(object):
def __init__(self):
self._dict = dict()
def __set__(self, instance, value):
if not isinstance(value, str):
raise AttributeError('Only Str type valid')
self._dict[instance] = value.title()
def __get__(self, instance, value):
@StephenFordham
StephenFordham / Using_classes_to_validate_attributes.py
Last active February 12, 2022 18:35
Example_2_using_classes.py
class StrAttrValidation(object):
def __init__(self):
self._dict = dict()
def __set__(self, instance, value):
self._dict[instance] = value.title()
def __get__(self, instance, value):
return self._dict[instance]
@StephenFordham
StephenFordham / Example_1_call_method_Part D.py
Created February 12, 2022 11:50
Unusual ways to control attrbute access in Python, Example 1
emp = Employees('stephen', 'bournemouth', 30)
for key, value in emp.__dict__.items():
print('{} = {}'.format(key, value))
print('###################################')
emp(location='newloc')
for key, value in emp.__dict__.items():