- Learning Data Mining with Python(Second Edition)
- Robert Layton
- 265字
- 2021-07-02 23:40:09
Using decision trees
We can import the DecisionTreeClassifier class and create a Decision Tree using scikit-learn:
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier(random_state=14)
We used 14 for our random_state again and will do so for most of the book. Using the same random seed allows for replication of experiments. However, with your experiments, you should mix up the random state to ensure that the algorithm's performance is not tied to the specific value.
We now need to extract the dataset from our pandas data frame in order to use it with our scikit-learn classifier. We do this by specifying the columns we wish to use and using the values parameter of a view of the data frame. The following code creates a dataset using our last win values for both the home team and the visitor team:
X_previouswins = dataset[["HomeLastWin", "VisitorLastWin"]].values
Decision trees are estimators, as introduced in Chapter 2, Classifying using scikit-learn Estimators, and therefore have fit and predict methods. We can also use the cross_val_score method to get the average score (as we did previously):
from sklearn.cross_validation import cross_val_score
import numpy as np
scores = cross_val_score(clf, X_previouswins, y_true,
scoring='accuracy')
print("Accuracy: {0:.1f}%".format(np.mean(scores) * 100))
This scores 59.4 percent: we are better than choosing randomly! However, we aren't beating our other baseline of just choosing the home team. In fact, we are pretty much exactly the same. We should be able to do better. Feature engineering is one of the most difficult tasks in data mining, and choosing good features is key to getting good outcomes—more so than choosing the right algorithm!
- Python數(shù)據(jù)分析入門與實(shí)戰(zhàn)
- Cocos2d-x游戲開發(fā):手把手教你Lua語言的編程方法
- Raspberry Pi for Secret Agents(Third Edition)
- Bootstrap 4:Responsive Web Design
- Rust Essentials(Second Edition)
- 算法訓(xùn)練營:提高篇(全彩版)
- C語言程序設(shè)計(jì)
- Statistical Application Development with R and Python(Second Edition)
- Mastering Web Application Development with AngularJS
- HTML+CSS+JavaScript編程入門指南(全2冊)
- Arduino Wearable Projects
- Training Systems Using Python Statistical Modeling
- Mudbox 2013 Cookbook
- 輕松學(xué)Scratch 3.0 少兒編程(全彩)
- Learning iOS Penetration Testing