0

So I have two questions: First I'm trying to print my array that contains 1004 elements but it's printing only the first 29 elements and then jumping to 974 to continue printing. How can I get the full array of 1004 elements?

This is my code

paired_data = []
for x in data:
    closest, ignored = pairwise_distances_argmin_min(x, result)
    paired_data.append([x, result[closest]])
#print paired_data
S = pd.DataFrame(paired_data, columns=['x','center'])
print S
# distance
Y = pdist(S, 'euclidean')
print Y

Also I want to calculate the distance between each two elements of the array. for example

0 [5, 4] [3, 2]

1 [22, -10] [78, 90]

I want to calculate the distance( Euclidean ) between [5, 4] and [3, 2] and so on for all the rest of the array.

7
  • 1
    Welcome to Stack Overflow! To keep things from getting confusing, can you please edit your question so it only has one question in it, then ask another question for your second question? Commented Mar 3, 2015 at 19:57
  • I can't ask two questions in the same day.. Sorry ! Commented Mar 3, 2015 at 19:58
  • Oh dang, I didn't realize that was a restriction! Well, check out this answer which addresses your first question: stackoverflow.com/questions/1987694/print-the-full-numpy-array Commented Mar 3, 2015 at 19:59
  • have you tried from scipy.spatial import distance Commented Mar 3, 2015 at 19:59
  • you might also find docs.scipy.org/doc/scipy/reference/generated/… helpful Commented Mar 3, 2015 at 20:01

1 Answer 1

1

Another solution to #1:

print(S.to_string())    # print the entire table

and to get distances

# assumes Python 3
from functools import partial

def dist(row, col1, col2):
    return sum((c2 - c1)**2 for c1,c2 in zip(row[col1], row[col2])) ** 0.5

# compose a function (name the columns it applies to)
s_dist = partial(dist, col1="x", col2="center")
# apply it
S["dist"] = S.apply(s_dist, axis=1)
Sign up to request clarification or add additional context in comments.

7 Comments

@Micheal: I think it should work as is, but I do not have anything but 3.4 on this machine to test with.
If I want to square ( ^2) each of these distances (1004 distances values), and then sum all of them after do the square, how can I do that?
@Micheal: it is an n-dimensional Euclidean distance. If you want sum of squares, try sq_sum = (S["dist"]**2).sum().
so why you multiply it by ** 0.5?
** is exponentiation, not multiplication, so x ** 0.5 is square root of x.
|

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.