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

arguments.py 1.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
  1. import numpy as np
  2. import pandas as pd
  3. from sklearn.base import ClassifierMixin
  4. from sklearn.utils.validation import check_X_y, check_array
  5. from sklearn.utils.multiclass import check_classification_targets
  6. import scipy.sparse
  7. def check_fit_arguments(model, X, y, feature_names):
  8. """Process arguments for fit and predict methods.
  9. """
  10. if isinstance(model, ClassifierMixin):
  11. model.classes_, y = np.unique(y, return_inverse=True) # deals with str inputs
  12. check_classification_targets(y)
  13. if feature_names is None:
  14. if isinstance(X, pd.DataFrame):
  15. model.feature_names_ = X.columns
  16. elif isinstance(X, list):
  17. model.feature_names_ = ['X' + str(i) for i in range(len(X[0]))]
  18. else:
  19. model.feature_names_ = ['X' + str(i) for i in range(X.shape[1])]
  20. else:
  21. model.feature_names_ = feature_names
  22. if scipy.sparse.issparse(X):
  23. X = X.toarray()
  24. X, y = check_X_y(X, y)
  25. _, model.n_features_in_ = X.shape
  26. assert len(model.feature_names_) == model.n_features_in_, 'feature_names should be same size as X.shape[1]'
  27. y = y.astype(float)
  28. return X, y, model.feature_names_
  29. def check_fit_X(X):
  30. """Process X argument for fit and predict methods.
  31. """
  32. if scipy.sparse.issparse(X):
  33. X = X.toarray()
  34. X = check_array(X)
  35. return X
Tip!

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

Comments

Loading...