2011-02-18 33 views
7

छँटाई मैं इस सरणी, नामित वी, dtype की ('float64') मिल गया है:अजगर (Numpy) सरणी

array([[ 9.33350000e+05, 8.75886500e+06, 3.45765000e+02], 
     [ 4.33350000e+05, 8.75886500e+06, 6.19200000e+00], 
     [ 1.33360000e+05, 8.75886500e+06, 6.76650000e+02]]) 

... जो मैं एनपी का उपयोग करके एक फ़ाइल से प्राप्त हुआ है। loadtxt कमांड। मैं इसे पहले कॉलम के मानों के बाद क्रमबद्ध करना चाहता हूं, संरचना को मिश्रित किए बिना जो एक ही पंक्ति पर सूचीबद्ध संख्याओं को एक साथ रखता है। (अक्ष = 0) v.sort का उपयोग करते हुए मुझे देता है:

array([[ 1.33360000e+05, 8.75886500e+06, 6.19200000e+00], 
     [ 4.33350000e+05, 8.75886500e+06, 3.45765000e+02], 
     [ 9.33350000e+05, 8.75886500e+06, 6.76650000e+02]]) 

... यानी, पहली पंक्ति में तीसरे स्तंभ की सबसे छोटी संख्या देता है आदि मैं नहीं बल्कि कुछ इस तरह चाहेगा ...

array([[ 1.33360000e+05, 8.75886500e+06, 6.76650000e+02], 
     [ 4.33350000e+05, 8.75886500e+06, 6.19200000e+00], 
     [ 9.33350000e+05, 8.75886500e+06, 3.45765000e+02]]) 

... जहां प्रत्येक पंक्ति के तत्व अपेक्षाकृत एक दूसरे के लिए स्थानांतरित नहीं किए गए हैं।

उत्तर

13

(v सरणी होने के साथ)

v[v[:,0].argsort()] 

की कोशिश करो। v[:,0] पहला कॉलम है, और .argsort() उन इंडेक्स को लौटाता है जो पहले कॉलम को सॉर्ट करेंगे। इसके बाद आप उन्नत अनुक्रमण का उपयोग कर पूरे सरणी में यह ऑर्डरिंग लागू करते हैं। ध्यान दें कि आपको सरणी की एक प्रतिलिपि प्रति प्राप्त होती है।

एक ही रास्ता मैं जगह में सरणी सॉर्ट करने के लिए के बारे में पता एक रिकार्ड dtype उपयोग करने के लिए है: आप जिन मामलों में v[:,0] कुछ समान मूल्य हैं और आप गौणतः तरह करने के लिए कॉलम 1 पर चाहते हैं

v.dtype = [("x", float), ("y", float), ("z", float)] 
v.shape = v.size 
v.sort(order="x") 
5

वैकल्पिक रूप से

import numpy as np 

order = v[:, 0].argsort() 
sorted = np.take(v, order, 0) 

'ऑर्डर' पहली पंक्ति के आदेश है की कोशिश करो। और फिर 'np.take' कॉलम को उनके संबंधित क्रम में ले जाएं।

रूप

help(np.take) 

ले (एक, सूचकांक, अक्ष = कोई नहीं, बाहर = कोई नहीं, मोड = 'बढ़ाने') 'np.take' की मदद देखें एक सरणी से तत्व ले लो धुरी के साथ।

This function does the same thing as "fancy" indexing (indexing arrays 
using arrays); however, it can be easier to use if you need elements 
along a given axis. 

Parameters 
---------- 
a : array_like 
    The source array. 
indices : array_like 
    The indices of the values to extract. 
axis : int, optional 
    The axis over which to select values. By default, the flattened 
    input array is used. 
out : ndarray, optional 
    If provided, the result will be placed in this array. It should 
    be of the appropriate shape and dtype. 
mode : {'raise', 'wrap', 'clip'}, optional 
    Specifies how out-of-bounds indices will behave. 

    * 'raise' -- raise an error (default) 
    * 'wrap' -- wrap around 
    * 'clip' -- clip to the range 

    'clip' mode means that all indices that are too large are 

सूचकांक कि कि धुरी के साथ पिछले तत्व के पते से बदल दिया। नोट कि यह नकारात्मक संख्याओं के साथ अनुक्रमण को अक्षम करता है।

Returns 
------- 
subarray : ndarray 
    The returned array has the same type as `a`. 

See Also 
-------- 
ndarray.take : equivalent method 

Examples 
-------- 
>>> a = [4, 3, 5, 7, 6, 8] 
>>> indices = [0, 1, 4] 
>>> np.take(a, indices) 
array([4, 3, 6]) 

In this example if `a` is an ndarray, "fancy" indexing can be used. 

>>> a = np.array(a) 
>>> a[indices] 
array([4, 3, 6]) 
संबंधित मुद्दे