slice

np.delete and np.s_. What's so special about np_s?

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I don't really understand why regular indexing can't be used for np.delete. What makes np.s_ so special? For example with this code, used to delete the some of the rows of this array.. inlet_names = np.delete(inlet_names, np.s_[1:9], axis = 0) Why can't I simply use regular indexing and do.. inlet_names = np.delete(inlet_names, [1:9], axis = 0) or inlet_names = np.delete(inlet_names, inlet_names[1:9], axis = 0) From what I can gather, np.s_ is the same as np.index_exp except it doesn't return a tuple, but both can be used anywhere in Python

slice pandas timeseries on date +/- 2 business days

余生颓废 提交于 2019-12-03 08:29:32
having following timeseries: In [65]: p Out[65]: Date 2008-06-02 125.20 2008-06-03 124.47 2008-06-04 124.40 2008-06-05 126.89 2008-06-06 122.84 2008-06-09 123.14 2008-06-10 122.53 2008-06-11 120.73 2008-06-12 121.19 Name: SPY how can I slice on a specfic date +/- 2 neighbouring (business) days, so ie if d = '2008-06-06': -2 2008-06-04 124.40 -1 2008-06-05 126.89 0 2008-06-06 122.84 1 2008-06-09 123.14 2 2008-06-10 122.53 You could use the index method get_loc , and then slice: d = pd.to_datetime('2008-06-06') loc = s.index.get_loc(d) In [12]: loc Out[12]: 4 In [13]: s[loc-2:loc+3] Out[13]:

How to slice a pyspark dataframe in two row-wise

匿名 (未验证) 提交于 2019-12-03 08:28:06
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am working in Databricks. I have a dataframe which contains 500 rows, I would like to create two dataframes on containing 100 rows and the other containing the remaining 400 rows. +--------------------+----------+ | userid| eventdate| +--------------------+----------+ |00518b128fc9459d9...|2017-10-09| |00976c0b7f2c4c2ca...|2017-12-16| |00a60fb81aa74f35a...|2017-12-04| |00f9f7234e2c4bf78...|2017-05-09| |0146fe6ad7a243c3b...|2017-11-21| |016567f169c145ddb...|2017-10-16| |01ccd278777946cb8...|2017-07-05| I have tried the below but I receive

Prevent Julia from automatically converting the type of a 1D matrix slice

怎甘沉沦 提交于 2019-12-03 07:52:32
alpha = [1 2 3; 4 5 6] alpha[:, 1] # Type is Array{Int64, 1} alpha[:, 1:2] # Type is Array{In64, 2} I just want to prevent the automatic type conversion, but I am having an amazingly hard time figuring out how to do this. Yeah, I could just go alpha[:, 1]'' , but I want to prevent the memory reallocation. There is vec() for going the other direction (1xn matrix) but I can't find a function for keeping a (nx1) matrix a matrix. jub0bs Use a range of length 1 instead of just an index Instead of simply specifying the index ( Int64 ) of the desired column, specify a range ( UnitRange{Int64} ) of

Can not save model using model.save following multi_gpu_model in Keras

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Following the upgrade to Keras 2.0.9, I have been using the multi_gpu_model utility but I can't save my models or best weights using model.save('path') The error I get is TypeError: can’t pickle module objects I suspect there is some problem gaining access to the model object. Is there a work around this issue? 回答1: Workaround Here's a patched version that doesn't fail while saving: from keras.layers import Lambda, concatenate from keras import Model import tensorflow as tf def multi_gpu_model(model, gpus): if isinstance(gpus, (list, tuple))

Why does slice capacity with odd numbers differ from behavior with even numbers

末鹿安然 提交于 2019-12-03 07:43:07
I noticed that the capacity of slices behaves in a different way, when the capacity is an odd number. More specifically: When an element is added to a slice, the capacity of the slice is doubled when the original capacity was an even number. But when the original capacity was an odd number, the capacity is incremented by one and then doubled . Example: s := make([]int, 28, 28) s = append(s, 1) fmt.Println("len=", len(s), " cap=", cap(s)) // len = len + 1, cap = 2 * cap pri := make([]int, 27, 27) pri = append(pri, 1) fmt.Println("len=", len(pri), " cap=", cap(pri)) // len = len + 1, cap = 2 *

Slicing a slice pointer passed as argument

匿名 (未验证) 提交于 2019-12-03 07:36:14
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have the following code: func main() { var buf []byte{1, 2, 3, 4, 5} buf = buf[2:] fmt.Println(buf) panic(1) } However I want to pass a pointer to buf byte slice to another function, and slice it there, so something like: func main() { var buf []byte{1, 2, 3, 4, 5} sliceArr(&buf, 2) fmt.Println(buf) panic(1) } func sliceArr(buf *[]byte, i int) { *buf = *buf[i:] } It gives me an error that I cannot use type []byte as type *[]byte in argument to sliceArr() function, and that I cannot slice type *[]byte . What's wrong? Aren't slices passed by

GO基础之切片

假装没事ソ 提交于 2019-12-03 07:35:28
一、什么是切片 Go语言切片是对数组的抽象。 Go数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片("动态数组"); 与数组相比切片的长度是不固定的,可以追加元素,在追加时可能使切片的容量增大。 切片本身没有任何数据,它们只是对现有数组的引用。 切片与数组相比,不需要设定长度,在[ ]中不用设定值,相对来说比较自由 从概念上面来说slice像一个结构体,这个结构体包含了三个元素: 切片的结构: 〇指针,指向数组中slice指定的开始位置 〇长度,SPslice的长度 〇最大长度,也就是slice开始位置到数组的最后位置的长度 二、切片的使用 切片的声明 s1 := make([]int, 5) s2 := make([]int, 5, 7) 切片的初始化: nums := []int{1, 2, 3, 4, 5} len()和cap()函数 1、 切片的长度是切片中元素的数量。 2、 切片的容量是从创建切片的索引开始的底层数组中元素的数量。 3、 切片是可索引的,并且可以由len()方法获取长度,切片提供了计算容量的方 法cap(),可以测量切片最长可以达到多少。[数组计算cap()结果与len()相同] 4、 切片实际的是获取数组的某一部分,len切片<=cap切片<=len数组 package main import "fmt"

Pick a random value from a Go Slice

隐身守侯 提交于 2019-12-03 07:27:52
问题 Situation: I've a slice of values and need to pick up a randomly chosen value from it. Then I want to concatenate it with a fixed string. This is my code so far: func main() { //create the reasons slice and append reasons to it reasons := make([]string, 0) reasons = append(reasons, "Locked out", "Pipes broke", "Food poisoning", "Not feeling well") message := fmt.Sprint("Gonna work from home...", pick a random reason ) } Question: Is there a built-in function, which can help me by doing the "

python create slice object from string

落爺英雄遲暮 提交于 2019-12-03 07:25:38
I'd like to create a slice object from a string; right now the only way seems through a cumbersome hacky eval statement class getslice: def __getitem__(self, idx): return idx[0] eval("getslice()[%s, 1]" %(":-1")) thanks in advance. Edit : Sorry if the original prompt was not clear, the input in this case was ":-1". The point was to parse the string. Ignacio Vazquez-Abrams's response at least solved the problem (and seems to work with reverse indexing as well), but I think my solution above is still more clear if not conceptually clean (and will work correctly if Python ever changes slicing