2016-09-29 72 views
0

我正在創建一個程序來計算我的客戶數據之間的相關性。我想將相關值打印到CSV,以便我可以進一步分析數據。Numpy Savetxt覆蓋,找不到放置迴路的位置

我已經成功地讓我的程序循環遍歷所有客戶(每個數據12個月),同時計算他們的多個安排的個人關聯。如果我打印到對話框中,我可以看到這一點。

但是,當我嘗試使用Savetxt保存時,我只獲得了我計算的最終值。

我想我把我的循環放在錯誤的地方,它應該去哪裏?我試過檢查其他問題,但並沒有給它提供太多的信息。

編輯:我已經試圖對齊寫作與外循環和內循環建議,都產生了相同的結果。

for x_customer in range(0,len(overalldata),12): 

     for x in range(0,13,1): 
       cust_months = overalldata[0:x,1] 
       cust_balancenormal = overalldata[0:x,16] 
       cust_demo_one = overalldata[0:x,2] 
       cust_demo_two = overalldata[0:x,3] 
       num_acct_A = overalldata[0:x,4] 
       num_acct_B = overalldata[0:x,5] 
    #Correlation Calculations 
       demo_one_corr_balance = numpy.corrcoef(cust_balancenormal, cust_demo_one)[1,0] 
       demo_two_corr_balance = numpy.corrcoef(cust_balancenormal, cust_demo_two)[1,0] 
       demo_one_corr_acct_a = numpy.corrcoef(num_acct_A, cust_demo_one)[1,0] 
       demo_one_corr_acct_b = numpy.corrcoef(num_acct_B, cust_demo_one)[1,0] 
       demo_two_corr_acct_a = numpy.corrcoef(num_acct_A, cust_demo_two)[1,0] 
       demo_two_corr_acct_b = numpy.corrcoef(num_acct_B, cust_demo_two)[1,0] 

       result_correlation = [(demo_one_corr_balance),(demo_two_corr_balance),(demo_one_corr_acct_a),(demo_one_corr_acct_b),(demo_two_corr_acct_a),(demo_two_corr_acct_b)] 

     result_correlation_combined = emptylist.append([result_correlation]) 
     cust_delete_list = [0,(x_customer),1] 
     overalldata = numpy.delete(overalldata, (cust_delete_list), axis=0) 

numpy.savetxt('correlationoutput.csv', numpy.column_stack(result_correlation), delimiter=',') 
print result_correlation 
+0

你的假設聽起來是對的。你應該unindent最後一個'savetxt'行 –

+0

這是拋出一個「意外縮進錯誤」 –

+0

沒有縮進應該這樣做(與外部'for'對齊) –

回答

1

的這部分代碼只是馬虎:

   result_correlation = [(demo_one_corr_balance),...] 

     result_correlation_combined = emptylist.append([result_correlation]) 
     cust_delete_list = [0,(x_customer),1] 
     overalldata = numpy.delete(overalldata, (cust_delete_list), axis=0) 

numpy.savetxt('correlationoutput.csv', numpy.column_stack(result_correlation), delimiter=',') 
print result_correlation 

您在最內層循環設置result_correlation,然後你用它在最後保存和打印。顯然它會打印最後一個循環的結果。

同時,您將其附加到result_correlation_combined之外,x循環之外,趨近於x_customer循環。但是你對這個清單沒有做任何事情。

最後在x_customer循環中您使用overalldata玩,但我沒有看到任何進一步的用途。

暫時忘掉savetxt,直接獲取數據集。

+0

你好,謝謝。我贊成你,一旦我的聲望更高,它就會顯示出來。 –

0

我接受了上述海報的建議並更正了我的代碼。我現在可以寫入一個文件。但是,我在完成迭代的次數方面遇到了問題,因此我會在不同的問題中發佈它,因爲它是不相關的。這是我使用的解決方案。

for x_customer in range(0,len(overalldata),12): 

     for x in range(0,13,1): 
       cust_months = overalldata[0:x,1] 

       cust_balancenormal = overalldata[0:x,16] 

       cust_demo_one = overalldata[0:x,2] 
       cust_demo_two = overalldata[0:x,3] 

       num_acct_A = overalldata[0:x,4] 
       num_acct_B = overalldata[0:x,5] 

       out_mark_channel_one = overalldata[0:x,25] 
       out_service_channel_two = overalldata[0:x,26] 
       out_mark_channel_three = overalldata[0:x,27] 
       out_mark_channel_four = overalldata[0:x,28] 


    #Correlation Calculations 

       #Demographic to Balance Correlations 
       demo_one_corr_balance = numpy.corrcoef(cust_balancenormal, cust_demo_one)[1,0] 
       demo_two_corr_balance = numpy.corrcoef(cust_balancenormal, cust_demo_two)[1,0] 


       #Demographic to Account Number Correlations 
       demo_one_corr_acct_a = numpy.corrcoef(num_acct_A, cust_demo_one)[1,0] 
       demo_one_corr_acct_b = numpy.corrcoef(num_acct_B, cust_demo_one)[1,0] 
       demo_two_corr_acct_a = numpy.corrcoef(num_acct_A, cust_demo_two)[1,0] 
       demo_two_corr_acct_b = numpy.corrcoef(num_acct_B, cust_demo_two)[1,0] 

       #Marketing Response Channel One 
       mark_one_corr_acct_a = numpy.corrcoef(num_acct_A, out_mark_channel_one)[1, 0] 
       mark_one_corr_acct_b = numpy.corrcoef(num_acct_B, out_mark_channel_one)[1, 0] 
       mark_one_corr_balance = numpy.corrcoef(cust_balancenormal, out_mark_channel_one)[1, 0] 

       #Marketing Response Channel Two 
       mark_two_corr_acct_a = numpy.corrcoef(num_acct_A, out_service_channel_two)[1, 0] 
       mark_two_corr_acct_b = numpy.corrcoef(num_acct_B, out_service_channel_two)[1, 0] 
       mark_two_corr_balance = numpy.corrcoef(cust_balancenormal, out_service_channel_two)[1, 0] 

       #Marketing Response Channel Three 
       mark_three_corr_acct_a = numpy.corrcoef(num_acct_A, out_mark_channel_three)[1, 0] 
       mark_three_corr_acct_b = numpy.corrcoef(num_acct_B, out_mark_channel_three)[1, 0] 
       mark_three_corr_balance = numpy.corrcoef(cust_balancenormal, out_mark_channel_three)[1, 0] 

       #Marketing Response Channel Four 
       mark_four_corr_acct_a = numpy.corrcoef(num_acct_A, out_mark_channel_four)[1, 0] 
       mark_four_corr_acct_b = numpy.corrcoef(num_acct_B, out_mark_channel_four)[1, 0] 
       mark_four_corr_balance = numpy.corrcoef(cust_balancenormal, out_mark_channel_four)[1, 0] 


       #Result Correlations For Exporting to CSV of all Correlations 
       result_correlation = [(demo_one_corr_balance),(demo_two_corr_balance),(demo_one_corr_acct_a),(demo_one_corr_acct_b),(demo_two_corr_acct_a),(demo_two_corr_acct_b),(mark_one_corr_acct_a),(mark_one_corr_acct_b),(mark_one_corr_balance), 
             (mark_two_corr_acct_a),(mark_two_corr_acct_b),(mark_two_corr_balance),(mark_three_corr_acct_a),(mark_three_corr_acct_b),(mark_three_corr_balance),(mark_four_corr_acct_a),(mark_four_corr_acct_b), 
             (mark_four_corr_balance)] 
       result_correlation_nan_nuetralized = numpy.nan_to_num(result_correlation) 
       c.writerow(result_correlation) 

     result_correlation_combined = emptylist.append([result_correlation]) 
     cust_delete_list = [0,x_customer,1] 
     overalldata = numpy.delete(overalldata, (cust_delete_list), axis=0)