خرید بک لینک

Vote count: 0

Question:

I have a list of arrays such as:

a = [array([0, 4]), array([1, 3, 2])]

From another variable X, I want to take two subsets which are chosen by the index in each array in a.

X = [0.1, 0.7, 0.9, 0.2, 0.3] 

What I want to have is now:

result_1 = [0.1, 0.3]
result_2 = [0.7, 0.2, 0.9]

My solution would be to use a for loop such as:

def getresult(X, indices):
     result = []
     for i in indices:
          result.append(X[i])
     retu result

This works fine:

getresult(X, a[0])
[0.1, 0.3]

My previous experience with programming suggest, that there is a much more beautiful and simple way to do so and that this is the place to ask.

Background/application:

Background: Crossvalidation for parameter optimization.

I have a list containing datapoints such as

X = [0.1, 0.7, 0.9, 0.2, 0.3]

Now I want to repeatedly take z samples out of that list (which actually is much bigger than this example). Therefore I create a new variable:

indices = np.arange(0,len(X),1)

HERE: [0, 1, 2, 3, 4]

Which then i shuffle and create nfold samples:

np.random.shuffle(indices)        
nfold_indices = np.array_split(indices,nfolds)

HERE with nfolds = 2: nfold_indices = [array([0, 4]), array([1, 3, 2])]

asked 43 secs ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 26 خرداد 1395 ساعت: 19:49

صفحه بندی