第十二届“中国软件杯”大赛:A10-基于机器学习的分布式系统故障诊断系统——baseline(一)

简介: 第十二届“中国软件杯”大赛:A10-基于机器学习的分布式系统故障诊断系统——baseline(一)

赛题介绍

在分布式系统中某个节点发生故障时,故障会沿着分布式系统的拓扑结构进行传播,造成自身节点及其邻接节点相关的KPI指标和发生大量日志异常。本次比赛提供分布式数据库的故障特征数据和标签数据,其中特征数据是系统发生故障时的KPI指标数据,KPI指标包括由feature0、feature1 …feature106共107个指标,标签数据为故障类别数据,共6个类别,用0、1、2、3、4、5分别表示6个故障,参赛人员可根据这些数据,借助机器学习、深度学习、web等技术搭建故障诊断系统,该系统支持用户上传训练集对模型进行训练和模型下载,同时支持用户上传单条或多条测试语句进行测试并可视化测试结果,支持测试结果下载。

baseline: DecisionTree

数据分析

读取数据

df = pd.read_csv('data/train/train.csv', index_col=None)

判断是否有缺失值

df.isnull().any()
'''
output: True即为存在缺失值
sample_id     False
feature0       True
feature1       True
feature2       True
feature3       True
              ...  
feature103     True
feature104     True
feature105    False
feature106     True
label         False
Length: 109, dtype: bool
'''

数据标准化及缺失值填充

# 数据标准化
features = df.iloc[:, 1:-1]
numeric_features = features.dtypes[features.dtypes != 'object'].index
features[numeric_features] = features[numeric_features].apply(
    lambda x: (x - x.mean()) / (x.std())
)
# 在标准化数据之后,所有均值消失,因此我们可以将缺失值设置为0
features[numeric_features] = features[numeric_features].fillna(0)
features_labels = pd.concat([features, df[['label']]], axis=1)
train_features = pd.concat([df[['sample_id']], features], axis=1)
train_label = df[['sample_id', 'label']]
df = pd.concat([train_features, train_label[['label']]], axis=1)

观察数据基本信息

1. # 观察前五行数据
2. df.head()


1b847b97c2e140b68effe0386b6d001f.png

# 数据大小
df.shape
'''
output:
(6296, 109)
'''
df.dtypes
'''
output:
sample_id       int64
feature0      float64
feature1      float64
feature2      float64
feature3      float64
               ...   
feature103    float64
feature104    float64
feature105    float64
feature106    float64
label           int64
Length: 109, dtype: object
'''
# 类别分布
df['label'].value_counts().sort_index().plot(kind='bar')
plt.show()
df['label'].value_counts().sort_index().plot(kind='pie')
plt.show()

8a7b850e12b149a0b45906a52c7482b5.png

dca5c306e70546b0ba7368dd5865a002.png

features.describe()


179578a1a4aa4e4c8b47443ca9266516.png

# 分组的平均数据统计
label_Summary = features_labels.groupby('label')
label_Summary.mean()

b1062eeab85347b281aee6d1c2f439ca.png

# 相关性矩阵
corr = features_labels.corr()
sns.set_context({'figure.figsize':[100, 100]})
fig = sns.heatmap(corr, 
                xticklabels=corr.columns.values, 
                yticklabels=corr.columns.values)
heatmap = fig.get_figure()
heatmap.savefig('work/heatmap.png', dpi=300)
corr

5bd426d717d64ddb818c0fc3fb2c8d97.png

# 各个特征的概率密度函数
feature_names = features.columns.values.tolist()
for name in feature_names:
    fig = plt.figure(figsize=(15, 4), )
    ax = sns.kdeplot(df.loc[(df['label'] == 0), name], color='b', shade=True, label='0')
    ax = sns.kdeplot(df.loc[(df['label'] == 1), name], color='r', shade=True, label='1')
    ax = sns.kdeplot(df.loc[(df['label'] == 2), name], color='g', shade=True, label='2')
    ax = sns.kdeplot(df.loc[(df['label'] == 3), name], color='y', shade=True, label='3')
    ax = sns.kdeplot(df.loc[(df['label'] == 4), name], color='m', shade=True, label='4')
    ax = sns.kdeplot(df.loc[(df['label'] == 5), name], color='c', shade=True, label='5')
    ax.set(xlabel=name, ylabel='频率')
    plt.title('{} Probabilitydensity function'.format(name))
    plt.savefig('work/{}的概率密度函数图.png'.format(name))

cf32e6b85e91459583e3423ccf450494.png

划分数据集

from sklearn.model_selection import train_test_split
target_name = 'label'
x = df.drop(['sample_id', 'label'], axis=1)
y = df[['label']]
x_train, x_test, y_train, y_test = train_test_split(
    x, y, test_size=0.15, random_state=123, stratify=y)

模型训练

from sklearn.tree import DecisionTreeClassifier
# 实例化
dtree = tree.DecisionTreeClassifier(
    criterion='entropy',
    min_weight_fraction_leaf=0.01
)
# train
dtree = dtree.fit(x_train, y_train)

评价指标计算

# 指标计算 参数:array
def metrics_calculate(pred, y_test, txt_path):
    TP = [0, 0, 0, 0, 0, 0]
    FP = [0, 0, 0, 0, 0, 0]
    FN = [0, 0, 0, 0, 0, 0]
    for i in range(len(y_test)):
        if pred[i] == 0 and y_test[i] == 0:
            TP[0] += 1
        if pred[i] != 0 and y_test[i] == 0:
            FN[0] += 1
        if pred[i] == 0 and y_test[i] != 0:
            FP[0] += 1
        if pred[i] == 1 and y_test[i] == 1:
            TP[1] += 1
        if pred[i] != 1 and y_test[i] == 1:
            FN[1] += 1
        if pred[i] == 1 and y_test[i] != 1:
            FP[1] += 1
        if pred[i] == 2 and y_test[i] == 2:
            TP[2] += 1
        if pred[i] != 2 and y_test[i] == 2:
            FN[2] += 1
        if pred[i] == 2 and y_test[i] != 2:
            FP[2] += 1
        if pred[i] == 3 and y_test[i] == 3:
            TP[3] += 1
        if pred[i] != 3 and y_test[i] == 3:
            FN[3] += 1
        if pred[i] == 3 and y_test[i] != 3:
            FP[3] += 1
        if pred[i] == 4 and y_test[i] == 4:
            TP[4] += 1
        if pred[i] != 4 and y_test[i] == 4:
            FN[4] += 1
        if pred[i] == 4 and y_test[i] != 4:
            FP[4] += 1
        if pred[i] == 5 and y_test[i] == 5:
            TP[5] += 1
        if pred[i] != 5 and y_test[i] == 5:
            FN[5] += 1
        if pred[i] == 5 and y_test[i] != 5:
            FP[5] += 1
    Precision = [0, 0, 0, 0, 0, 0]
    Recall = [0, 0, 0, 0, 0, 0]
    F1 = [0, 0, 0, 0, 0, 0]
    Precision[0] = TP[0] / (TP[0] + FP[0])
    Precision[1] = TP[1] / (TP[1] + FP[1])
    Precision[2] = TP[2] / (TP[2] + FP[2])
    Precision[3] = TP[3] / (TP[3] + FP[3])
    Precision[4] = TP[4] / (TP[4] + FP[4])
    Precision[5] = TP[5] / (TP[5] + FP[5])
    for i in range(6):
        print('Precision: {}\n'.format(Precision[i]))
    Recall[0] = TP[0] / (TP[0] + FN[0])
    Recall[1] = TP[1] / (TP[1] + FN[1])
    Recall[2] = TP[2] / (TP[2] + FN[2])
    Recall[3] = TP[3] / (TP[3] + FN[3])
    Recall[4] = TP[4] / (TP[4] + FN[4])
    Recall[5] = TP[5] / (TP[5] + FN[5])
    for i in range(6):
        print('Recall: {}\n'.format(Recall[i]))
    F1[0] = (2 * Precision[0] * Recall[0]) / (Precision[0] + Recall[0])
    F1[1] = (2 * Precision[1] * Recall[1]) / (Precision[1] + Recall[1])
    F1[2] = (2 * Precision[2] * Recall[2]) / (Precision[2] + Recall[2])
    F1[3] = (2 * Precision[3] * Recall[3]) / (Precision[3] + Recall[3])
    F1[4] = (2 * Precision[4] * Recall[4]) / (Precision[4] + Recall[4])
    F1[5] = (2 * Precision[5] * Recall[5]) / (Precision[5] + Recall[5])
    for i in range(6):
        print('F1: {}\n'.format(F1[i]))
    Macro_Precision = sum([Precision[0], Precision[1], Precision[2],
                            Precision[3], Precision[4], Precision[5]]) / 6
    Macro_Recall = sum([Recall[0], Recall[1], Recall[2],
                        Recall[3], Recall[4], Recall[5]]) / 6
    Macro_F1 = sum([F1[0], F1[1], F1[2], F1[3], F1[4], F1[5]]) / 6
    l_sum = sum([TP[0], TP[1], TP[2], TP[3], TP[4], TP[5]])
    m_sum = l_sum + sum([FP[0], FP[1], FP[2], FP[3], FP[4], FP[5]])
    n_sum = l_sum + sum([FN[0], FN[1], FN[2], FN[3], FN[4], FN[5]])
    Micro_Precision = l_sum / m_sum
    print('Micro_Precision: {}\n'.format(Micro_Precision))
    Micro_Recall = l_sum / n_sum
    print('Micro_Recall: {}\n'.format(Micro_Recall))
    Micro_F1 = (2 * Micro_Precision * Micro_Recall) / (Micro_Precision + Micro_Recall)
    print('Micro_F1: {}\n'.format(Micro_F1))
    f = open(txt_path, 'a', encoding='utf-8')
    for i in range(6):
        f.write('类别{}: '.format(i))
        f.write('\n')
        f.write('Precision: {:.2f}%'.format(Precision[i] * 100))
        f.write('\n')
        f.write('Recall: {:.2f}%'.format(Recall[i] * 100))
        f.write('\n')
        f.write('F1: {:.2f}'.format(F1[i]))
        f.write('\n')
    f.write('Macro_Precision: {:.2f}%'.format(Macro_Precision * 100))
    f.write('\n')
    f.write('Macro_Recall: {:.2f}%'.format(Macro_Recall * 100))
    f.write('\n')
    f.write('Macro_F1: {:.2f}'.format(Macro_F1))
    f.write('\n')
    f.write('Micro_Precision: {:.2f}%'.format(Micro_Precision * 100))
    f.write('\n')
    f.write('Micro_Recall: {:.2f}%'.format(Micro_Recall * 100))
    f.write('\n')
    f.write('Micro_F1: {:.2f}'.format(Micro_F1))
    f.write('\n')
    f.close()

验证模型

# 验证
pred = dtree.predict(x_test)
y_test = y_test.reshape((-1, ))
txt_path = 'work/result_RandomForest.txt'
metrics_calculate(pred, y_test, txt_path)
'''
Precision: 0.8382066276803118
Precision: 0.6823529411764706
Precision: 0.7553956834532374
Precision: 0.7368421052631579
Precision: 0.972972972972973
Precision: 0.8157894736842105
Recall: 0.8829568788501027
Recall: 0.5858585858585859
Recall: 0.6907894736842105
Recall: 0.8433734939759037
Recall: 0.6792452830188679
Recall: 0.8732394366197183
F1: 0.86
F1: 0.6304347826086957
F1: 0.7216494845360826
F1: 0.7865168539325843
F1: 0.7999999999999999
F1: 0.8435374149659864
Micro_Precision: 0.8052910052910053
Micro_Recall: 0.8052910052910053
Micro_F1: 0.8052910052910053
'''
相关文章
|
Kubernetes 大数据 调度
Airflow vs Argo Workflows:分布式任务调度系统的“华山论剑”
本文对比了Apache Airflow与Argo Workflows两大分布式任务调度系统。两者均支持复杂的DAG任务编排、社区支持及任务调度功能,且具备优秀的用户界面。Airflow以Python为核心语言,适合数据科学家使用,拥有丰富的Operator库和云服务集成能力;而Argo Workflows基于Kubernetes设计,支持YAML和Python双语定义工作流,具备轻量化、高性能并发调度的优势,并通过Kubernetes的RBAC机制实现多用户隔离。在大数据和AI场景中,Airflow擅长结合云厂商服务,Argo则更适配Kubernetes生态下的深度集成。
1323 34
|
9月前
|
存储 算法 安全
“卧槽,系统又崩了!”——别慌,这也许是你看过最通俗易懂的分布式入门
本文深入解析分布式系统核心机制:数据分片与冗余副本实现扩展与高可用,租约、多数派及Gossip协议保障一致性与容错。探讨节点故障、网络延迟等挑战,揭示CFT/BFT容错原理,剖析规模与性能关系,为构建可靠分布式系统提供理论支撑。
406 2
|
人工智能 自然语言处理 安全
通过阿里云Milvus与PAI搭建高效的检索增强对话系统
阿里云向量检索Milvus版是一款全托管的云服务,兼容开源Milvus并支持无缝迁移。它提供大规模AI向量数据的相似性检索服务,具备易用性、可用性、安全性和低成本等优势,适用于多模态搜索、检索增强生成(RAG)、搜索推荐、内容风险识别等场景。用户可通过PAI平台部署RAG系统,创建和配置Milvus实例,并利用Attu工具进行可视化操作,快速开发和部署应用。使用前需确保Milvus实例和PAI在相同地域,并完成相关配置与开通服务。
|
9月前
|
机器学习/深度学习 算法 安全
新型电力系统下多分布式电源接入配电网承载力评估方法研究(Matlab代码实现)
新型电力系统下多分布式电源接入配电网承载力评估方法研究(Matlab代码实现)
289 3
|
12月前
|
机器学习/深度学习 存储 运维
机器学习异常检测实战:用Isolation Forest快速构建无标签异常检测系统
本研究通过实验演示了异常标记如何逐步完善异常检测方案和主要分类模型在欺诈检测中的应用。实验结果表明,Isolation Forest作为一个强大的异常检测模型,无需显式建模正常模式即可有效工作,在处理未见风险事件方面具有显著优势。
980 46
|
11月前
|
数据采集 缓存 NoSQL
分布式新闻数据采集系统的同步效率优化实战
本文介绍了一个针对高频新闻站点的分布式爬虫系统优化方案。通过引入异步任务机制、本地缓存池、Redis pipeline 批量写入及身份池策略,系统采集效率提升近两倍,数据同步延迟显著降低,实现了分钟级热点追踪能力,为实时舆情监控与分析提供了高效、稳定的数据支持。
483 1
分布式新闻数据采集系统的同步效率优化实战
|
存储 人工智能 自然语言处理
基于QwQ-32B+Hologres+PAI搭建 RAG 检索增强对话系统
本文介绍如何使用PAI-EAS部署基于QwQ大模型的RAG服务,并关联Hologres引擎实例。Hologres与达摩院自研高性能向量计算软件库Proxima深度整合,支持高性能、低延时、简单易用的向量计算能力。通过PAI-EAS,用户可以一键部署集成大语言模型(LLM)和检索增强生成(RAG)技术的对话系统服务,显著缩短部署时间并提升问答质量。具体步骤包括准备Hologres向量检索库、部署RAG服务、通过WebUI页面进行模型推理验证及API调用验证。Hologres支持高性能向量计算,适用于复杂任务的动态决策,帮助克服大模型在领域知识局限、信息更新滞后和误导性输出等方面的挑战。
|
人工智能 自然语言处理 API
Hologres × PAI × DeepSeek 搭建 RAG 检索增强对话系统
本文介绍如何使用PAI-EAS部署基于DeepSeek大模型的RAG(检索增强生成)服务,并关联Hologres引擎实例。Hologres与阿里云自研高性能向量计算软件库Proxima深度整合,支持高性能、低延时的向量计算能力。通过PAI-EAS,用户可以一键部署集成了大语言模型和RAG技术的对话系统服务,显著缩短部署时间,并提高问答质量。部署步骤包括准备Hologres向量检索库、部署基于DeepSeek的RAG服务、通过WebUI进行模型推理验证,以及通过API调用进行模型推理验证。Hologres还提供了特色功能支持,如高性能向量计算等。
|
存储 运维 安全
盘古分布式存储系统的稳定性实践
本文介绍了阿里云飞天盘古分布式存储系统的稳定性实践。盘古作为阿里云的核心组件,支撑了阿里巴巴集团的众多业务,确保数据高可靠性、系统高可用性和安全生产运维是其关键目标。文章详细探讨了数据不丢不错、系统高可用性的实现方法,以及通过故障演练、自动化发布和健康检查等手段保障生产安全。总结指出,稳定性是一项系统工程,需要持续迭代演进,盘古经过十年以上的线上锤炼,积累了丰富的实践经验。
1399 7
|
存储 分布式计算 Hadoop
基于Java的Hadoop文件处理系统:高效分布式数据解析与存储
本文介绍了如何借鉴Hadoop的设计思想,使用Java实现其核心功能MapReduce,解决海量数据处理问题。通过类比图书馆管理系统,详细解释了Hadoop的两大组件:HDFS(分布式文件系统)和MapReduce(分布式计算模型)。具体实现了单词统计任务,并扩展支持CSV和JSON格式的数据解析。为了提升性能,引入了Combiner减少中间数据传输,以及自定义Partitioner解决数据倾斜问题。最后总结了Hadoop在大数据处理中的重要性,鼓励Java开发者学习Hadoop以拓展技术边界。
573 7