Python में Tree-Based Models के साथ Machine Learning
Elie Kawerk
Data Scientist
समझने में आसान.
व्याख्या करने में आसान.
इस्तेमाल में आसान.
लचीलापन: non-linear dependencies का वर्णन कर सकता है.
प्रीप्रोसेसिंग: फीचर्स को standardize/normalize करने की ज़रूरत नहीं, ...
Classification: केवल orthogonal decision boundaries बनाता है.
प्रशिक्षण सेट में छोटे बदलावों के प्रति संवेदनशील.
उच्च variance: बिना बाधा वाले CARTs प्रशिक्षण सेट पर overfit कर सकते हैं.
समाधान: एन्सेम्बल लर्निंग.
एक ही डेटासेट पर अलग-अलग मॉडल ट्रेन करें.
हर मॉडल अपनी predictions दे.
मेटा-मॉडल: individual models की predictions को समेकित करता है.
अंतिम prediction: ज़्यादा robust और कम त्रुटिप्रवण.
सर्वोत्तम परिणाम: जब मॉडल अलग-अलग तरीकों से निपुण हों.

बाइनरी classification टास्क.
$N$ classifiers predictions देते हैं: $P_1$, $P_2$, ..., $P_N$ जहाँ $P_i$ = 0 या 1.
मेटा-मॉडल prediction: hard voting.

# Import functions to compute accuracy and split data
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
# Import models, including VotingClassifier meta-model
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.ensemble import VotingClassifier
# Set seed for reproducibility
SEED = 1
# Split data into 70% train and 30% test X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.3, random_state= SEED) # Instantiate individual classifiers lr = LogisticRegression(random_state=SEED) knn = KNN() dt = DecisionTreeClassifier(random_state=SEED)# Define a list called classifier that contains the tuples (classifier_name, classifier) classifiers = [('Logistic Regression', lr), ('K Nearest Neighbours', knn), ('Classification Tree', dt)]
# Iterate over the defined list of tuples containing the classifiers
for clf_name, clf in classifiers:
#fit clf to the training set
clf.fit(X_train, y_train)
# Predict the labels of the test set
y_pred = clf.predict(X_test)
# Evaluate the accuracy of clf on the test set
print('{:s} : {:.3f}'.format(clf_name, accuracy_score(y_test, y_pred)))
Logistic Regression: 0.947
K Nearest Neighbours: 0.930
Classification Tree: 0.930
# Instantiate a VotingClassifier 'vc'
vc = VotingClassifier(estimators=classifiers)
# Fit 'vc' to the traing set and predict test set labels
vc.fit(X_train, y_train)
y_pred = vc.predict(X_test)
# Evaluate the test-set accuracy of 'vc'
print('Voting Classifier: {.3f}'.format(accuracy_score(y_test, y_pred)))
Voting Classifier: 0.953
Python में Tree-Based Models के साथ Machine Learning