2016-07-13 7 views
1

모양 (29421, 11001) 인 매트릭스 (ndarray)에서 주어진 제한 f보다 큰 상관 계수를 찾으려면 this 답변을 사용하고 있습니다.) [즉 29,421 행 및 11,001 열]. 다음 I 코드를 적응 한치수 감소를위한 상관 계수 (Pearson) 사용

는 (랜덤 비트를 제거하는 두 개의 열 중 하나를 선택하는 부가 적으로, 링크 된 대답에 대응하는 행이 그 후 "###"가)

문제 : 나는 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)]