slice

irregular slicing/copying in numpy array

会有一股神秘感。 提交于 2020-01-17 01:55:16
问题 Suppose I have an array with 10 elements, e.g. a=np.arange(10) . If I want to create another array with the 1st, 3rd, 5th, 7th, 9th, 10th elements of the original array, i.e. b=np.array([0,2,4,6,8,9]) , how can I do it efficiently? thanks 回答1: a[[0, 2, 4, 6, 8, 9]] Index a with a list or array representing the desired indices. (Not 1, 3, 5, 7, 9, 10 , because indexing starts from 0.) It's a bit confusing that the indices and the values are the same here, so have a different example: >>> a =

Combining two views of same numpy array into single view without copying the array?

大兔子大兔子 提交于 2020-01-16 18:48:35
问题 I have a large 2d numpy array and I want to remove subsets of it and handle what remains to a function. I need to do this for many subsets, and thus I would ideally not want to create a copy of the array each time. The function doesn't change any values in the array. mat = np.load(filename) mat_1 = mat[:i,:] mat_2 = mat[j:,:] So far, mat_1 and mat_2 are views. Then I would like to do mat_s = np.concatenate((mat_1,mat_2)) result = func(mat_s) but without making a copy. Is this possible? 回答1:

Combining two views of same numpy array into single view without copying the array?

时光总嘲笑我的痴心妄想 提交于 2020-01-16 18:48:29
问题 I have a large 2d numpy array and I want to remove subsets of it and handle what remains to a function. I need to do this for many subsets, and thus I would ideally not want to create a copy of the array each time. The function doesn't change any values in the array. mat = np.load(filename) mat_1 = mat[:i,:] mat_2 = mat[j:,:] So far, mat_1 and mat_2 are views. Then I would like to do mat_s = np.concatenate((mat_1,mat_2)) result = func(mat_s) but without making a copy. Is this possible? 回答1:

Combining two views of same numpy array into single view without copying the array?

不打扰是莪最后的温柔 提交于 2020-01-16 18:48:26
问题 I have a large 2d numpy array and I want to remove subsets of it and handle what remains to a function. I need to do this for many subsets, and thus I would ideally not want to create a copy of the array each time. The function doesn't change any values in the array. mat = np.load(filename) mat_1 = mat[:i,:] mat_2 = mat[j:,:] So far, mat_1 and mat_2 are views. Then I would like to do mat_s = np.concatenate((mat_1,mat_2)) result = func(mat_s) but without making a copy. Is this possible? 回答1:

r array-slice of programmatically determined dimension

南笙酒味 提交于 2020-01-16 00:55:11
问题 Is there a way of elegantly specify a slice in a multidimensional array in R, whereby the number of dimensions is not known beforehand? For instance, for a 5-dimensional array, slicing out the last dimension can be done using fourdimslice <- fivedimarray[,,,,1] but I'l like to code this for arbitrary dimensions, something like slice <- arbitrarydimarray(dim=5, index=1) I've not been able to understand if do.call('[', ...) would be an approach. I've also tried named dimensions, but a[fifth=1]

What is the fastest way to append one array to another in Go?

时光总嘲笑我的痴心妄想 提交于 2020-01-15 18:53:07
问题 Suppose that I have arrays A and B in Go. What is the fastest way to append all the values of B to A ? 回答1: Arrays in Go are secondary, slices are the way to go. Go provides a built-in append() function to append slices: a := []int{1, 2, 3} b := []int{4, 5} a = append(a, b...) fmt.Println(a) Output: [1 2 3 4 5] Try it on the Go Playground. Notes: Arrays in Go are fixed sizes: once an array is created, you cannot increase its size so you can't append elements to it. If you would have to, you

What is the fastest way to append one array to another in Go?

白昼怎懂夜的黑 提交于 2020-01-15 18:51:31
问题 Suppose that I have arrays A and B in Go. What is the fastest way to append all the values of B to A ? 回答1: Arrays in Go are secondary, slices are the way to go. Go provides a built-in append() function to append slices: a := []int{1, 2, 3} b := []int{4, 5} a = append(a, b...) fmt.Println(a) Output: [1 2 3 4 5] Try it on the Go Playground. Notes: Arrays in Go are fixed sizes: once an array is created, you cannot increase its size so you can't append elements to it. If you would have to, you

Slices in Go: why does it allow appending more than the capacity allows?

我只是一个虾纸丫 提交于 2020-01-15 12:17:30
问题 The capacity parameter in making a slice in Go does not make much sense to me. For example, aSlice := make([]int, 2, 2) //a new slice with length and cap both set to 2 aSlice = append(aSlice, 1, 2, 3, 4, 5) //append integers 1 through 5 fmt.Println("aSlice is: ", aSlice) //output [0, 0, 1, 2, 3, 4, 5] If the slice allows inserting more elements than the capacity allows, why do we need to set it in the make() function? 回答1: The builtin append() function uses the specified slice to append

Get an arbitrary slice of a Nd4j array

北战南征 提交于 2020-01-14 14:26:11
问题 I want to perform slicing in Nd4j of arbitrary sizes in the same manner as I am able to do using Numpy. a = numpy.arange(100) a[25:50] The nd4j slice method only takes dimension and index arguments, not length. How can I achieve this? 回答1: I know it's an old question but I ran across it while googling this problem exactly. By inspecting the source code for slice I believe it can only return full rows/columns not partial from index to index. You can use method get with an NDArrayIndex instance

Fast slicing of numpy array multiple times

被刻印的时光 ゝ 提交于 2020-01-14 04:07:40
问题 I have something like a np.arange([100000]) and i need to retrieve data between two indexes multiple times. Currently i running this which is slow data = np.arange([100000]) # This array usually contains thousands of slices slices = np.array( [ [1, 4], [10,20], [100,110], [1000,1220] ]) # One way i have been doing it np.take(data, [i for iin, iout in slices for idx in range(iin, iout)]) # The other way [data[iin:iout] for iin, iout in slices] Both ways are slow. I need this to be very fast. I