Out: array(, dtype=bool)Īnd boolean indexing can be used to select rows like this: In : mask = np. In the first array-like is in the second array-like: In : np.in1d(range(5), ) Mask = np.in1d(self.pca_pers, train_pers)įor example, np.in1d creates a boolean array which is True when the element With a call to np.in1d to create a boolean mask, and thenĭefine ain_stack and self.test_stack by indexing self.pca_data using the mask: for fold, (train_ind, test_ind) in enumerate(kf): If we can assume that every row of self.pca_data belongs in either ain_stack or self.test_stack, then you could replace the entire for-loop for data in range(len(self.pca_data)): Each time you do this,Īll the data from the original aray and the new row is copied into the new array.Īll that copying makes such a solution slower than necessary. Shape(489, 20) and Shape(489, 20) like self.pca_dataĪvoid calling np.vstack in a loop. self.test_stack and ain_stack should be for e.g.self.pca_pers: Shape(978, 1) Type(type 'numpy.ndarray').self.pca_data: Shape(978, 20) Type(type 'numpy.ndarray').Note that the ain_stack is in a loop, so an if statement, for if the variable doesn't exist, will not reset the variable when entering the loop for the 2nd time. What is the right way to initialize an empty numpy array? (type 'numpy.ndarray') If I would use numpy.zeros, then the first stack are 0's, and I want it to be completely empty before vstacking. I also tried ain_stack =, but this raises the error "ValueError: array dimensions must agree except for d_0". I tried the type() function, but this seems to be wrong. Self.pca_data contains all the image data, this data has to be distributed over ain_stack and self.test_stack. The np.stack function was added in NumPy 1.10. This function continues to be supported for backward compatibility, but you should prefer np.concatenate or np.stack. Take a sequence of arrays and stack them vertically to make a single array. The import code here is: #Set train+test stack to empty numpy.vstack numpy.vstack(tup) source Stack arrays in sequence vertically (row wise). ![]() Self.test_stack = np.vstack((self.test_stack, self.pca_data)) ain_stack = np.vstack((ain_stack, self.pca_data)) Kf = cross_validation.KFold(n, n_folds=2) My simplified code looks like this: #k-fold the data However how do I initialize an empty numpy array so I can start vstacking? I have vstacked image data and now I wish to split this in a training and test set.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |