slice

Get mean of 2D slice of a 3D array in numpy

匿名 (未验证) 提交于 2019-12-03 01:10:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a numpy array with a shape of: ( 11L , 5L , 5L ) I want to calculate the mean over the 25 elements of each 'slice' of the array [0, :, :], [1, :, :] etc, returning 11 values. It seems silly, but I can't work out how to do this. I've thought the mean(axis=x) function would do this, but I've tried all possible combinations of axis and none of them give me the result I want. I can obviously do this using a for loop and slicing, but surely there is a better way? 回答1: Use a tuple for axis : >>> a = np . arange ( 11 * 5 * 5 ).

How slice Numpy array by column value

匿名 (未验证) 提交于 2019-12-03 01:03:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an array like this numpy array dd =[[0.567 2 0.611] [0.469 1 0.479] [0.220 2 0.269] [0.480 1 0.508] [0.324 1 0.324]] I need 2 seperate array dd[:,1] ==1 and dd[:,1] ==2 These array are what I am after: na =[[0.469 1 0.479] [0.480 1 0.508] [0.324 1 0.324]] na2 =[[0.567 2 0.611] [0.220 2 0.269]] I have tried np.where did really work 回答1: You could use numpy fancy indexing: [~/repo/py] |32>dd[dd[:,1] == 1] [32] array([[ 0.469, 1. , 0.479], [ 0.48 , 1. , 0.508], [ 0.324, 1. , 0.324]]) [~/repo/py] |33>dd[dd[:,1] == 2] [33] array([[ 0.567,

tf.assign to variable slice doesn't work inside tf.while_loop

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What is wrong with the following code? The tf.assign op works just fine when applied to a slice of a tf.Variable if it happens outside of a loop. But, in this context, it gives the error below. import tensorflow as tf v = [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0] n = len(v) a = tf.Variable(v, name = 'a') def cond(i, a): return i < n def body(i, a): tf.assign(a[i], a[i-1] + a[i-2]) return i + 1, a i, b = tf.while_loop(cond, body, [2, a]) results in: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/hrbigelow/anaconda3

IndexError: fail to coerce slice entry of type tensorvariable to integer

匿名 (未验证) 提交于 2019-12-03 00:50:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I run "ipython debugf.py" and it gave me error message as below IndexError Traceback (most recent call last) /home/ml/debugf.py in <module>() 8 fff = theano.function(inputs=[index], 9 outputs=cost, ---> 10 givens={x: train_set_x[index: index+1]}) IndexError: failed to coerce slice entry of type TensorVariable to integer" I search the forum and no luck, is there someone can help ? thanks! debugf.py : import theano.tensor as T import theano import numpy index =T.lscalar() x=T.dmatrix() cost=x +index train_set_x=numpy.arange(100).reshape([20,5]

Golang: get the type of slice

匿名 (未验证) 提交于 2019-12-03 00:50:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using reflect package to get the type of arbitrary array, but getting prog.go:17: cannot use sample_array1 (type []int) as type []interface {} in function argument [process exited with non-zero status] How do I get the type from array? I know how to get it from value. func GetTypeArray(arr []interface{}) reflect.Type { return reflect.TypeOf(arr[0]) } http://play.golang.org/p/sNw8aL0a5f 回答1: Change: GetTypeArray(arr []interface{}) to: GetTypeArray(arr interface{}) By the way, []int is not an array but a slice of integers. 回答2: The fact

Why can't I duplicate a slice with `copy()`?

北战南征 提交于 2019-12-03 00:45:56
问题 I need to make a copy of a slice in Go and reading the docs there is a copy function at my disposal. The copy built-in function copies elements from a source slice into a destination slice. (As a special case, it also will copy bytes from a string to a slice of bytes.) The source and destination may overlap. Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst). But when I do: arr := []int{1, 2, 3} tmp := []int{} copy(tmp, arr) fmt.Println(tmp) fmt

Reverse string: string[::-1] works, but string[0::-1] and others don't

我的梦境 提交于 2019-12-03 00:37:25
问题 I am somewhat of a python/programming newbie, and I have just been playing around with string slicing. So the simple string reverse method of string[::-1] works just fine as we know, but there are other instances in my code below that yields unexpected results: In [1]: string = "Howdy doody" In [2]: string[::] Out[2]: 'Howdy doody' In [3]: string[::-1] Out[3]: 'ydood ydwoH' In [4]: string[0:] Out[4]: 'Howdy doody' In [5]: string[0::-1] Out[5]: 'H' # what up with this? In [6]: string[:len

How to generate a compiler warning/error when object sliced

落花浮王杯 提交于 2019-12-03 00:25:42
I want to know if it is possible to let compiler issue a warning/error for code as following: Note: 1. Yea, it is bad programming style and we should avoid such cases - but we are dealing with legacy code and hope compiler can help identify such cases for us.) 2. I prefer a compiler option (VC++) to disable or enable object slicing, if there is any. class Base{}; class Derived: public Base{}; void Func(Base) { } //void Func(Derived) //{ // //} //main Func(Derived()); Here if I comment out the second function, the first function would be called - and the compiler (both VC++ and Gcc) feels

Go初始化变量的招式

匿名 (未验证) 提交于 2019-12-03 00:21:02
年初的立的各种Flag,已经被我抛到九霄云外去了。2018年已经过去了一半,终于开始了第三篇文章,距离全年30篇的输出计划,仅剩27本,我很有“信心完成”剩下的部分。 2018年伊始,开始从PHP转到Go的开发方向,虽然说学习路线并不是非常陡峭,但是过程中遇到不少坑以及有意思的地方,忍不住想总结分享给大家。今天先来聊一聊Go中初始化变量的几种方式。 Golang的数据类型可以分为: 值类型 与 引用类型 ,我先来总结一下Go中值类型(以 string 为例)的初始化方式: var a1 string fmt.Printf( "a1: %#v \n" , a1) // a1: "" var a2 * string fmt.Printf( "a2: %#v \n" , a2) // a2: (*string)(nil) // panic: runtime error: invalid memory address or nil pointer dereference //fmt.Printf(**"a2: %#v \n"**, *a2) a3 := new ( string ) fmt.Printf( "a3: %#v \n" , a3) // a3: (*string)(0xc42000e200) fmt.Printf(** "a3: %#v \n" **, *a3) // a3:

Xilinx-7系列FPGA架构学习 --- CLB

匿名 (未验证) 提交于 2019-12-03 00:18:01
Achievement provides the only real pleasure in life. 有所成就是人生唯一的真正的乐趣。 本文主要参考:xilinx官方文档UG474《7 Series FPGAs Configurable Logic Block》和White Paper《Xilinx 7 Series FPGAs:The Logical Advantage》。 CLBs是实现时序逻辑组合逻辑电路的主要逻辑单元。 一个CLB由两个slice组成,而每个slice由4个6输入LUT、8个FF、复用器和算术进位单元组成。 latches. In that case, the remaining four flip-flops in that slice must remain unused.每个slice中的4个FF可以做锁存器,但是剩下的4个FF不能够再使用,也就是说有50%的资源浪费。这也就是为什么教科书上说,尽量避免latchs使用,除了时序问题之外,还有资源浪费。从芯片资源手册也可以看出,以kintex-7为例: Slices = SliceL + SliceM;SliceL / SliceM = 2;Slices / LUTs = 4;FF / Slices = 8。 两个slice分别位于CLB的左列底部和右列顶部。这两个slice不是直接互联