2017-09-18 2 views
0

j'ai un large éventail 100x15 comme ceci:Création d'un sous-tableau sans de aubarrays passés comme arguments en python

[a b c d e f g h i j k l m n o] 
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] 
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] 
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] 
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] 
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] 
. 
. 
.(Up to 100 rows) 

Je veux sélectionner une partie de ces données dans un sous-ensemble en utilisant une fonction qui a un argument « k » dans laquelle « k » désigne le pas de sous-ensembles à réaliser, comme dire k = 5 signifie que les attributs de données sont divisés en 3 sous-ensembles comme ci-dessous:

[a b c d e] [f g h i j] [k l m n o] 
[1 2 3 4 5] [6 7 8 9 10] [11 12 13 14 15] 
[1 2 3 4 5] [6 7 8 9 10] [11 12 13 14 15] 
[1 2 3 4 5] [6 7 8 9 10] [11 12 13 14 15] 
[1 2 3 4 5] [6 7 8 9 10] [11 12 13 14 15] 
. 
. 
.(Up to 100 rows) 

et elles sont stockées dans un réseau différent. Je veux l'implémenter en utilisant python. J'ai implémenté cela partiellement. Quelqu'un peut-il implémenter ceci et me fournir le code dans la réponse?

logique partielle pour la boucle intérieure

given k 
set start_index = 0 
end_index = length of array/k = increment 

for j from start_index to end_index 
    start_index=end_index + 1 
    end_index = end_index + increment 
    //newarray[][] (I'm not sure abt here) 

Merci.

+0

Simplement remodeler. Regardez dans 'numpy.reshape'. – Divakar

+0

Est-ce que cela donnera tous les éléments dans différents tableaux? – PSN

+0

Je ne vois qu'un seul tableau d'entrée ici. – Divakar

Répondre

0

Malheureusement, je dois le faire moi-même et c'est le code en python pour la logique. Quoi qu'il en soit, merci à @astaning pour la tentative.

def build_rotationtree_model(k): 
mtx =np.array([[2.95,6,63,23],[2,53,7,79],[3.57,5,65,32],[3.16,5,47,34],[21,2.58,4,46],[3.1,2.16,6,22],[3.5,3.27,3,52],[12,2.56,4,42]])  
#Length of attributes (width of matrix) 
a = mtx.shape[1] 
newArray =[[0 for x in range(k)] for y in range(len(mtx))] 
#Height of matrix(total rows) 
b = mtx.shape[0] 
#Seperation limit 
limit = a/k 
#Starting of sub matrix 
start = 0 
#Ending of sub matrix 
end = a/k 
print(end) 
print(a) 

#Loop 
while(end != a): 
    for i in range(0,b-1): 
     for j in range(start,int(end)): 
      newArray[i][j] = mtx[i][j] 
     print(newArray[i]) 
    #Call LDA function and add the result to Sparse Matrix 
    #sparseMat = LDA(newArray) SHould be inside a loop 
    start = end + 1 
    end = end + limit 
1

Ce retourne un tableau de matrices avec ColumnSize = 2, qui travaille pour k = 2:

import numpy as np 

def portion(mtx, k): 

    array = [] 
    array.append(mtx[:, :k]) 

    for i in range(1, mtx.shape[1]-1): 

     array.append(mtx[:, k*i:k*(i+1)]) 

    return array[:k+1] 

mtx = np.matrix([[1,2,3,10,13,14], [4,5,6,11,15,16], [7,8,9,12,17,18]]) 
k = 2 
print(portion(mtx, k)) 
+0

Merci pour le code mais malheureusement cela ne retourne pas la sortie désirée. S'il vous plaît lire ma question et laissez-moi savoir si vous avez un doute. – PSN

+0

Ok, j'essayais juste de donner l'idée générale mais je peux être plus explicite. dans la version éditée ci-dessus, il retourne au lieu d'imprimer. – astaning