array

Sorting a python array/recarray by column

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a fairly simple question about how to sort an entire array/recarray by a given column. For example, given the array: import numpy as np data = np.array([[5,2], [4,1], [3,6]]) I would like to sort data by the first column to return: array([[3,6], [4,1], [5,2]]) 回答1: Use data[np.argsort(data[:, 0])] where the 0 is the column index on which to sort: In [27]: import numpy as np In [28]: data = np.array([[5,2], [4,1], [3,6]]) In [29]: col = 0 In [30]: data=data[np.argsort(data[:,col])] Out[30]: array([[3, 6], [4, 1], [5, 2]]) 回答2: you are

Find the lonely integer in an array

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Please refer to this hackerrank challenge if you can. The problem is to find the lonely integer in an array, given an array consists of only pairs except one lonely integer. The problem is with this test case 9 4 9 95 93 57 4 57 93 9 9 is array size and below is the array See the part of code highlighted by //------ If I place scanf("%d", &n) above int arr[n] code works fine, but gives horrible results the other way round. Please help me out #include <stdio.h> int lonely_integer(int* a, int size); int main(){ //n is size of array, i is

What are the rules for comparing numpy arrays using ==?

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: For example, trying to make sense of these results: >>> x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> (x == np.array([[1],[2]])).astype(np.float32) array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32) >>> (x == np.array([1,2])) False >>> (x == np.array([[1]])).astype(np.float32) array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32) >>> (x == np.array([1])).astype(np.float32) array([ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32) >>> (x == np.array([[1,3],[2]])) False

np.delete and np.s_. What&#039;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

Set rank of array at runtime

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I was wondering what the simplest way would be to implement an array who's rank is specified at runtime. The example I am working on stores a array of boolean values for lattice points, and I want the user to be able to chose how many spatial dimensions the model uses at runtime. I've looked at the Array.newInstance() method: dimensionOfSpace = userInputValue ; // this value comes from GUI or whatever int latticeLength = 5 ; // square lattice for simplicity int [] dimensions = new int [ dimensionOfSpace ]; for ( int i = 0 ; i < l .

NameError: name &#039;array&#039; is not defined in python

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I get NameError: name 'array' is not defined in python error when I want to create array, for example: a = array([1,8,3]) What am I doing wrong? How to use arrays? 回答1: If you need a container to hold a bunch of things, then lists might be your best bet: a = [1,8,3] Type dir([]) from a Python interpreter to see the methods that lists support, such as append, pop, reverse, and sort. Lists also support list comprehensions and Python's iterable interface: for x in a: print x y = [x ** 2 for x in a] 回答2: You need to import the array method from

numpy append_field gives shape error for new field with 2d shape

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a structured numpy array, I want to use the recfunctions library http://pyopengl.sourceforge.net/pydoc/numpy.lib.recfunctions.html function append_fields() or rec_append_fields() to append a field with some shape to it. However, I get an error: ValueError: operands could not be broadcast together with shapes (10) (10,3) where 10 is the length of my existing array, and (3,) is the shape of the field I want to append. For example: import numpy as np from numpy.lib.recfunctions import append_fields my_structured_array = np.array( zip([0

Flatten object to array?

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm using an object as a hash table. I'd like to quickly print out its contents (for alert() for instance). Is there anything built in to convert a hash into arrays of (key, value) pairs? 回答1: Since you want to alert it out I assume it's not for your production version, and that old browser compatibility is not an issue. If this is the case, then you can do this: var myHash = ...... alert ( Object . keys ( myHash ). map ( function ( key ) { return [ key , myHash [ key ]]; })); 回答2: I updated this some more. This is much easier to

Performing grouped average and standard deviation with NumPy arrays

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a set of data (X,Y). My independent variable values X are not unique, so there are multiple repeated values, I want to output a new array containing : X_unique, which is a list of unique values of X. Y_mean, the mean of all of the Y values corresponding to X_unique. Y_std, the standard deviation of all the Y values corresponding to X_unique. x = data[:,0] y = data[:,1] 回答1: x_unique = np.unique(x) y_means = np.array([np.mean(y[x==u]) for u in x_unique]) y_stds = np.array([np.std(y[x==u]) for u in x_unique]) 回答2: You can use binned

why a char array must end with a null character

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: why a char array must end with a null character. is there any reason that I've to add the null character in every char array ? it seems that they get treated the same way. 回答1: In C, if you have a pointer to an array, then there is not way to determine the length of that array. As @AProgrammer points out, the designers could have left it at that and forced the programmer to keep track of the length of all character arrays. However, that would have made text processing in C even harder than it already is. Therefore the language designers