-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample_scikit.py
More file actions
78 lines (56 loc) · 2.19 KB
/
Copy pathexample_scikit.py
File metadata and controls
78 lines (56 loc) · 2.19 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Scikit Example
==============
An example to demonstrate the usage of Howso in "traditional" ML ways.
The howso python package extends the scikit-learn Estimator via the following classes:
* `howso.scikit.HowsoEstimator`
* `howso.scikit.HowsoClassifier`
* `howso.scikit.HowsoRegressor`
HowsoEstimator provides users with a Python interface that follows the
conventions of sklearn estimators. For use of Howso's functionality use
`howso.engine.Trainee`.
This is a simple example on how to use the `howso.scikit.HowsoRegressor`
which extends the `howso.scikit.HowsoEstimator` to fit data and make
predictions based on that data.
"""
import pandas as pd
from pprint import pprint
from howso.scikit import HowsoClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
def main():
# Read in the data.
print("Reading breast cancer data set.")
df = pd.read_csv("data/breast_cancer.csv")
# Split the dataset into the feature (X) and targets (y)
X = df.drop('y', axis=1).values.astype(float)
y = df['y'].values.astype(float)
le = LabelEncoder()
le.fit(df['y'])
y = le.transform(df['y'])
print(f"Target values encoded from {list(le.classes_)} to "
f"{list(le.transform(le.classes_))}.")
# Split the dataset into an 80/20 train/test set.
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, shuffle=True)
# Create a classifier.
dp = HowsoClassifier()
# Fit the training data.
print("Training on a random selection of 80% of the data.")
dp.fit(X_train, y_train)
# Test against the reserved test data.
print("Scoring against 20% reserve test data:")
score = dp.score(X_test, y_test)
# Print the resulting accuracy.
print(score)
# Detailed prediction results
print("Getting details for most similar cases from the first prediction:")
results = dp.describe_prediction(X_test)
pprint(results['details']['most_similar_cases'][0])
print("Getting class probabilities and classes for the model:")
probas = dp.predict_proba(X_test)
pprint(probas)
pprint(dp.classes_)
return score
if __name__ == "__main__":
main()