def train_model(data, with_mac=True):
global without_mac_clf, mac_clf
df = pd.DataFrame.from_dict(data)
y = df.pop("location")
features = [f for f in df.columns if f is not 'mac']
df = df.rename(columns=dict(zip(features, [POWER_SLAVE_PREFIX + f for f in features])))
model_name = MODEL_MAC_NAME if with_mac else MODEL_NAME
if with_mac:
df = df.apply(LabelEncoder().fit_transform)
else:
df.drop("mac", axis=1, inplace=True)
clf = DecisionTreeClassifier()
clf.fit(df, y)
joblib.dump(clf, model_name)
if with_mac and mac_clf is None:
mac_clf = clf
if not with_mac and without_mac_clf is None:
without_mac_clf = clf
export_graphviz(clf, feature_names=list(df.columns), class_names=y.unique(), filled=True, rounded=True, out_file='model.dot')
os.system("dot -Tpng model.dot -o model.png")
评论列表
文章目录