parent
d6db0ce71f
commit
e37b42b49c
Binary file not shown.
@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
# @author: 原凯峰
|
||||
# @contact: 2894340009@qq.com
|
||||
# @software: pycharm
|
||||
# @file: MachineLearningDivider.py
|
||||
# @time: 2024/6/26 8:21
|
||||
# @desc:利用随机森林法进行模型训练,能够通过平均响应时间、故障率等数据计算出服务器的健康状态
|
||||
|
||||
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
from sklearn.metrics import classification_report, accuracy_score
|
||||
import pickle
|
||||
def trainmodel():
|
||||
# 假设我们有以下数据集
|
||||
X = [
|
||||
[0.3, 0.005], # 服务器特征:平均响应时间和故障率
|
||||
[2.5, 0.03],
|
||||
[0.7, 0.045],
|
||||
[1.2, 0.002],
|
||||
[3.5, 0.1],
|
||||
[1.3, 0.05],
|
||||
[0.01, 0.15], # 服务器特征:平均响应时间和故障率
|
||||
[5, 0.03],
|
||||
[0.7, 0.015],
|
||||
[1.4, 0.02],
|
||||
[0.15, 0.2],
|
||||
[1.3, 0.005],
|
||||
|
||||
]
|
||||
y = ['良好', '一般', '一般', '良好', '差', '一般', '一般', '差', '良好', '差', '差', '良好'] # 对应的健康状态标签
|
||||
|
||||
# 将健康状态标签转换为数值
|
||||
label_mapping = {'一般': 0, '良好': 1, '差': 2}
|
||||
y_encoded = [label_mapping[label] for label in y]
|
||||
|
||||
# 划分训练集和测试集
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.4, random_state=42)
|
||||
|
||||
# 选择模型,这里使用随机森林分类器
|
||||
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
||||
|
||||
# 训练模型
|
||||
model.fit(X_train, y_train)
|
||||
|
||||
# 预测测试集
|
||||
y_pred = model.predict(X_test)
|
||||
|
||||
# 评估模型
|
||||
print(classification_report(y_test, y_pred))
|
||||
print("Accuracy:", accuracy_score(y_test, y_pred))
|
||||
|
||||
# 保存模型
|
||||
with open('server_health_model.pkl', 'wb') as file:
|
||||
pickle.dump(model, file)
|
||||
|
||||
|
||||
# trainmodel()
|
||||
# 定义一个函数来加载模型并进行预测
|
||||
def load_model_and_predict(new_data):
|
||||
with open('../LogAnalyze/server_health_model.pkl', 'rb') as file:
|
||||
loaded_model = pickle.load(file)
|
||||
predictions = loaded_model.predict(new_data)
|
||||
return predictions
|
||||
|
||||
# 定义一个函数来将预测结果转换为健康等级
|
||||
def predict_health_status(new_data):
|
||||
label_mapping = {'一般': 0, '良好': 1, '差': 2}
|
||||
predictions = load_model_and_predict(new_data)
|
||||
# 创建逆向映射字典
|
||||
inverse_label_mapping = {value: key for key, value in label_mapping.items()}
|
||||
# 使用逆向映射字典转换预测结果
|
||||
health_status = [inverse_label_mapping[pred] for pred in predictions]
|
||||
return health_status
|
||||
|
||||
# 测试函数
|
||||
def testcase():
|
||||
new_data = [[0.4, 0.01]] # 新的服务器数据
|
||||
health_status = predict_health_status(new_data)
|
||||
print("预测的健康状态:", health_status)
|
||||
|
||||
# testcase()
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,22 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# 定义时间格式
|
||||
date_format = "%Y-%m-%d"
|
||||
|
||||
# 获取当前日期,并按照定义的格式转换
|
||||
current_date = datetime.now().strftime(date_format)
|
||||
# 去除前导零
|
||||
current_date = current_date.replace("-0", "-")
|
||||
# 打印当前日期
|
||||
print("当前日期:", current_date)
|
||||
|
||||
for day in range(1,16):
|
||||
|
||||
# 计算当前日期之前15天的日期
|
||||
delta = timedelta(days=-day)
|
||||
previous_date = (datetime.now() + delta).strftime(date_format)
|
||||
# 去除前导零
|
||||
previous_date = previous_date.replace("-0", "-")
|
||||
print(previous_date)
|
||||
# 打印之前15天的日期
|
||||
print("之前15天的日期:", previous_date)
|
@ -1,74 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
# @author: 原凯峰
|
||||
# @contact: 2894340009@qq.com
|
||||
# @software: pycharm
|
||||
# @file: MachineLearningDivider.py
|
||||
# @time: 2024/6/26 8:21
|
||||
# @desc:利用随机森林法进行模型训练,能够通过平均响应时间、故障率等数据计算出服务器的健康状态
|
||||
|
||||
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
from sklearn.metrics import classification_report, accuracy_score
|
||||
import pickle
|
||||
# 假设我们有以下数据集
|
||||
X = [
|
||||
[0.3, 0.005], # 服务器特征:平均响应时间和故障率
|
||||
[2.5, 0.03],
|
||||
[0.7, 0.045],
|
||||
[1.2, 0.002]
|
||||
]
|
||||
y = ['良好', '差', '差', '良好'] # 对应的健康状态标签
|
||||
|
||||
# 将健康状态标签转换为数值
|
||||
label_mapping = {'一般': 0, '良好': 1, '差': 2, '极差': 3}
|
||||
y_encoded = [label_mapping[label] for label in y]
|
||||
|
||||
# 划分训练集和测试集
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.25, random_state=42)
|
||||
|
||||
# 选择模型,这里使用随机森林分类器
|
||||
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
||||
|
||||
# 训练模型
|
||||
model.fit(X_train, y_train)
|
||||
|
||||
# 预测测试集
|
||||
y_pred = model.predict(X_test)
|
||||
|
||||
# 评估模型
|
||||
print(classification_report(y_test, y_pred))
|
||||
print("Accuracy:", accuracy_score(y_test, y_pred))
|
||||
|
||||
with open('server_health_model.pkl', 'wb') as file:
|
||||
pickle.dump(model, file)
|
||||
|
||||
|
||||
# 保存模型
|
||||
with open('server_health_model.pkl', 'wb') as file:
|
||||
pickle.dump(model, file)
|
||||
|
||||
# 定义一个函数来加载模型并进行预测
|
||||
def load_model_and_predict(new_data):
|
||||
with open('server_health_model.pkl', 'rb') as file:
|
||||
loaded_model = pickle.load(file)
|
||||
predictions = loaded_model.predict(new_data)
|
||||
return predictions
|
||||
|
||||
# 定义一个函数来将预测结果转换为健康等级
|
||||
def predict_health_status(new_data):
|
||||
predictions = load_model_and_predict(new_data)
|
||||
# 创建逆向映射字典
|
||||
inverse_label_mapping = {value: key for key, value in label_mapping.items()}
|
||||
# 使用逆向映射字典转换预测结果
|
||||
health_status = [inverse_label_mapping[pred] for pred in predictions]
|
||||
return health_status
|
||||
|
||||
# 测试函数
|
||||
def testcase():
|
||||
new_data = [[0.4, 0.01]] # 新的服务器数据
|
||||
health_status = predict_health_status(new_data)
|
||||
print("预测的健康状态:", health_status)
|
||||
|
||||
testcase()
|
Binary file not shown.
Binary file not shown.
@ -1,2 +0,0 @@
|
||||
dic = {'a':1,'b':2,'c':3}
|
||||
for i in dic.keys():
|
Loading…
Reference in new issue