【程式媛曬83行程式碼】雲棲社群聚能聊專家,背後83行程式碼的故事

馬銘芳發表於2018-07-23

在中國程式媛中,他們的程式碼有什麼樣的魅力,Aone聯合雲棲社群、餓了麼、釘釘、阿里雲、天貓、口碑發起首屆程式媛比碼活動——不秀大長腿,秀高智商;不秀美圖照,秀程式碼圖,參與曬碼互動遊戲贏“83行程式碼”T恤!

我們來說說這群女工程師的第83行程式碼及程式碼背後的故事:

平時習慣於用Jupyter Notebook寫程式碼,以至於很多程式碼像是這樣:

# 分塊計算缺失程度(23塊)
block_list = [1,5,6,20,24,28,32,36,48,52,54,64,72,76,102,107,111,155,161,166,211,254,278,298]

for i in tqdm(range(len(block_list)-1)):
    tmp_df = train_test.iloc[:,block_list[i]-1:block_list[i+1]-1]
    print(tmp_df.columns)
    tmp_df_T = tmp_df.T
    tmp_df[`count`] = tmp_df_T.count()
    train_test[`count_f` + str(block_list[i]) + `_f` +str(block_list[i+1])] = tmp_df[`count`]
    train_test[`count_label_f` + str(block_list[i]) + `_f` +str(block_list[i+1]-1)]=np.where(train_test[`count_f` + str(block_list[i]) + `_f` +str(block_list[i+1])] > 0,0,1)

# 計算佔比(基於date)
for i in tqdm(r_list):
    df_dratio = pd.DataFrame()
    for j in date_list:
        df_tmp = train_test[train_test[`date`] == j]
        f_ratio = pd.DataFrame(df_tmp[i].value_counts())
        f_ratio[`date`] = j
        f_ratio[i + `_rcount`] = f_ratio[i]
        f_ratio[i + `_ratio`] = f_ratio[i]/len(df_tmp)
        f_ratio[i] = f_ratio.index
        f_ratio = f_ratio.reset_index(drop = True)
        df_dratio = pd.concat([df_dratio, f_ratio], axis = 0)
        df_dratio = df_dratio.reset_index(drop = True)
    train_test = train_test.merge(df_dratio,on = [`date`,i],how = `left`)

比較喜歡的一段程式碼是這樣:

import copy
def cal_all_altcol_1(col_, labelcol = None, error_corr = False):  
    
#     if labelcol is not None:
#         print("pcsing by groups")
        
    col = copy.deepcopy(col_)
    colname = col.columns[0]

    if(error_corr == True):
        if labelcol is not None:
            mean = pd.DataFrame(calmean(col, labelcol))
            alt_col = [col, 1 / col, col-mean, np.fabs(col-mean)]
            alt_col_name = [colname + "_X", colname + "_1/X", colname + "_Xerr", colname + "_Xerr_abs"]
        else:
            mean = calmean(col, labelcol)
            alt_col = [col, 1 / col, np.fabs(col-mean)]
            alt_col_name = [colname + "_X", colname + "_1/X", colname + "_Xerr_abs"]
    elif(error_corr == False):
        alt_col = [col , 1 / col]
        alt_col_name = [colname + "_X", colname + "_1/X"]
    
    alt_list = pd.concat(alt_col, axis = 1)
    alt_list.columns = alt_col_name
    return alt_list

def cal_all_altcol_1_base(col_, labelcol = None, error_corr = False):  
    # 只取正負列
    
#     if labelcol is not None:
#         print("pcsing by groups")
        
    col = copy.deepcopy(col_)
    colname = col.columns[0]
    
    if(error_corr == True):
        if labelcol is not None:
            mean = pd.DataFrame(calmean(col, labelcol))
            alt_col = [col, np.fabs(col-mean), col-mean, ]
            alt_col_name = [colname + "_X", colname + "_Xerr_abs", colname + "_Xerr"]
        else:
            mean = pd.DataFrame(calmean(col, labelcol))
            print(mean)
            alt_col = [col, np.fabs(col-mean)]
            alt_col_name = [colname + "_X", colname + "_Xerr_abs"]
    elif(error_corr == False):
        alt_col = [col]
        alt_col_name = [colname + "_X"]
    
    alt_list = pd.concat(alt_col, axis = 1)
#     print (alt_list)
    alt_list.columns = alt_col_name
    return alt_list


def cal_all_altcol_2(col_i, col_j, labelcol = None, error_corr = False):
    
#     if labelcol is not None:
#         print("pcsing by groups")
    coli = copy.deepcopy(col_i)
    colj = copy.deepcopy(col_j)
    coliname = coli.columns[0]
    coljname = colj.columns[0]
    colbet = coliname + "_" + coljname
    coli.columns, colj.columns = [colbet], [colbet] # 需要把列名統一不然不能直接減
    
    Plus = coli + colj
    Minus = coli - colj
    Multiply = coli * colj
    Divide_1 = coli / colj
    Divide_2 = colj / coli
    everynum_perpart = 5
    
    if(error_corr == True):
        P_mean = pd.DataFrame(calmean(Plus, labelcol))
        Mi_mean = pd.DataFrame(calmean(Minus, labelcol))
        Mu_mean = pd.DataFrame(calmean(Multiply, labelcol))
        D1_mean = pd.DataFrame(calmean(Divide_1, labelcol))
        D2_mean = pd.DataFrame(calmean(Divide_2, labelcol))
        if labelcol is not None:
            alt_col =  [Plus, Minus, Multiply, Divide_1, Divide_2,
                        np.fabs(Plus - P_mean), np.fabs(Minus - Mi_mean), np.fabs(Multiply - Mu_mean),
                        np.fabs(Divide_1 - D1_mean), np.fabs(Divide_2 - D2_mean),
                        Plus - P_mean, Minus - Mi_mean, Multiply - Mu_mean,Divide_1 - D1_mean, Divide_2 - D2_mean]
            alt_col_name = [colbet + "_X+Y", colbet + "_X-Y", colbet + "_X*Y", colbet + "_X/Y", colbet + "_Y/X",
                            colbet + "_X+Yerr_abs", colbet + "_X-Yerr_abs", colbet + "_X*Yerr_abs",
                            colbet + "_X/Yerr_abs", colbet + "_Y/Xerr_abs",
                            colbet + "_X+Yerr", colbet + "_X-Yerr", colbet + "_X*Yerr",
                            colbet + "_X/Yerr", colbet + "_Y/Xerr",]
        elif labelcol is None:
            alt_col = [Plus, Minus, Multiply, Divide_1, Divide_2, 
                       np.fabs(Plus - P_mean), np.fabs(Minus - Mi_mean), np.fabs(Multiply - Mu_mean),
                       np.fabs(Divide_1 - D1_mean), np.fabs(Divide_2 - D2_mean)]
            alt_col_name = [colbet + "_X+Y", colbet + "_X-Y", colbet + "_X*Y", colbet + "_X/Y", colbet + "_Y/X",
                            colbet + "_X+Yerr_abs", colbet + "_X-Yerr_abs", colbet + "_X*Yerr_abs",
                            colbet + "_X/Yerr_abs", colbet + "_Y/Xerr_abs"]
    elif(error_corr == False):
        alt_col = [Plus, Minus, Multiply, Divide_1, Divide_2]
        alt_col_name = [colbet + "_X+Y", colbet + "_X-Y", colbet + "_X*Y", colbet + "_X/Y", colbet + "_Y/X"]
    
    alt_list = pd.concat(alt_col, axis = 1)
    alt_list.columns = alt_col_name
#     print(alt_list)
    return alt_list
# def cal_all_alt2tol(coli, colj, )
# cal_all_altcol_2(test, test2)

與海洋的雲小姐姐互動,為她打call——>點選進去曬碼


更多小姐姐,點選進入檢視程式碼

有被程式碼耽誤的釘釘吃貨程式媛,寫程式碼寫到忘記吃飯的——採霜
她急需能把她從程式碼中叫醒去吃飯的小夥伴,趕緊勾搭;
http://yq.aliyun.com/roundtable/126499/answer/170319#visit170319

有以程式碼為樂的餓了麼大前端打(bei)雜(guo)工程師——敖天羽
http://yq.aliyun.com/roundtable/126499/answer/170299#visit170299

還有全棧美女工程師——前端後端一鍋端的——墨瑜女神
http://yq.aliyun.com/roundtable/126499/answer/170316#visit170316

還有阿里雲活好碼贊,像男人一樣活著的技術專家小姐姐——清宵http://yq.aliyun.com/roundtable/126499/answer/170296#visit170296

和天貓Java程式媛,happy(被)來(逼)的——採月
http://yq.aliyun.com/roundtable/126499/answer/170354#visit170354

阿里口碑 搜尋推薦組的JAVA女工程師——妍霏
http://yq.aliyun.com/roundtable/126499/answer/214059#visit214059

阿里口碑基礎平臺技術部的工程師-銜遠,一個技術與馬甲線並存的mm
http://yq.aliyun.com/roundtable/126499/answer/248798#visit248798

雲棲社群聚能聊專家,具有知性溫婉氣質的——海洋的雲http://yq.aliyun.com/roundtable/126499/answer/170290#visit170290

福州市五佰網路科技有限公司的Java女工程師——江小白太白
http://yq.aliyun.com/roundtable/126499/answer/189332#visit189332

在下*的程式媛——cn_suqingnian
http://yq.aliyun.com/roundtable/126499/answer/170309#visit170309

交通銀行的前端女工程師——昆0830
http://yq.aliyun.com/roundtable/126499/answer/201979#visit201979


相關文章