2016-07-13 85 views
1

我正在使用this答案,以便在矩陣(ndarray)中找到大於給定極限f的相關係數,其形狀爲(29421,11001 )[即29,421行和11,001列]。使用相關係數(Pearson)降維[Python]

我已經適應代碼,如下所示(隨機位選擇兩個列中的一個,以除去;此外,對應於該連接的答案的行具有後他們「###」):

問題:我得到的數千個相關係數大於1 ......從我的理解來看,這不應該發生?

rand = random() 
    rows = dataset_normalized.shape[0] ### 
    print("Rows: " + str(dataset_normalized.shape[0]) + ", Columns: " + str(dataset_normalized.shape[1])) 
    ms = dataset_normalized.mean(axis=1)[(slice(None, None, None), None)] ### 
    datam = dataset_normalized - ms ### 
    datass = np.sqrt(scipy.stats.ss(datam, axis=1)) ### 
    correlations = {} 
    percent_rand_one = 0 
    percent_rand_zero = 0 
    for i in range(rows): ### 
    if(0 in datass[i:] or datass[i] == 0): 
     continue 
    else: 
     temp = np.dot(datam[i:], datam[i].T) ### 
     rs = temp/(datass[i:] * datass[i]) ### 
     for counter, corr in enumerate(rs): 
     if(corr > 1 or corr < -1): 
      # ERROR IS HERE: This is printing right now, 
      # a lot, so I'm not sure what's happening? 
      print("Correlation of " + str(corr) + " on " + str(i) + " and " + str(counter) + ".") 
      print("Something went wrong. Correlations calculated were either above 1 or below -1.") 
     elif(corr > f or corr < f): 
      rand_int = randint(1, 100) 
      if(rand_int > 50): 
      correlations[counter] = corr 
      percent_rand_one += 1 
      else: 
      correlations[i] = corr   
      percent_rand_zero += 1 

任何建議或想法?

回答

0

想出來......這是最奇怪的事情。我只需要翻轉軸。

# Create correlations. 
    dataset_normalized_switched = np.swapaxes(dataset_normalized, 0, 1) 
    columns = dataset_normalized_switched.shape[0] ### This is the major change... 
    ms = dataset_normalized_switched.mean(axis=1)[(slice(None, None, None), None)] 
    datam = dataset_normalized_switched - ms 
    datass = np.sqrt(scipy.stats.ss(datam, axis=1)) 
    correlations = {} 
    for i in range(columns): 
    temp = np.dot(datam[i:], datam[i].T) 
    with warnings.catch_warnings(): 
     warnings.filterwarnings('ignore') 
     rs = temp/(datass[i:] * datass[i]) 
     correlations[i] = [(index + i) for index, value in enumerate(rs) if (index != 0 and abs(value) < 1.1 and abs(value) > f)]