-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule_based.py
34 lines (25 loc) · 964 Bytes
/
rule_based.py
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
import numpy as np
from pandas.api.types import is_sparse
from numpy.lib.function_base import select
from sklearn.base import BaseEstimator
class RuleBasedEstimator(BaseEstimator):
def __init__(self) -> None:
super().__init__()
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
return X
def predict(self, X):
X_ = X.copy()
# Rule No.1
X_['rule_based_predict'] = X_['device_brand_Spider'].values
if is_sparse(X_['rule_based_predict']):
rule_based_predict = X_['rule_based_predict'].sparse.to_dense().values
else:
rule_based_predict = X_['rule_based_predict'].values
if is_sparse(X_['pca_predict']):
pca_predict = X_['pca_predict'].sparse.to_dense().values
else:
pca_predict = X_['pca_predict'].values
predict = np.logical_or(rule_based_predict, pca_predict)
return predict