Python趋势自动化描述最终(最新)

本文介绍了Python趋势自动化分析的更新,新代码增强了突变分析,包括R方和P值检验,以及利用0-1标准化差分进行波动描述。旧代码存在不确定性,新代码则去掉了定基比计算,并在线性趋势度描述中加入了致命点去除后的重新线性回归逻辑。更新还提供了多个示例,展示了不同趋势和突变点情况的分析结果。
摘要由CSDN通过智能技术生成

目录

1、旧代码
2、更新代码

说明:
1、旧代码描述部分内容较多,还有一些不确定性
2、新代码:增加了前几篇内容的突变分析代码,增加了线性的R方以及P值得检验分析,对不满足上述条件的,需要进行阶段性分析:突变点分析,整体波动性大小分析由之前的通过突变点个数来决定改为利用0-1标准化之后得差分进行描述,去掉了定基比的计算,线性趋势度描述部分增加了致命点去除之后的重新线性回归的逻辑(最终描述的结果中可以增加致命点描述,在这就没有增加)

1、旧代码

def trend_desc(inputdata,conf_level=0.95):
    overall_num = len(inputdata)
    # Sen's slope
    trend_result = sens_slope_trend_detection(inputdata,conf_level)
    # trend desc
    # 99% ——> +—2.576
    # 95% ——> +—1.96
    # 90% ——> +—1.645
    change_rate = trend_result[0]
    # fixed base ratio
    fixed_base_ratio_value = fixed_base_ratio(inputdata)
    # temp_result = []
    # overall trend
    linear_result = linear_trend_degree(inputdata)
    #
    overall_change_degree = temp_trend_desc(linear_result[0])
    if linear_result[0] < 0:
        overall_trend = '下降'
    elif linear_result[0] > 0:
        overall_trend = '上升'
    else:
        overall_trend = '无显著线性趋势'
    # begin
    if fixed_base_ratio_value * overall_change_degree[0] > 0:
        if np.abs(overall_change_degree[0]) > 10:
            trend_degree_desc = '大幅'
        else:
            trend_degree_desc = '小幅'
    else :
        judging = judging_fatal_point(inputdata)
        have_or_not = judging[0]
        if have_or_not:
            delete_fatal_point = judging[1]
            noFaltalPoint_linear_result = linear_trend_degree(delete_fatal_point)
            noFaltalPoint_fixed_base_ratio = fixed_base_ratio(delete_fatal_point)
            noFaltalPoint_overall_change_degree = temp_trend_desc(noFaltalPoint_linear_result[0])
            if noFaltalPoint_fixed_base_ratio * noFaltalPoint_overall_change_degree[0] > 0:
                if np.abs(noFaltalPoint_overall_change_degree[0]) > 10:
                    trend_degree_desc = '大幅'
                    if noFaltalPoint_linear_result[0] < 0:
                        overall_trend = '下降存在致命点'
                    elif noFaltalPoint_linear_result[0] > 0:
                        overall_trend = '上升存在致命点'
                    else:
                        overall_trend = '不明显趋势但存在致命点'
                else:
                    trend_degree_desc = '小幅'
            else:
                trend_degree_desc = '不明显'
        else:
            trend_degree_desc = '不明显'
    if linear_result[1] < 0.05:
        final_desc = {
   '整体趋势':[trend_degree_desc + overall_trend],
                      '整体定基比(%)':[fixed_base_ratio_value],
                      '整体变化角度':[overall_change_degree[0]],
                      '突变点个数':['-'],
                      '突变点位置':['-'],
                      'Mann-Kendall突变位置':['-'] ,
                      'Pettitt突变位置':['-'] ,
                      'Buishand_U突变位置':['-'] ,
                      'SNHT突变位置':['-'] ,
                      '详情':['-']}
    else:
        # overall trend desc (wave)
        # the same of overall fixed base ratio
        # four change point methods
        Kendall_change_point_result    = Kendall_change_point_detection(inputdata)
        Pettitt_change_point_result    = Pettitt_change_point_detection(inputdata)
        Buishand_U_change_point_result = Buishand_U_change_point_detection(inputdata)
        SNHT_change_point_result       = SNHT_change_point_detection(inputdata)
        # Logical determination of change point
        temp_result = Kendall_change_point_result + [Pettitt_change_point_result, Buishand_U_change_point_result, SNHT_change_point_result]
        # Calculate the trend degree of each segment.
        # desc begin
        if Kendall_change_point_result==[]:
            overall_wave = '小波动'
            final_desc = {
   '整体趋势':[overall_wave + trend_degree_desc + overall_trend],
                          '整体定基比(%)':[fixed_base_ratio_value],
                          '整体变化角度':[overall_change_degree[0]],
                          '突变点个数':['-'],
                          '突变点位置':['-'],
                          'Mann-Kendall突变位置':['-'] ,
                          'Pettitt突变位置':['-'] ,
                          'Buishand_U突变位置':['-'] ,
                          'SNHT突变位置':['-'] ,
                          '详情':['-']}
        else:
            #temp_result = Series(temp_result).value_counts()
            #change_point_mode = list((temp_result.loc[temp_result==max(temp_result)]).index)
            # change_point_list = temp_result.drop_duplicates( keep='first', inplace=False)
            # change_point_num = len(change_point_list)
            temp_change_point_listT = np.array(temp_result)
            temp_change_point_list = list(temp_change_point_listT[temp_change_point_listT > 2])
            temp_change_point_list = list(set(temp_change_point_list))
            temp_change_point_list.sort()
            delete_aj = []
            for i in range(len(temp_change_point_list) - 1):
                for j in range(i+1,len(temp_change_point_list)):
                    temp_ij = np.abs(temp_change_point_list[j]-temp_change_point_list[i])
                    if temp_ij <=5:
                        delete_aj.append(temp_change_point_list[j])
                temp_change_point_list = [x for x in temp_change_point_list if x not in delete_aj]
            change_point_list = temp_change_point_list
            change_point_num = len(change_point_list)
            if change_point_num <=2 and len(temp_change_point_list) <= 3:
                if fixed_base_ratio_value * overall_change_degree[0] < 0 :
                    overall_wave = '大波动'
                else:
                    overall_wave = '小波动'
            else:
                overall_wave = '大波动'
            if change_point_num <= 2:
                #overall_wave = '小波动'
                if change_point_num == 1:
                    first_trend_desc01  = linear_trend_degree(inputdata[:change_point_list[0]-1],conf_level)
                    first_trend_desc1 = temp_trend_desc(first_trend_desc01[0])
                    second_trend_desc02  = linear_trend_degree(inputdata[change_point_list[0]:],conf_level)
                    second_trend_desc2 = temp_trend_desc(second_trend_desc02[0])
                    second_num = overall_num - change_point_list[0]
                    if (change_point_list[0]-1) <= 3:
                        first_fixed_base_ratio  = '数据量太少'
                    else:
                        first_fixed_base_ratio  = fixed_base_ratio(inputdata[:change_point_list[0]-1])
                    first_change_degree = first_trend_desc1[0]
                    first_change_trend = first_trend_desc1[1]
                    if second_num <= 3:
                        second_fixed_base_ratio  = '数据量太少'
                    else:
                        second_fixed_base_ratio  = fixed_base_ratio(inputdata[change_point_list
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值