0

Hi guys I have really simple question for you, let me explain shortly, for more detailed explanation please read below.

I want to order my 2D array which is an array of coordinates. However k= np.sort(k,0) is not working for me since it causes shuffling of y values. How can I solve this problem without losing my y values in an array of (x,y) ?

enter image description here

On the above you can see the result of np.sort(). It sorts all columns and shuffling the values, this makes the data useless.

Here is a longer explanation why I need that order:

I have a 2D array which are centers for K-means

centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5);

enter image description here

I would like to sort this array with their x values because I want to use this in a RBF function like this:

plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5);

rbfi = Rbf(centers[:, 0], centers[:, 1],function='gaussian')
u = rbfi(centers[:,0])
plt.plot(centers[:,0],u,'orange',linewidth=4)
plt.tick_params(labelcolor = 'white')

enter image description here

Since it is not ordered the function takes 2D points randomly and try to draw a line between them.

The result should look like something like this:

enter image description here

2 Answers 2

1

I'm assuming you mean that you want to keep the index unchanged. If that assumption is correct, I would suggest a dataframe, sorted with "inplace=True":

import dataframe as df
df = pd.DataFrame({'x': your_x_numpy_array, 'y': your_y_numpy_array})
df.sort_values(by=[your_index_of_choice],inplace = True)
Sign up to request clarification or add additional context in comments.

Comments

0

Here is my answer similar as @Hayden Eastwood

import pandas as pd
df = pd.DataFrame({'x': centers[:,0], 'y': centers[:,1]})
df = df.sort_values(by='x')
centers = pd.DataFrame(df).to_numpy()

And result:

enter image description here

Comments

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.