$50.00
Order Nowfrom sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, plot_confusion_matrix
Investigate following parameters of Random Forest classifier and tune them using Randomized Search and Grid Search.
from sklearn.model_selection import RandomizedSearchCV,GridSearchCV
# Number of trees in random forest
n_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]
# Number of features to consider at every split
max_features = ['auto', 'sqrt','log2']
# Maximum number of levels in tree
max_depth = [int(x) for x in np.linspace(10, 1000,10)]
# Minimum number of samples required to split a node
min_samples_split = [2, 5, 8, 11,14]
# Minimum number of samples required at each leaf node
min_samples_leaf = [1, 2, 4,6,8]
Load iris dataset from sklearn.
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
x = {'JAN':31.9, 'FEB':32.3, 'MAR':35, 'APR':52, 'MAY':60.8, 'JUN':68.7, 'JUL':73.3, 'AUG':72.1, 'SEP':65.2, 'OCT':54.8, 'NOV':40, 'DEC':38}
hac = HAC(param={'dist': 'eucl'})
hac.fit(x)
for c in hac.dendrogram:
print(c)
Expected output:
(0, ['JAN', 'FEB'], 0.4)
(1, ['JUL', 'AUG'], 1.2)
(2, ['NOV', 'DEC'], 2.0)
(3, ['APR', 'OCT'], 2.8)
(4, ['JAN', 'FEB', 'MAR'], 2.9)
(5, ['JUN', 'SEP'], 3.5)
(6, ['APR', 'OCT', 'MAY'], 7.4)
class HAC:
def __init__(self, X, param):
self.X = x
self.__distances__(param['dist'])
def __distances__(self, dist='eucl'):
'''
Implement __distances__ method to caculate pair-wise distances
among datapoint in X with respect to distance measures
- eucl : eucledean distance
- manh : manhattan
- misk : miskownski
'''
if dist not in ['eucl', 'manh', 'misk']:
raise Exception('Not a valid dist measure. Choose among eucl, manh, misk')
self.C = None
def __merge__(self):
'''
Implement __merge__ method to recursively merge the nearest datapoints in X
using pair-wise distances matrix X.
Save the merge results at each iteration/'recursive call'
in dendrogram list of clusters.
'''
self.denrogram = None
def __display__(self):
'''
Implement __display__ method to cretively show the contents of dendrogram.
'''
pass
def fit(self, X):
self.X = list(x.values())
self.labels = list(x.keys())
self.__distances__()
self.dendrogram = list()
self.__merge__()
<__main__.HAC at 0x7f6b46433050>
WhatsApp us