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

demo_helper.py 2.3 KB

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
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
  1. import numpy as np
  2. from scipy.io.arff import loadarff
  3. from sklearn import metrics
  4. import matplotlib.pyplot as plt
  5. from sklearn.datasets import fetch_openml
  6. from sklearn.model_selection import train_test_split
  7. import os
  8. path_to_current_file = os.path.dirname(os.path.abspath(__file__))
  9. def viz_classification_preds(probs, y_test):
  10. '''look at prediction breakdown
  11. '''
  12. plt.subplot(121)
  13. plt.hist(probs[:, 1][y_test == 0], label='Class 0')
  14. plt.hist(probs[:, 1][y_test == 1], label='Class 1', alpha=0.8)
  15. plt.ylabel('Count')
  16. plt.xlabel('Predicted probability of class 1')
  17. plt.legend()
  18. plt.subplot(122)
  19. preds = np.argmax(probs, axis=1)
  20. plt.title('ROC curve')
  21. fpr, tpr, thresholds = metrics.roc_curve(y_test, preds)
  22. plt.xlabel('False positive rate')
  23. plt.ylabel('True positive rate')
  24. plt.plot(fpr, tpr)
  25. plt.tight_layout()
  26. plt.show()
  27. def get_ames_data():
  28. try:
  29. housing = fetch_openml(name="house_prices", as_frame=True, parser='auto')
  30. except:
  31. housing = fetch_openml(name="house_prices", as_frame=True)
  32. housing_target = housing['target'].values
  33. housing_data_numeric = housing['data'].select_dtypes('number').drop(columns=['Id']).dropna(axis=1)
  34. feature_names = housing_data_numeric.columns.values
  35. X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(
  36. housing_data_numeric.values, housing_target, test_size=0.75)
  37. return X_train_reg, X_test_reg, y_train_reg, y_test_reg, feature_names
  38. def get_diabetes_data():
  39. '''load (classification) data on diabetes
  40. '''
  41. data = loadarff(os.path.join(path_to_current_file, "../tests/test_data/diabetes.arff"))
  42. data_np = np.array(list(map(lambda x: np.array(list(x)), data[0])))
  43. X = data_np[:, :-1].astype('float32')
  44. y_text = data_np[:, -1].astype('str')
  45. y = (y_text == 'tested_positive').astype(int) # labels 0-1
  46. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.75) # split
  47. feature_names = ["#Pregnant", "Glucose concentration test", "Blood pressure(mmHg)",
  48. "Triceps skin fold thickness(mm)",
  49. "2-Hour serum insulin (mu U/ml)", "Body mass index", "Diabetes pedigree function", "Age (years)"]
  50. return X_train, X_test, y_train, y_test, feature_names
Tip!

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

Comments

Loading...