-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathexample_rerfClassifier.py
More file actions
63 lines (44 loc) · 1.58 KB
/
Copy pathexample_rerfClassifier.py
File metadata and controls
63 lines (44 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Example shows usage of rerfClassifier class.
Based on https://www.datacamp.com/community/tutorials/random-forests-classifier-python
with rerfClassifier swapped out instead of sklearn's RandomForestClassifier
"""
from rerf.rerfClassifier import rerfClassifier
# Import scikit-learn dataset library
from sklearn import datasets
# Load dataset
iris = datasets.load_iris()
# print the label species(setosa, versicolor,virginica)
print(iris.target_names)
# print the names of the four features
print(iris.feature_names)
# Creating a DataFrame of given iris dataset.
import pandas as pd
data = pd.DataFrame(
{
"sepal length": iris.data[:, 0],
"sepal width": iris.data[:, 1],
"petal length": iris.data[:, 2],
"petal width": iris.data[:, 3],
"species": iris.target,
}
)
print(data.head())
# Import train_test_split function
from sklearn.model_selection import train_test_split
X = data[["sepal length", "sepal width", "petal length", "petal width"]] # Features
y = data["species"] # Labels
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3
) # 70% training and 30% test
# Create a Gaussian Classifier
clf = rerfClassifier(n_estimators=100)
print(clf)
# Train the model using the training sets y_pred=clf.predict(X_test)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
# Import scikit-learn metrics module for accuracy calculation
from sklearn import metrics
# Model Accuracy, how often is the classifier correct?
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))