0

I have a 2 dimensional numpy array. I am trying to make pairs of each row with every other row.

For example:

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

It should have the following pairs(comma separated):

Pair(1,2) 1 2 3 4 5, 6 7 8 9 10
Pair(1,3) 1 2 3 4 5, 11 12 13 14 15
Pair(2,1) 6 7 8 9 10, 1 2 3 4 5
Pair(2,3) 6 7 8 9 10, 11 12 13 14 15
Pair(3,1) 11 12 13 14 15, 1 2 3 4 5
Pair(3,2) 11 12 13 14 15, 6 7 8 9 10

To achieve this task, I am converting each row to string then removing multiple white spaces, end line chars and [ ] chars.

But when I use numpy.array_str() for converting a large array (1500*100) it converts the row to:

1 2 ...., 4 5, 6 7 ....,8 9

Why is this happening and how can I fix this problem such that it doesn't slows down my program? If there is no possible way to do this without compromising speed then how can I achieve this task?

2 Answers 2

0

Maybe then a posible answer is:

import numpy as np
def pair(matrix,i,j):
    str1=''
    for value in matrix[i]:
        str1+=str(value)+' '
    # Delete one extra space
    str1=str1[:-1]

    str2=''
    for value in matrix[j]:
        str2+=str(value)+' '
    str2=str2[:-1]
    return str1+', '+str2

matrix=np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])

print pair(matrix,0,1)
print pair(matrix,0,2)
3
  • pair(matrix, 0,1) will give: [ 1 2 3 4 5 6 7 8 9 10]. I want [ 1 2 3 4 5, 6 7 8 9 10] with or without brackets. Commented Nov 9, 2015 at 21:51
  • @BlackDragon So you need it to be a string? if not you cannot put a comma there Commented Nov 9, 2015 at 21:53
  • Yes. I need it to be a string as that's the only possible way I found in doing that but the problem is that numpy.array_str() for a large row/col will give result like this: [1 2 ..., 4 5, 6 7 ..., 9 10] . The ellipses(...) are appearing in the result. Commented Nov 9, 2015 at 22:02
0

When the array gets big enough the default print starts using ellipsis

In [351]: np.array_str(np.ones((15,100)))
Out[351]: '[[ 1.  1.  1. ...,  1.  1.  1.]\n [ 1.  1.  1. ...,  1.  1.  1.]\n [ 1.  1.  1. ...,  1.  1.  1.]\n ..., \n [ 1.  1.  1. ...,  1.  1.  1.]\n [ 1.  1.  1. ...,  1.  1.  1.]\n [ 1.  1.  1. ...,  1.  1.  1.]]'

In [354]: np.ones((15,100))
Out[354]: 
array([[ 1.,  1.,  1., ...,  1.,  1.,  1.],
       [ 1.,  1.,  1., ...,  1.,  1.,  1.],
       [ 1.,  1.,  1., ...,  1.,  1.,  1.],
       ..., 
       [ 1.,  1.,  1., ...,  1.,  1.,  1.],
       [ 1.,  1.,  1., ...,  1.,  1.,  1.],
       [ 1.,  1.,  1., ...,  1.,  1.,  1.]])

In [355]: np.get_printoptions()
Out[355]: 
{'threshold': 1000,
 'infstr': 'inf',
 'suppress': False,
 'linewidth': 75,
 'formatter': None,
 'nanstr': 'nan',
 'edgeitems': 3,
 'precision': 8}

printoptions lets you tweak the formatting. I think it's the threshold that triggers the ....

np.ones((11,100)) gets the ..., np.ones((10,100)) does not.

np.set_printoptions(threshold=3000)
np.ones((21,100))   # no ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.