Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

train.py 777 B

You have to be logged in to leave a comment. Sign In
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
  1. from sklearn.ensemble import RandomForestClassifier
  2. from sklearn.metrics import ConfusionMatrixDisplay
  3. import matplotlib.pyplot as plt
  4. import json
  5. import os
  6. import numpy as np
  7. # Read in data
  8. X_train = np.genfromtxt("data/train_features.csv")
  9. y_train = np.genfromtxt("data/train_labels.csv")
  10. X_test = np.genfromtxt("data/test_features.csv")
  11. y_test = np.genfromtxt("data/test_labels.csv")
  12. # Fit a model
  13. depth = 2
  14. clf = RandomForestClassifier(max_depth=depth)
  15. clf.fit(X_train, y_train)
  16. acc = clf.score(X_test, y_test)
  17. print(acc)
  18. with open("metrics.txt", "w") as outfile:
  19. outfile.write("Accuracy: " + str(acc) + "\n")
  20. # Plot it
  21. disp = ConfusionMatrixDisplay.from_estimator(
  22. clf, X_test, y_test, normalize="true", cmap=plt.cm.Blues
  23. )
  24. plt.savefig("confusion_matrix.png")
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...