下面是我收到Broken Pipe错误的代码。我没有收到小数据集的这个错误。仅当数据集很大时才会出现这种情况。我也无法通过例外处理它。
## reading the data from CSV file
import csv
csv_file='Two_Patterns_TRAIN.csv'
data=[]
with open (csv_file,'rb') as csv_file:
csvreader=csv.reader(csv_file, delimiter=';',quotechar='|')
## fetch the rows one by one
for row in csvreader:
## split the rows in columns
columns=row[0].split(',')
myrow=[]
for col in range(len(columns)):
## fetch the columns one by one and append it to the according row
myrow.append(float(columns[col]))
data.append(myrow)
def dtw(seqA, seqB, d = lambda x,y: abs(x-y)):
# create the cost matrix
numRows, numCols = len(seqA), len(seqB)
cost = [[0 for _ in range(numCols)] for _ in range(numRows)]
# initialize the first row and column
cost[0][0] = d(seqA[0], seqB[0])
for i in xrange(1, numRows):
cost[i][0] = cost[i-1][0] + d(seqA[i], seqB[0])
for j in xrange(1, numCols):
cost[0][j] = cost[0][j-1] + d(seqA[0], seqB[j])
# fill in the rest of the matrix
for i in xrange(1, numRows):
for j in xrange(1, numCols):
choices = cost[i-1][j], cost[i][j-1], cost[i-1][j-1]
cost[i][j] = min(choices) + d(seqA[i], seqB[j])
return cost[-1][-1]
def knn(mat,k):
## fetch number of rows and columns of the matrix
nrow=len(mat)
if nrow<k:
print "K can not be larger than n-1"
return
neigh=[[0]*k for i in range(nrow)]
for i in range(nrow):
dist=[[0]*2 for count in range(nrow)]
for j in range(nrow):
dist[j][0]=j+1
dist[j][1]=dtw(mat[i],mat[j])
dist = sorted(dist, key=lambda a_entry: a_entry[1])
neigh[i]=[row[0] for row in dist][1:k+1]
# print neigh
return neigh
a=data[:500]
b=data[501:]
try:
c=knn(a,5)
except IOError, e:
print e
以下是我得到的错误
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/monitor.py", line 588, in run
already_pickled=True)
File "/usr/lib/python2.7/dist-packages/spyderlib/utils/bsdsocket.py", line 24, in write_packet
sock.send(struct.pack("l", len(sent_data)) + sent_data)
error: [Errno 32] Broken pipe
答案 0 :(得分:1)