This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# __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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(): |
NewerOlder