numpy-ndarray

How to create a numpy array of all True or all False?

吃可爱长大的小学妹 提交于 2019-11-27 05:03:05
问题 In Python, how do I create a numpy array of arbitrary shape filled with all True or all False? 回答1: numpy already allows the creation of arrays of all ones or all zeros very easily: e.g. numpy.ones((2, 2)) or numpy.zeros((2, 2)) Since True and False are represented in Python as 1 and 0 , respectively, we have only to specify this array should be boolean using the optional dtype parameter and we are done. numpy.ones((2, 2), dtype=bool) returns: array([[ True, True], [ True, True]], dtype=bool)

Better way to shuffle two numpy arrays in unison

三世轮回 提交于 2019-11-27 04:09:26
问题 I have two numpy arrays of different shapes, but with the same length (leading dimension). I want to shuffle each of them, such that corresponding elements continue to correspond -- i.e. shuffle them in unison with respect to their leading indices. This code works, and illustrates my goals: def shuffle_in_unison(a, b): assert len(a) == len(b) shuffled_a = numpy.empty(a.shape, dtype=a.dtype) shuffled_b = numpy.empty(b.shape, dtype=b.dtype) permutation = numpy.random.permutation(len(a)) for old

Zero pad numpy array

谁说我不能喝 提交于 2019-11-27 02:04:38
问题 What's the more pythonic way to pad an array with zeros at the end? def pad(A, length): ... A = np.array([1,2,3,4,5]) pad(A, 8) # expected : [1,2,3,4,5,0,0,0] In my real use case, in fact I want to pad an array to the closest multiple of 1024. Ex: 1342 => 2048, 3000 => 3072 回答1: numpy.pad with constant mode does what you need, where we can pass a tuple as second argument to tell how many zeros to pad on each size, a (2, 3) for instance will pad 2 zeros on the left side and 3 zeros on the

What is the purpose of meshgrid in Python / NumPy?

不羁岁月 提交于 2019-11-26 23:44:53
问题 Can someone explain to me what is the purpose of meshgrid function in Numpy? I know it creates some kind of grid of coordinates for plotting, but I can't really see the direct benefit of it. I am studying "Python Machine Learning" from Sebastian Raschka, and he is using it for plotting the decision borders. See input 11 here. I have also tried this code from official documentation, but, again, the output doesn't really make sense to me. x = np.arange(-5, 5, 1) y = np.arange(-5, 5, 1) xx, yy =

how to copy numpy array value into higher dimensions

≡放荡痞女 提交于 2019-11-26 21:35:39
问题 I have a (w,h) np array in 2d. I want to make a 3d dimension that has a value greater than 1 and copy its value over along the 3rd dimensions. I was hoping broadcast would do it but it can't. This is how i'm doing it arr = np.expand_dims(arr, axis=2) arr = np.concatenate((arr,arr,arr), axis=2) is there a a faster way to do so? 回答1: You can push all dims forward, introducing a singleton dim/new axis as the last dim to create a 3D array and then repeat three times along that one with np.repeat,

How do I calculate percentiles with python/numpy?

可紊 提交于 2019-11-26 19:21:17
Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy array? I am looking for something similar to Excel's percentile function. I looked in NumPy's statistics reference, and couldn't find this. All I could find is the median (50th percentile), but not something more specific. Jon W You might be interested in the SciPy Stats package. It has the percentile function you're after and many other statistical goodies. percentile() is available in numpy too. import numpy as np a = np.array([1,2,3,4,5]) p = np.percentile(a, 50) # return 50th percentile, e.g

What is the difference between ndarray and array in numpy?

寵の児 提交于 2019-11-26 18:47:16
问题 What is the difference between ndarray and array in Numpy? And where can I find the implementations in the numpy source code? 回答1: numpy.array is just a convenience function to create an ndarray ; it is not a class itself. You can also create an array using numpy.ndarray , but it is not the recommended way. From the docstring of numpy.ndarray : Arrays should be constructed using array , zeros or empty ... The parameters given here refer to a low-level method ( ndarray(...) ) for instantiating

How does numpy.newaxis work and when to use it?

倖福魔咒の 提交于 2019-11-26 16:51:10
When I try numpy.newaxis the result gives me a 2-d plot frame with x-axis from 0 to 1. However, when I try using numpy.newaxis to slice a vector, vector[0:4,] [ 0.04965172 0.04979645 0.04994022 0.05008303] vector[:, np.newaxis][0:4,] [[ 0.04965172] [ 0.04979645] [ 0.04994022] [ 0.05008303]] Is it the same thing except that it changes a row vector to a column vector? Generally, what is the use of numpy.newaxis , and in which circumstances should we use it? Simply put, the newaxis is used to increase the dimension of the existing array by one more dimension , when used once . Thus, 1D array will

How does the axis parameter from NumPy work?

雨燕双飞 提交于 2019-11-26 15:24:15
问题 Can someone explain exactly what the axis parameter in NumPy does? I am terribly confused. I'm trying to use the function myArray.sum(axis=num) At first I thought if the array is itself 3 dimensions, axis=0 will return three elements, consisting of the sum of all nested items in that same position. If each dimension contained five dimensions, I expected axis=1 to return a result of five items, and so on. However this is not the case, and the documentation does not do a good job helping me out

Concatenating two one-dimensional NumPy arrays

孤街醉人 提交于 2019-11-26 15:13:52
I have two simple one-dimensional arrays in NumPy . I should be able to concatenate them using numpy.concatenate . But I get this error for the code below: TypeError: only length-1 arrays can be converted to Python scalars Code import numpy a = numpy.array([1, 2, 3]) b = numpy.array([5, 6]) numpy.concatenate(a, b) Why? Winston Ewert The line should be: numpy.concatenate([a,b]) The arrays you want to concatenate need to passed in as a sequence, not as separate arguments. From the NumPy documentation : numpy.concatenate((a1, a2, ...), axis=0) Join a sequence of arrays together. It was trying to