68 lines
2.7 KiB
Python
68 lines
2.7 KiB
Python
|
|
from xml.parsers.expat import model
|
||
|
|
import ml_utiliy
|
||
|
|
import json
|
||
|
|
from sklearn.model_selection import train_test_split#训练集/测试机划分函数
|
||
|
|
|
||
|
|
|
||
|
|
class TrainModle(object):
|
||
|
|
def __init__(self) -> None:
|
||
|
|
self.models_={}
|
||
|
|
self.ihd_=ml_utiliy.ihyperDB()
|
||
|
|
def Train(self,param_json):
|
||
|
|
ruleid=param_json["ruleid"]
|
||
|
|
model_type=param_json["model_type"]
|
||
|
|
stime=param_json["stime"]
|
||
|
|
etime=param_json["etime"]
|
||
|
|
features=param_json["features"]
|
||
|
|
target=param_json["target"]
|
||
|
|
[target_flags,target_data]=self.ihd_.select_raw_data(target,stime,etime)
|
||
|
|
# print(target_data)
|
||
|
|
y=ml_utiliy.np.array(ml_utiliy.copy.deepcopy(target_data))
|
||
|
|
X_d=[]
|
||
|
|
for k in features:
|
||
|
|
# print(k)
|
||
|
|
[feature_flag,feature_data]=self.ihd_.select_raw_data(k,stime,etime)
|
||
|
|
X_d.append(ml_utiliy.np.array(ml_utiliy.copy.deepcopy(feature_data)))
|
||
|
|
X=ml_utiliy.np.array(X_d).T
|
||
|
|
print(X.shape)
|
||
|
|
print(y.shape)
|
||
|
|
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2,stratify=y)
|
||
|
|
scores=ml_utiliy.train(ruleid=ruleid,model_type=model_type, x_train=x_train,y_train=y_train)
|
||
|
|
self.models_[ruleid+model_type]=ml_utiliy.read_mode(ruleid=ruleid,model_type=model_type)
|
||
|
|
print(scores)
|
||
|
|
print(ml_utiliy.predict_score(ruleid=ruleid,model_type=model_type, x_test=x_test,y_test=y_test))
|
||
|
|
return scores
|
||
|
|
|
||
|
|
def Predict(self,param_json):
|
||
|
|
ruleid=param_json["ruleid"]
|
||
|
|
model_type=param_json["model_type"]
|
||
|
|
stime=param_json["stime"]
|
||
|
|
etime=param_json["etime"]
|
||
|
|
features=param_json["features"]
|
||
|
|
target=param_json["target"]
|
||
|
|
try:
|
||
|
|
self.models_[ruleid+model_type]
|
||
|
|
except:
|
||
|
|
print("载入")
|
||
|
|
self.models_[ruleid+model_type]=ml_utiliy.read_mode(ruleid=ruleid,model_type=model_type)
|
||
|
|
[target_flags,target_data]=self.ihd_.select_raw_data(target,stime,etime)
|
||
|
|
# print(target_data)
|
||
|
|
y=ml_utiliy.np.array(ml_utiliy.copy.deepcopy(target_data))
|
||
|
|
X_d=[]
|
||
|
|
for k in features:
|
||
|
|
# print(k)
|
||
|
|
[feature_flag,feature_data]=self.ihd_.select_raw_data(k,stime,etime)
|
||
|
|
X_d.append(ml_utiliy.np.array(ml_utiliy.copy.deepcopy(feature_data)))
|
||
|
|
X=ml_utiliy.np.array(X_d).T
|
||
|
|
print(X.shape)
|
||
|
|
print(y.shape)
|
||
|
|
if(self.models_[ruleid+model_type]):
|
||
|
|
scores=self.models_[ruleid+model_type].score(X,y)
|
||
|
|
print("scores:{}".format(scores))
|
||
|
|
return scores
|
||
|
|
print("ERROR")
|
||
|
|
scores=ml_utiliy.predict_score(ruleid=ruleid,model_type=model_type,x_test=X,y_test=y)
|
||
|
|
print("scores:{}".format(scores))
|
||
|
|
return scores
|
||
|
|
|