0

So I have 10 txt files named A_1,A_2,......A_10 and a working txt file named A. In each column of these txt files, there are 4320 values. My goal is to only compare the first 1440 values of any column of the A txt file with the other 10 txt files(A_1,A_2,.....A_10) and find the sum of square of differences. My approach is this but it gives me the difference of all the 4320 values, i am stuck at how to manipluate the code to find the difference of only the first 1440 values:

import numpy as np
filelist=[]
for i in range(1,11):
    filelist.append("/Users/Hrihaan/Desktop/A_%s.txt" %i)
for fname in filelist:
    data=np.loadtxt(fname)
    data1=np.loadtxt('/Users/Hrihaan/Desktop/A.txt')
    x=data[:,1]
    x1=data1[:,1]
    x2=(x-x1)**2
    x3=sum(x2)
    print(fname)
    print(x3)
Hrihaan
  • 275
  • 5
  • 21

1 Answers1

1

Adding the slice below should do the trick.

np.loadtxt(fname)[:1440]

It causes data to only include the rows indexed 0 up to but not including 1440... since Python is zero-based indexing, that gives you 1440 rows total.

for fname in filelist:
    data=np.loadtxt(fname)[:1440]
    data1=np.loadtxt('/Users/Hrihaan/Desktop/A.txt')
    x=data[:,1]
    x1=data1[:,1]
    x2=(x-x1)**2
    x3=sum(x2)
    print(fname)
    print(x3)
E. Ducateme
  • 4,028
  • 2
  • 20
  • 30
  • It worked, tried doing it the last 24 hours in such complicated ways, didn't knew a simple trick would do, thanks a ton. – Hrihaan Jul 28 '17 at 04:19
  • So, the way my code is set up in the last two commands, it prints out the filename(A_1,A_2.....A_10) and the sum of square of differences, I want to know the smallest 3 differences and their corresponding files, in my code seeing manually the answers I can do that but when I will be working with many files that will be not viable. Any suggestion how can I print out the smallest 3 sums and their corresponding txt file name? – Hrihaan Jul 28 '17 at 05:51