Dataset Viewer
Auto-converted to Parquet Duplicate
text
stringlengths
29
2.66k
source
stringclasses
5 values
**How do I print the full NumPy array, without truncation?** **Top answer** Use numpy.set_printoptions: import sys import numpy numpy.set_printoptions(threshold=sys.maxsize)
stackoverflow.com
**How do I get indices of N maximum values in a NumPy array?** **Top answer** Newer NumPy versions (1.8 and up) have a function called argpartition for this. To get the indices of the four largest elements, do >>> a = np.array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0]) >>> a array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0]) >>> ind = np.argpartition(a, -4)[-4:] >>> ind array([1, 5, 8, 0]) >>> top4 = a[ind] >>> top4 array([4, 9, 6, 9]) Unlike argsort, this function runs in linear time in the worst case, but the returned indices are not sorted, as can be seen from the result of evaluating a[ind]. If you need that too, sort them afterwards: >>> ind[np.argsort(a[ind])] array([1, 8, 5, 0]) To get the top-k elements in sorted order in this way takes O(n + k log k) time.
stackoverflow.com
**Dump a NumPy array into a csv file** **Top answer** numpy.savetxt saves an array to a text file. import numpy a = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ]) numpy.savetxt("foo.csv", a, delimiter=",")
stackoverflow.com
**How can the Euclidean distance be calculated with NumPy?** **Top answer** Use numpy.linalg.norm: dist = numpy.linalg.norm(a-b) This works because the Euclidean distance is the l2 norm, and the default value of the ord parameter in numpy.linalg.norm is 2. For more theory, see Introduction to Data Mining:
stackoverflow.com
**Convert Pandas dataframe to NumPy array** **Top answer** Use df.to_numpy() It's better than df.values, here's why.* It's time to deprecate your usage of values and as_matrix(). pandas v0.24.0 introduced two new methods for obtaining NumPy arrays from pandas objects: to_numpy(), which is defined on Index, Series, and DataFrame objects, and array, which is defined on Index and Series objects only. If you visit the v0.24 docs for .values, you will see a big red warning that says: Warning: We recommend using DataFrame.to_numpy() instead. See this section of the v0.24.0 release notes, and this answer for more information. * - to_numpy() is my recommended method for any production code that needs to run reliably for many versions into the future. However
stackoverflow.com
**Most efficient way to map function over numpy array** **Top answer** I've tested all suggested methods plus np.array(list(map(f, x))) with perfplot (a small project of mine). Message #1: If you can use numpy's native functions, do that. If the function you're trying to vectorize already is vectorized (like the x**2 example in the original post), using that is much faster than anything else (note the log scale): If you actually need vectorization, it doesn't really matter much which variant you use. Code to reproduce the plots: import numpy as np import perfplot import math def f(x): # return math.sqrt(x) return np.sqrt(x) vf = np.vectorize(f) def array_for(x): return np.array([f(xi) for xi in x]) def array_map(x): return np.array(list(map(f, x))) def fromiter(x): return np.fromiter((f(xi) for xi in x), x.dtype) def vectorize(x): return np.vectorize(f)(x) def vectorize_without_init(x): return vf(x) b = perfplot.bench( setup=np.random.rand, n_range=[2 ** k for k in range(20)], kernels=[ f, array_for, array_map, fromiter, vectorize, vectorize_without_init, ], xlabel="len(x)", ) b.save("out1.svg") b.show()
stackoverflow.com
**What does -1 mean in numpy reshape?** **Top answer** The criterion to satisfy for providing the new shape is that 'The new shape should be compatible with the original shape' numpy allow us to give one of new shape parameter as -1 (eg: (2,-1) or (-1,3) but not (-1, -1)). It simply means that it is an unknown dimension and we want numpy to figure it out. And numpy will figure this by looking at the 'length of the array and remaining dimensions' and making sure it satisfies the above mentioned criteria Now see the example. z = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) z.shape (3, 4) Now trying to reshape with (-1) . Result new shape is (12,) and is compatible with original shape (3,4) z.reshape(-1) array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) Now trying to reshape with (-1, 1) . We have provided column as 1 but rows as unknown . So we get result new shape as (12, 1).again compatible with original shape(3,4) z.reshape(-1,1) array([[ 1], [ 2], [ 3], [ 4], [ 5], [ 6], [ 7], [ 8], [ 9], [10], [11], [12]]) The above is consistent with numpy advice/error message, to use reshape(-1,1) for a single feature; i.e. single column Reshape your data using array.reshape(-1, 1) if your data has a single feature New shape as (-1, 2). row unknown, column 2. we get result new shape as
stackoverflow.com
**How do I access the ith column of a NumPy multidimensional array?** **Top answer** With: test = np.array([[1, 2], [3, 4], [5, 6]]) To access column 0: >>> test[:, 0] array([1, 3, 5]) To access row 0: >>> test[0, :] array([1, 2]) This is covered in Section 1.4 (Indexing) of the NumPy reference. This is quick, at least in my experience. It's certainly much quicker than accessing each element in a loop.
stackoverflow.com
**How do I count the occurrence of a certain item in an ndarray?** **Top answer** Using numpy.unique: import numpy a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4]) unique, counts = numpy.unique(a, return_counts=True) >>> dict(zip(unique, counts)) {0: 7, 1: 4, 2: 1, 3: 2, 4: 1} Non-numpy method using collections.Counter; import collections, numpy a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4]) counter = collections.Counter(a) >>> counter Counter({0: 7, 1: 4, 3: 2, 2: 1, 4: 1})
stackoverflow.com
**Is there a NumPy function to return the first index of something in an array?** **Top answer** Yes, given an array, array, and a value, item to search for, you can use np.where as: itemindex = numpy.where(array == item) The result is a tuple with first all the row indices, then all the column indices. For example, if an array is two dimensions and it contained your item at two locations then array[itemindex[0][0]][itemindex[1][0]] would be equal to your item and so would be: array[itemindex[0][1]][itemindex[1][1]]
stackoverflow.com
**Pandas read_csv: low_memory and dtype options** **Top answer** The deprecated low_memory option The low_memory option is not properly deprecated, but it should be, since it does not actually do anything differently[source] The reason you get this low_memory warning is because guessing dtypes for each column is very memory demanding. Pandas tries to determine what dtype to set by analyzing the data in each column. Dtype Guessing (very bad) Pandas can only determine what dtype a column should have once the whole file is read. This means nothing can really be parsed before the whole file is read unless you risk having to change the dtype of that column when you read the last value. Consider the example of one file which has a column called user_id. It contains 10 million rows where
stackoverflow.com
**How can I use the apply() function for a single column?** **Top answer** Given a sample dataframe df as: a b 0 1 2 1 2 3 2 3 4 3 4 5 what you want is: df['a'] = df['a'].apply(lambda x: x + 1) that returns: a b 0 2 2 1 3 3 2 4 4 3 5 5
stackoverflow.com
**How do I read CSV data into a record array in NumPy?** **Top answer** Use numpy.genfromtxt() by setting the delimiter kwarg to a comma: from numpy import genfromtxt my_data = genfromtxt('my_file.csv', delimiter=',')
stackoverflow.com
**What are the advantages of NumPy over regular Python lists?** **Top answer** NumPy's arrays are more compact than Python lists -- a list of lists as you describe, in Python, would take at least 20 MB or so, while a NumPy 3D array with single-precision floats in the cells would fit in 4 MB. Access in reading and writing items is also faster with NumPy. Maybe you don't care that much for just a million cells, but you definitely would for a billion cells -- neither approach would fit in a 32-bit architecture, but with 64-bit builds NumPy would get away with 4 GB or so, Python alone would need at least about 12 GB (lots of pointers which double in size) -- a much costlier piece of hardware! The difference is mostly due to "indirectness" -- a Python list is an array of pointers to Python objects, at least 4 bytes per pointer plus 16 bytes for even the smallest Python object (4 for type pointer, 4 for reference count, 4 for value -- and the memory allocators rounds up to 16). A NumPy array is an array of uniform values -- single-precision numbers takes 4 bytes each, double-precision ones, 8 bytes. Less flexible, but you pay substantially for the flexibility of standard Python lists!
stackoverflow.com
**Sorting arrays in NumPy by column** **Top answer** To sort by the second column of a: a[a[:, 1].argsort()]
stackoverflow.com
**What does numpy.random.seed(0) do?** **Top answer** np.random.seed(0) makes the random numbers predictable >>> numpy.random.seed(0) ; numpy.random.rand(4) array([ 0.55, 0.72, 0.6 , 0.54]) >>> numpy.random.seed(0) ; numpy.random.rand(4) array([ 0.55, 0.72, 0.6 , 0.54]) With the seed reset (every time), the same set of numbers will appear every time. If the random seed is not reset, different numbers appear with every invocation: >>> numpy.random.rand(4) array([ 0.42, 0.65, 0.44, 0.89]) >>> numpy.random.rand(4) array([ 0.96, 0.38, 0.79, 0.53]) (pseudo-)random numbers work by starting with a number (the seed), multiplying it by a large number, adding an offset, then taking modulo of that sum. The resulting number is then used as the seed to generate the next "random" number. When you set the seed (every time), it does the same thing every time, giving you the same numbers. If you want seemingly random numbers, do not set the seed. If you have code that uses random numbers that you want to debug, however, it can be very helpful to set the seed before each run so that the code does the same thing every time you run it. To get the most random numbers for each run, call numpy.random.seed(). This will cause numpy to set the seed to a random number obtained from /dev/urandom or its Windows analog or, if neither of those is available, it will use the clock. For more information on using seeds to generate pseudo-random numbers, see wikipedia.
stackoverflow.com
**How do I create a new column where the values are selected based on an existing column?** **Top answer** If you only have two choices to select from then use np.where: df['color'] = np.where(df['Set']=='Z', 'green', 'red') For example, import pandas as pd import numpy as np df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')}) df['color'] = np.where(df['Set']=='Z', 'green', 'red') print(df) yields Set Type color 0 Z A green 1 Z B green 2 X B red 3 Y C red If you have more than two conditions then use np.select. For example, if you want color to be yellow when (df['Set'] == 'Z') & (df['Type'] == 'A') otherwise blue when (df['Set'] == 'Z') & (df['Type'] == 'B') otherwise purple when (df['Type'] == 'B') otherwise black, then use df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')}) conditions = [ (df['Set'] == 'Z') & (df['Type'] == 'A'), (df['Set'] == 'Z') & (df['Type'] == 'B'), (df['Type'] == 'B')] choices = ['yellow', 'blue', 'purple'] df['color'] = np.select(conditions, choices, default='black') print(df) which yields Set Type color 0 Z A yellow 1 Z B blue 2 X B purple 3 Y C black
stackoverflow.com
**What is the purpose of meshgrid in NumPy?** **Top answer** The purpose of meshgrid is to create a rectangular grid out of an array of x values and an array of y values. So, for example, if we want to create a grid where we have a point at each integer value between 0 and 4 in both the x and y directions. To create a rectangular grid, we need every combination of the x and y points. This is going to be 25 points, right? So if we wanted to create an x and y array for all of these points, we could do the following. x[0,0] = 0 y[0,0] = 0 x[0,1] = 1 y[0,1] = 0 x[0,2] = 2 y[0,2] = 0 x[0,3] = 3 y[0,3] = 0 x[0,4] = 4 y[0,4] = 0 x[1,0] = 0 y[1,0] = 1 x[1,1] = 1 y[1,1] = 1 ... x[4,3] = 3 y[4,3] = 4 x[4,4] = 4 y[4,4] = 4 This would result in the following x and y matrices, such that the pairing of the corresponding element in each matrix gives the x and y coordinates of a point in the grid. x = 0 1 2 3 4 y = 0 0 0 0 0 0 1 2 3 4 1 1 1 1 1 0 1 2 3 4 2 2 2 2 2 0 1 2 3 4 3 3 3 3 3 0 1 2 3 4 4 4 4
stackoverflow.com
**Find nearest value in numpy array** **Top answer** import numpy as np def find_nearest(array, value): array = np.asarray(array) idx = (np.abs(array - value)).argmin() return array[idx] Example usage: array = np.random.random(10) print(array) # [ 0.21069679 0.61290182 0.63425412 0.84635244 0.91599191 0.00213826 # 0.17104965 0.56874386 0.57319379 0.28719469] print(find_nearest(array, value=0.5)) # 0.568743859261
stackoverflow.com
**How do I convert a PIL Image into a NumPy array?** **Top answer** You're not saying how exactly putdata() is not behaving. I'm assuming you're doing >>> pic.putdata(a) Traceback (most recent call last): File "...blablabla.../PIL/Image.py", line 1185, in putdata self.im.putdata(data, scale, offset) SystemError: new style getargs format but argument is not a tuple This is because putdata expects a sequence of tuples and you're giving it a numpy array. This >>> data = list(tuple(pixel) for pixel in pix) >>> pic.putdata(data) will work but it is very slow. As of PIL 1.1.6, the "proper" way to convert between images and numpy arrays is simply >>> pix = numpy.array(pic) although the resulting array is in a different format than yours (3-d array or rows/columns/rgb in this case). Then, after you make your changes to the array, you should be able to do either pic.putdata(pix) or create a new image with Image.fromarray(pix).
stackoverflow.com
**Pretty-print a NumPy array without scientific notation and with given precision** **Top answer** Use numpy.set_printoptions to set the precision of the output: import numpy as np x = np.random.random(10) print(x) # [ 0.07837821 0.48002108 0.41274116 0.82993414 0.77610352 0.1023732 # 0.51303098 0.4617183 0.33487207 0.71162095] np.set_printoptions(precision=3) print(x) # [ 0.078 0.48 0.413 0.83 0.776 0.102 0.513 0.462 0.335 0.712] And suppress suppresses the use of scientific notation for small numbers: y = np.array([1.5e-10, 1.5, 1500]) print(y) # [ 1.500e-10 1.500e+00 1.500e+03] np.set_printoptions(suppress=True) print(y) # [ 0. 1.5 1500. ] To apply print options locally, using NumPy 1.15.0 or later, you could use the numpy.printoptions context manager. For example, inside the with-suite precision=3 and suppress=True are set: x = np.random.random(10) with np.printoptions(precision=3, suppress=True): print(x) # [ 0.073 0.461 0.689 0.754 0.624 0.901 0.049 0.582 0.557 0.348] But outside the with-suite the print options are back to default settings: print(x) # [ 0.07334334 0.46132615 0.68935231 0.75379645 0.62424021 0
stackoverflow.com
**Converting between datetime, Timestamp and datetime64** **Top answer** You can just use the pd.Timestamp constructor. The following diagram may be useful for this and related questions.
stackoverflow.com
**NumPy array is not JSON serializable** **Top answer** I regularly "jsonify" np.arrays. Try using the ".tolist()" method on the arrays first, like this: import numpy as np import codecs, json a = np.arange(10).reshape(2,5) # a 2 by 5 array b = a.tolist() # nested lists with same data, indices file_path = "/path.json" ## your path variable json.dump(b, codecs.open(file_path, 'w', encoding='utf-8'), separators=(',', ':'), sort_keys=True, indent=4) ### this saves the array in .json format In order to "unjsonify" the array use: obj_text = codecs.open(file_path, 'r', encoding='utf-8').read() b_new = json.loads(obj_text) a_new = np.array(b_new)
stackoverflow.com
**What is the difference between np.array() and np.asarray()?** **Top answer** The definition of asarray is: def asarray(a, dtype=None, order=None): return array(a, dtype, copy=False, order=order) So it is like array, except it has fewer options, and copy=False. array has copy=True by default. The main difference is that array (by default) will make a copy of the object, while asarray will not unless necessary.
stackoverflow.com
**Comparing two NumPy arrays for equality, element-wise** **Top answer** (A==B).all() test if all values of array (A==B) are True. Note: maybe you also want to test A and B shape, such as A.shape == B.shape Special cases and alternatives (from dbaupp's answer and yoavram's comment) It should be noted that: this solution can have a strange behavior in a particular case: if either A or B is empty and the other one contains a single element, then it return True. For some reason, the comparison A==B returns an empty array, for which the all operator returns True. Another risk is if A and B don't have the same shape and aren't broadcastable, then this approach will raise an error. In conclusion, if you have a doubt about A and B shape or simply want to be safe: use one of the specialized functions: np.array_equal(A,B) # test if same shape, same elements values np.array_equiv(A,B) # test if broadcastable shape, same elements values np.allclose(A,B,...) # test if same shape, elements have close enough values
stackoverflow.com
**What is the difference between flatten and ravel functions in numpy?** **Top answer** The current API is that: flatten always returns a copy. ravel returns a contiguous view of the original array whenever possible. This isn't visible in the printed output, but if you modify the array returned by ravel, it may modify the entries in the original array. If you modify the entries in an array returned from flatten this will never happen. ravel will often be faster since no memory is copied, but you have to be more careful about modifying the array it returns. reshape((-1,)) gets a view whenever the strides of the array allow it even if that means you don't always get a contiguous array.
stackoverflow.com
**How do I create an empty array and then append to it in NumPy?** **Top answer** That is the wrong mental model for using NumPy efficiently. NumPy arrays are stored in contiguous blocks of memory. To append rows or columns to an existing array, the entire array needs to be copied to a new block of memory, creating gaps for the new elements to be stored. This is very inefficient if done repeatedly. Instead of appending rows, allocate a suitably sized array, and then assign to it row-by-row: >>> import numpy as np >>> a = np.zeros(shape=(3, 2)) >>> a array([[ 0., 0.], [ 0., 0.], [ 0., 0.]]) >>> a[0] = [1, 2] >>> a[1] = [3, 4] >>> a[2] = [5, 6] >>> a array([[ 1., 2.], [ 3., 4.], [ 5., 6.]])
stackoverflow.com
**What are the differences between numpy arrays and matrices? Which one should I use?** **Top answer** Numpy matrices are strictly 2-dimensional, while numpy arrays (ndarrays) are N-dimensional. Matrix objects are a subclass of ndarray, so they inherit all the attributes and methods of ndarrays. The main advantage of numpy matrices is that they provide a convenient notation for matrix multiplication: if a and b are matrices, then a*b is their matrix product. import numpy as np a = np.mat('4 3; 2 1') b = np.mat('1 2; 3 4') print(a) # [[4 3] # [2 1]] print(b) # [[1 2] # [3 4]] print(a*b) # [[13 20] # [ 5 8]] On the other hand, as of Python 3.5, NumPy supports infix matrix multiplication using the @ operator, so you can achieve the same convenience of matrix multiplication with ndarrays in Python >= 3.5. import numpy as np a = np.array([[4, 3], [2, 1]]) b = np.array([[1, 2], [3, 4]]) print(a@b) # [[13 20] # [ 5 8]] Both matrix objects and ndarrays have .T to return the transpose, but matrix objects also have .H for the conjugate transpose, and .I for the inverse. In contrast, numpy arrays consistently abide by the rule
stackoverflow.com
**Numpy array dimensions** **Top answer** Use .shape to obtain a tuple of array dimensions: >>> a.shape (2, 2)
stackoverflow.com
**Saving a Numpy array as an image** **Top answer** Using PIL, save a NumPy array arr by doing: from PIL import Image im = Image.fromarray(arr) im.save("your_file.jpeg") See the docs for available data formats, including JPEG, PNG, and so on.
stackoverflow.com
**Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?** **Top answer** Specify data, index and columns to the DataFrame constructor, as follows: >>> pd.DataFrame(data=data[1:,1:], # values ... index=data[1:,0], # 1st column as index ... columns=data[0,1:]) # 1st row as the column names As @joris mentions, you may need to change above to np.int_(data[1:,1:]) to have the correct data type.
stackoverflow.com
**Difference between numpy.array shape (R, 1) and (R,)** **Top answer** 1. The meaning of shapes in NumPy You write, "I know literally it's list of numbers and list of lists where all list contains only a number" but that's a bit of an unhelpful way to think about it. The best way to think about NumPy arrays is that they consist of two parts, a data buffer which is just a block of raw elements, and a view which describes how to interpret the data buffer. For example, if we create an array of 12 integers: >>> a = numpy.arange(12) >>> a array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) Then a consists of a data buffer, arranged something like this: ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐ │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │ └────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘ and a view which describes how to interpret the data: >>> a.flags C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA :
stackoverflow.com
**Convert NumPy array to Python list** **Top answer** Use tolist(): >>> import numpy as np >>> np.array([[1,2,3],[4,5,6]]).tolist() [[1, 2, 3], [4, 5, 6]] Note that this converts the values from whatever numpy type they may have (e.g. np.int32 or np.float32) to the "nearest compatible Python type" (in a list). If you want to preserve the numpy data types, you could call list() on your array instead, and you'll end up with a list of numpy scalars. (Thanks to Mr_and_Mrs_D for pointing that out in a comment.)
stackoverflow.com
**Simple Digit Recognition OCR in OpenCV-Python** **Top answer** Well, I decided to workout myself on my question to solve the above problem. What I wanted is to implement a simple OCR using KNearest or SVM features in OpenCV. And below is what I did and how. (it is just for learning how to use KNearest for simple OCR purposes). 1) My first question was about letter_recognition.data file that comes with OpenCV samples. I wanted to know what is inside that file. It contains a letter, along with 16 features of that letter. And this SOF helped me to find it. These 16 features are explained in the paper Letter Recognition Using Holland-Style Adaptive Classifiers. (Although
stackoverflow.com
**Concatenating two one-dimensional NumPy arrays** **Top answer** Use: np.concatenate([a, b]) The arrays you want to concatenate need to be 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 interpret your b as the axis parameter, which is why it complained it couldn't convert it into a scalar.
stackoverflow.com
**ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()** **Top answer** If a and b are Boolean NumPy arrays, the & operation returns the elementwise-and of them: a & b That returns a Boolean array. To reduce this to a single Boolean value, use either (a & b).any() or (a & b).all() Note: if a and b are non-Boolean arrays, consider (a - b).any() or (a - b).all() instead. Rationale The NumPy developers felt there was no one commonly understood way to evaluate an array in Boolean context: it could mean True if any element is True, or it could mean True if all elements are True, or True if the array has non-zero length, just to name three possibilities. Since different users might have different needs and different assumptions, the NumPy developers refused to guess and instead decided to raise a ValueError whenever one tries to evaluate an array in Boolean context. Applying and to two numpy arrays causes the two arrays to be evaluated in Boolean context (by calling __bool__ in Python3 or __nonzero__ in Python2).
stackoverflow.com
**NumPy array initialization (fill with identical values)** **Top answer** NumPy 1.8 introduced np.full(), which is a more direct method than empty() followed by fill() for creating an array filled with a certain value: >>> np.full((3, 5), 7) array([[ 7., 7., 7., 7., 7.], [ 7., 7., 7., 7., 7.], [ 7., 7., 7., 7., 7.]]) >>> np.full((3, 5), 7, dtype=int) array([[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]]) This is arguably the way of creating an array filled with certain values, because it explicitly describes what is being achieved (and it can in principle be very efficient since it performs a very specific task).
stackoverflow.com
**Frequency counts for unique values in a NumPy array** **Top answer** Use numpy.unique with return_counts=True (for NumPy 1.9+): import numpy as np x = np.array([1,1,1,2,2,2,5,25,1,1]) unique, counts = np.unique(x, return_counts=True) >>> print(np.asarray((unique, counts)).T) [[ 1 5] [ 2 3] [ 5 1] [25 1]] In comparison with scipy.stats.itemfreq: In [4]: x = np.random.random_integers(0,100,1e6) In [5]: %timeit unique, counts = np.unique(x, return_counts=True) 10 loops, best of 3: 31.5 ms per loop In [6]: %timeit scipy.stats.itemfreq(x) 10 loops, best of 3: 170 ms per loop
stackoverflow.com
**What is the difference between ndarray and array in NumPy?** **Top answer** 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 an array. Most of the meat of the implementation is in C code, here in multiarray, but you can start looking at the ndarray interfaces here: https://github.com/numpy/numpy/blob/master/numpy/core/numeric.py
stackoverflow.com
**Dropping infinite values from dataframes in pandas?** **Top answer** First replace() infs with NaN: df.replace([np.inf, -np.inf], np.nan, inplace=True) and then drop NaNs via dropna(): df.dropna(subset=["col1", "col2"], how="all", inplace=True) For example: >>> df = pd.DataFrame({"col1": [1, np.inf, -np.inf], "col2": [2, 3, np.nan]}) >>> df col1 col2 0 1.0 2.0 1 inf 3.0 2 -inf NaN >>> df.replace([np.inf, -np.inf], np.nan, inplace=True) >>> df col1 col2 0 1.0 2.0 1 NaN 3.0 2 NaN NaN >>> df.dropna(subset=["col1", "col2"], how="all", inplace=True) >>> df col1 col2 0 1.0 2.0 1 NaN 3.0 The same method also works for Series.
stackoverflow.com
**How do I use np.newaxis?** **Top answer** Simply put, numpy.newaxis is used to increase the dimension of the existing array by one more dimension, when used once. Thus, 1D array will become 2D array 2D array will become 3D array 3D array will become 4D array 4D array will become 5D array and so on.. Here is a visual illustration which depicts promotion of 1D array to 2D arrays. Scenario-1: np.newaxis might come in handy when you want to explicitly convert a 1D array to either a row vector or a column vector, as depicted in the above picture. Example: # 1D array In [7]: arr = np.arange(4) In [8]: arr.shape Out[8]: (4,) # make it as row vector by inserting an axis along first dimension In [9]: row_vec = arr[np.newaxis, :] # arr[None, :] In [10]: row_vec.shape Out[10]: (1, 4) # make it as column vector by inserting an axis along second dimension In [11]: col_vec = arr[:, np.newaxis] # arr[:
stackoverflow.com
**Converting numpy dtypes to native python types** **Top answer** Use val.item() to convert most NumPy values to a native Python type: import numpy as np # for example, numpy.float32 -> python float val = np.float32(0) pyval = val.item() print(type(pyval)) # <class 'float'> # and similar... type(np.float64(0).item()) # <class 'float'> type(np.uint32(0).item()) # <class 'int'> type(np.int16(0).item()) # <class 'int'> type(np.cfloat(0).item()) # <class 'complex'> type(np.datetime64(0, 'D').item()) # <class 'datetime.date'> type(np.datetime64('2001-01-01 00:00:00').item()) # <class 'datetime.datetime'> type(np.timedelta64(0, 'D').item()) # <class 'datetime.timedelta'> ... (A related method np.asscalar(val) was deprecated with 1.16, and removed with 1.23). For the curious, to build a table of conversions of NumPy array scalars for your system: for name in dir(np): obj = getattr(np, name) if hasattr(obj, 'dtype'): try: if 'time' in name: npn = obj(0, 'D') else: npn = obj(0) nat = npn.item() print('{0} ({1!r}) -> {2}'.format(name, npn.dtype.char, type(nat))) except: pass There are a few NumPy types that have no native Python equivalent on some systems, including: clongdouble, clongfloat, complex192, complex256, float128, longcomplex, longdouble and longfloat.
stackoverflow.com
**How do I remove NaN values from a NumPy array?** **Top answer** To remove NaN values from a NumPy array x: x = x[~numpy.isnan(x)] Explanation The inner function numpy.isnan returns a boolean/logical array which has the value True everywhere that x is not-a-number. Since we want the opposite, we use the logical-not operator ~ to get an array with Trues everywhere that x is a valid number. Lastly, we use this logical array to index into the original array x, in order to retrieve just the non-NaN values.
stackoverflow.com
**How do I check which version of NumPy I&#39;m using?** **Top answer** import numpy numpy.version.version
stackoverflow.com
**How do I convert a numpy array to (and display) an image?** **Top answer** Use plt.imshow to create the figure, and plt.show to display it: from matplotlib import pyplot as plt plt.imshow(data, interpolation='nearest') plt.show() For Jupyter notebooks, add this line before importing matplotlib: %matplotlib inline For interactive plots in Jupyter [demo], install ipyml pip install ipympl, then use: %matplotlib widget
stackoverflow.com
**Most efficient way to reverse a numpy array** **Top answer** reversed_arr = arr[::-1] gives a reversed view into the original array arr. Any changes made to the original array arr will also be immediately visible in reversed_arr. The underlying data buffers for arr and reversed_arr are shared, so creating this view is always instantaneous, and does not require any additional memory allocation or copying for the array contents. See also, this discussion on NumPy views: How do I create a view onto a NumPy array? Possible solutions to performance problems regarding views Are you re-creating the view more often than you need to? You should be able to do something like this: arr = np.array(some_sequence) reversed_arr = arr[::-1] do_something(arr) look_at(reversed_arr) do_something_else(arr) look_at(reversed_arr) I'm not a numpy expert, but this seems like it would be the fastest way to do things in numpy. If this is what you are already doing, I don't think you can improve on it.
stackoverflow.com
**Understanding NumPy&#39;s einsum** **Top answer** (Note: this answer is based on a short blog post about einsum I wrote a while ago.) What does einsum do? Imagine that we have two multi-dimensional arrays, A and B. Now let's suppose we want to... multiply A with B in a particular way to create new array of products; and then maybe sum this new array along particular axes; and then maybe transpose the axes of the new array in a particular order. There's a good chance that einsum will help us do this faster and more memory-efficiently than combinations of the NumPy functions like multiply, sum and transpose will allow. How does einsum work? Here's a simple (but not completely trivial) example. Take the following two arrays: A = np.array([0, 1, 2]) B = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) We
stackoverflow.com
**Split (explode) pandas dataframe string entry to separate rows** **Top answer** UPDATE 3: it makes more sense to use Series.explode() / DataFrame.explode() methods (implemented in Pandas 0.25.0 and extended in Pandas 1.3.0 to support multi-column explode) as is shown in the usage example: for a single column: In [1]: df = pd.DataFrame({'A': [[0, 1, 2], 'foo', [], [3, 4]], ...: 'B': 1, ...: 'C': [['a', 'b', 'c'], np.nan, [], ['d', 'e']]}) In [2]: df Out[2]: A B C 0 [0, 1, 2] 1 [a, b, c] 1 foo 1 NaN 2 [] 1 [] 3 [3, 4] 1 [d, e] In [3]: df.explode('A') Out[3]: A B
stackoverflow.com
**Convert 2D float array to 2D int array in NumPy** **Top answer** Use the astype method. >>> x = np.array([[1.0, 2.3], [1.3, 2.9]]) >>> x array([[ 1. , 2.3], [ 1.3, 2.9]]) >>> x.astype(int) array([[1, 2], [1, 2]])
stackoverflow.com
**How to remove specific elements in a numpy array** **Top answer** Use numpy.delete(), which returns a new array with sub-arrays along an axis deleted. numpy.delete(a, index) For your specific question: import numpy as np a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) index = [2, 3, 6] new_a = np.delete(a, index) print(new_a) # Output: [1, 2, 5, 6, 8, 9] Note that numpy.delete() returns a new array since array scalars are immutable, similar to strings in Python, so each time a change is made to it, a new object is created. I.e., to quote the delete() docs: "A copy of arr with the elements specified by obj removed. Note that delete does not occur in-place..." If the code I post has output, it is the result of running the code.
stackoverflow.com
**Convert array of indices to one-hot encoded array in NumPy** **Top answer** Create a zeroed array b with enough columns, i.e. a.max() + 1. Then, for each row i, set the a[i]th column to 1. >>> a = np.array([1, 0, 3]) >>> b = np.zeros((a.size, a.max() + 1)) >>> b[np.arange(a.size), a] = 1 >>> b array([[ 0., 1., 0., 0.], [ 1., 0., 0., 0.], [ 0., 0., 0., 1.]])
stackoverflow.com
**Better way to shuffle two numpy arrays in unison** **Top answer** Your can use NumPy's array indexing: def unison_shuffled_copies(a, b): assert len(a) == len(b) p = numpy.random.permutation(len(a)) return a[p], b[p] This will result in creation of separate unison-shuffled arrays.
stackoverflow.com
**Convert a tensor to numpy array in Tensorflow?** **Top answer** TensorFlow 2.x Eager Execution is enabled by default, so just call .numpy() on the Tensor object. import tensorflow as tf a = tf.constant([[1, 2], [3, 4]]) b = tf.add(a, 1) a.numpy() # array([[1, 2], # [3, 4]], dtype=int32) b.numpy() # array([[2, 3], # [4, 5]], dtype=int32) tf.multiply(a, b).numpy() # array([[ 2, 6], # [12, 20]], dtype=int32) See NumPy Compatibility for more. It is worth noting (from the docs), Numpy array may share a memory with the Tensor object. Any changes to one may
stackoverflow.com
**Error &quot;Import Error: No module named numpy&quot; on Windows** **Top answer** You can simply use pip install numpy Or for python3, use pip3 install numpy
stackoverflow.com
**ValueError: setting an array element with a sequence** **Top answer** Possible reason 1: trying to create a jagged array You may be creating an array from a list that isn't shaped like a multi-dimensional array: numpy.array([[1, 2], [2, 3, 4]]) # wrong! numpy.array([[1, 2], [2, [3, 4]]]) # wrong! In these examples, the argument to numpy.array contains sequences of different lengths. Those will yield this error message because the input list is not shaped like a "box" that can be turned into a multidimensional array. Possible reason 2: providing elements of incompatible types For example, providing a string as an element in an array of type float: numpy.array([1.2, "abc"], dtype=float) # wrong! If you really want to have a NumPy array containing both strings and floats, you could use the dtype object, which allows the array to hold arbitrary Python objects: numpy.array([1.2, "abc"], dtype=object)
stackoverflow.com
**How to take column-slices of dataframe in pandas** **Top answer** 2017 Answer - pandas 0.20: .ix is deprecated. Use .loc See the deprecation in the docs .loc uses label based indexing to select both rows and columns. The labels being the values of the index or the columns. Slicing with .loc includes the last element. Let's assume we have a DataFrame with the following columns: foo, bar, quz, ant, cat, sat, dat. # selects all rows and all columns beginning at 'foo' up to and including 'sat' df.loc[:, 'foo':'sat'] # foo bar quz ant cat sat .loc accepts the same slice notation that Python lists do for both row and columns. Slice notation being start:stop:step # slice from 'foo' to 'cat' by every 2nd column df.loc[:, 'foo':'cat':2] # foo quz cat # slice from the beginning to 'bar' df.loc[:, :'bar'] # foo bar # slice from 'quz' to the end by 3 df.loc[:, 'quz'::3] # quz sat # attempt from 'sat' to 'bar' df.loc[:, 'sat':'bar'] # no columns returned # slice from 'sat' to 'bar' df.loc[:, 'sat':'bar':-1] sat cat ant quz bar # slice notation is syntatic sugar for the slice function # slice from 'quz' to the end by 2 with slice function df.loc[:, slice('quz',None, 2)] # quz cat dat # select specific columns with a list # select columns foo, bar and dat df.loc[:, ['foo','bar','dat']] # foo bar dat You can slice by rows and columns.
stackoverflow.com
**How to normalize a numpy array to a unit vector** **Top answer** If you're using scikit-learn you can use sklearn.preprocessing.normalize: import numpy as np from sklearn.preprocessing import normalize x = np.random.rand(1000)*10 norm1 = x / np.linalg.norm(x) norm2 = normalize(x[:,np.newaxis], axis=0).ravel() print np.all(norm1 == norm2) # True
stackoverflow.com
**How can I map True/False to 1/0 in a Pandas DataFrame?** **Top answer** A succinct way to convert a single column of boolean values to a column of integers 1 or 0: df["somecolumn"] = df["somecolumn"].astype(int)
stackoverflow.com
**How do I calculate percentiles with python/numpy?** **Top answer** NumPy has np.percentile(). import numpy as np a = np.array([1,2,3,4,5]) p = np.percentile(a, 50) # return 50th percentile, i.e. median. >>> print(p) 3.0 SciPy has scipy.stats.scoreatpercentile(), in addition to many other statistical goodies.
stackoverflow.com
**How to implement the Softmax function in Python?** **Top answer** They're both correct, but yours is preferred from the point of view of numerical stability. You start with e ^ (x - max(x)) / sum(e^(x - max(x)) By using the fact that a^(b - c) = (a^b)/(a^c) we have = e ^ x / (e ^ max(x) * sum(e ^ x / e ^ max(x))) = e ^ x / sum(e ^ x) Which is what the other answer says. You could replace max(x) with any variable and it would cancel out.
stackoverflow.com
**Moving average or running mean** **Top answer** NOTE: More efficient solutions may include scipy.ndimage.uniform_filter1d (see this answer), or using newer libraries including talib's talib.MA. Use np.convolve: np.convolve(x, np.ones(N)/N, mode='valid') Explanation The running mean is a case of the mathematical operation of convolution. For the running mean, you slide a window along the input and compute the mean of the window's contents. For discrete 1D signals, convolution is the same thing, except instead of the mean you compute an arbitrary linear combination, i.e., multiply each element by a corresponding coefficient and add up the results. Those coefficients, one for each position in the window, are sometimes called the convolution kernel. The arithmetic mean of N values is (x_1 + x_2 + ... + x_N) / N, so the corresponding kernel is (1/N, 1/N, ..., 1/N), and that's exactly what we get by using np.ones(N)/N. Edges The mode argument of np.convolve specifies how to handle the edges. I chose the valid mode here because I think that's how most people expect the running mean to work, but you may have other priorities. Here is a plot that illustrates the difference between the modes: import numpy as np import matplotlib.pyplot as plt modes = ['full', 'same', 'valid'] for m in modes: plt.plot(np.convolve(np.ones(200), np.ones(50)/50, mode=m)); plt.axis([-10, 251, -.1, 1.1]); plt.legend(modes, loc='lower center'); plt.show()
stackoverflow.com
**How do I create a numpy array of all True or all False?** **Top answer** The answer: numpy.full((2, 2), True) Explanation: numpy creates 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) UPDATE: 30 October 2013 Since numpy version 1.8, we can use full to achieve the same result with syntax that more clearly shows our intent (as fmonegaglia points out): numpy.full((2, 2), True, dtype=bool) UPDATE: 16 January 2017 Since at least numpy version 1.12, full automatically casts to the dtype of the second parameter, so we can just write: numpy.full((2, 2), True)
stackoverflow.com
**Create numpy matrix filled with NaNs** **Top answer** You rarely need loops for vector operations in numpy. You can create an uninitialized array and assign to all entries at once: >>> a = numpy.empty((3,3,)) >>> a[:] = numpy.nan >>> a array([[ NaN, NaN, NaN], [ NaN, NaN, NaN], [ NaN, NaN, NaN]]) I have timed the alternatives a[:] = numpy.nan here and a.fill(numpy.nan) as posted by Blaenk: $ python -mtimeit "import numpy as np; a = np.empty((100,100));" "a.fill(np.nan)" 10000 loops, best of 3: 54.3 usec per loop $ python -mtimeit "import numpy as np; a = np.empty((100,100));" "a[:] = np.nan" 10000 loops, best of 3: 88.8 usec per loop The timings show a preference for ndarray.fill(..) as the faster alternative. OTOH, I like numpy's convenience implementation where you can assign values to whole slices at the time, the code's intention is very clear. Note that ndarray.fill performs its operation in-place, so numpy.empty((3,3,)).fill(numpy.nan) will instead return None.
stackoverflow.com
**How to smooth a curve for a dataset** **Top answer** I prefer a Savitzky-Golay filter. It's available in scipy here. It uses least squares to regress a small window of your data onto a polynomial, then uses the polynomial to estimate the point in the center of the window. Finally the window is shifted forward by one data point and the process repeats. This continues until every point has been optimally adjusted relative to its neighbors. It works great even with noisy samples from non-periodic and non-linear sources. Here is a thorough cookbook example, although this is outdated now. Note: I left out the code for defining the savitzky_golay() function because you can copy/paste it from the cookbook example I linked above. import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,2*np.pi,100) y = np.sin(x) + np.random.random(100) * 0.2 yhat = savitzky_golay(y, 51, 3) # window size 51, polynomial order 3 plt.plot(x,y) plt.plot(x,yhat, color='red') plt.show() UPDATE: It has come to my attention that the cookbook example I linked to has been taken down. Fortunately, the Savitzky-Golay filter has been incorporated into the SciPy library, as pointed out by @dodohjk (thanks @bicarlsen for the updated link). To adapt the above code by using SciPy source, type: from scipy.signal import savgol_filter yhat = savgol_filter(y, 51, 3) # window size 51, polynomial order 3
stackoverflow.com
**Take multiple lists into dataframe** **Top answer** I think you're almost there, try removing the extra square brackets around the lst's (Also you don't need to specify the column names when you're creating a dataframe from a dict like this): import pandas as pd lst1 = range(100) lst2 = range(100) lst3 = range(100) percentile_list = pd.DataFrame( {'lst1Title': lst1, 'lst2Title': lst2, 'lst3Title': lst3 }) percentile_list lst1Title lst2Title lst3Title 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 ... If you need a more performant solution you can use np.column_stack rather than zip as in your first attempt, this has around a 2x speedup on the example here, however comes at bit of a cost of readability in my opinion: import numpy as np percentile_list = pd.DataFrame(np.column_stack([lst1, lst2, lst3]), columns=['lst1Title', 'lst2Title', 'lst3Title'])
stackoverflow.com
**How can I check whether a numpy array is empty or not?** **Top answer** You can always take a look at the .size attribute. It is defined as an integer, and is zero (0) when there are no elements in the array: import numpy as np a = np.array([]) if a.size == 0: # Do something when `a` is empty
stackoverflow.com
**Replacing Pandas or Numpy Nan with a None to use with MysqlDB** **Top answer** df = df.replace({np.nan: None}) Note: For pandas versions <1.4, this changes the dtype of all affected columns to object. To avoid that, use this syntax instead: df = df.replace(np.nan, None) Note 2: If you don't want to import numpy, np.nan can be replaced with native float('nan'): df = df.replace({float('nan'): None}) Credit goes to this guy here on this Github issue, Killian Huyghe's comment and Matt's answer.
stackoverflow.com
**How to convert a NumPy array to PIL image applying matplotlib colormap** **Top answer** Quite a busy one-liner, but here it is: First ensure your NumPy array, myarray, is normalised with the max value at 1.0. Apply the colormap directly to myarray. Rescale to the 0-255 range. Convert to integers, using np.uint8(). Use Image.fromarray(). And you're done: from PIL import Image from matplotlib import cm im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255)) with plt.savefig(): with im.save():
stackoverflow.com
**Transposing a 1D NumPy array** **Top answer** It's working exactly as it's supposed to. The transpose of a 1D array is still a 1D array! (If you're used to matlab, it fundamentally doesn't have a concept of a 1D array. Matlab's "1D" arrays are 2D.) If you want to turn your 1D vector into a 2D array and then transpose it, just slice it with np.newaxis (or None, they're the same, newaxis is just more readable). import numpy as np a = np.array([5,4])[np.newaxis] print(a) print(a.T) Generally speaking though, you don't ever need to worry about this. Adding the extra dimension is usually not what you want, if you're just doing it out of habit. Numpy will automatically broadcast a 1D array when doing various calculations. There's usually no need to distinguish between a row vector and a column vector (neither of which are vectors. They're both 2D!) when you just want a vector.
stackoverflow.com
**Replace all elements of NumPy array that are greater than some value** **Top answer** I think both the fastest and most concise way to do this is to use NumPy's built-in Fancy indexing. If you have an ndarray named arr, you can replace all elements >255 with a value x as follows: arr[arr > 255] = x I ran this on my machine with a 500 x 500 random matrix, replacing all values >0.5 with 5, and it took an average of 7.59ms. In [1]: import numpy as np In [2]: A = np.random.rand(500, 500) In [3]: timeit A[A > 0.5] = 5 100 loops, best of 3: 7.59 ms per loop
stackoverflow.com
**Relationship between SciPy and NumPy** **Top answer** Last time I checked it, the scipy __init__ method executes a from numpy import * so that the whole numpy namespace is included into scipy when the scipy module is imported. The log10 behavior you are describing is interesting, because both versions are coming from numpy. One is a ufunc, the other is a numpy.lib function. Why scipy is preferring the library function over the ufunc, I don't know off the top of my head. EDIT: In fact, I can answer the log10 question. Looking in the scipy __init__ method I see this: # Import numpy symbols to scipy name space import numpy as _num from numpy import oldnumeric from numpy import * from numpy.random import rand, randn from numpy.f
stackoverflow.com
**Counting unique values in a column in pandas dataframe like in Qlik?** **Top answer** Count distinct values, use nunique: df['hID'].nunique() 5 Count only non-null values, use count: df['hID'].count() 8 Count total values including null values, use the size attribute: df['hID'].size 8 Edit to add condition Use boolean indexing: df.loc[df['mID']=='A','hID'].agg(['nunique','count','size']) OR using query: df.query('mID == "A"')['hID'].agg(['nunique','count','size']) Output: nunique 5 count 5 size 5 Name: hID, dtype: int64
stackoverflow.com
**Is it possible to use argsort in descending order?** **Top answer** If you negate an array, the lowest elements become the highest elements and vice-versa. Therefore, the indices of the n highest elements are: (-avgDists).argsort()[:n] Another way to reason about this, as mentioned in the comments, is to observe that the big elements are coming last in the argsort. So, you can read from the tail of the argsort to find the n highest elements: avgDists.argsort()[::-1][:n] Both methods are O(n log n) in time complexity, because the argsort call is the dominant term here. But the second approach has a nice advantage: it replaces an O(n) negation of the array with an O(1) slice. If you're working with small arrays inside loops then you may get some performance gains from avoiding that negation, and if you're working with huge arrays then you can save on memory usage because the negation creates a copy of the entire array. Note that these methods do not always give equivalent results: if a stable sort implementation is requested to argsort, e.g. by passing the keyword argument kind='mergesort', then the first strategy will preserve the sorting stability, but the second strategy will break stability (i.e. the positions of equal items will get reversed). Example timings: Using a small array of 100 floats and a length 30 tail, the view method was about 15% faster >>> avgDists = np.random.rand(100) >>> n = 30 >>> timeit (-avgDists).argsort()[:n] 1.93 µs ± 6.68 ns per loop (mean ± std. d
stackoverflow.com
**List of lists into numpy array** **Top answer** If your list of lists contains lists with varying number of elements then the answer of Ignacio Vazquez-Abrams will not work. Instead there are at least 3 options: 1) Make an array of arrays: x=[[1,2],[1,2,3],[1]] y=numpy.array([numpy.array(xi) for xi in x]) type(y) >>><type 'numpy.ndarray'> type(y[0]) >>><type 'numpy.ndarray'> 2) Make an array of lists: x=[[1,2],[1,2,3],[1]] y=numpy.array(x) type(y) >>><type 'numpy.ndarray'> type(y[0]) >>><type 'list'> 3) First make the lists equal in length: x=[[1,2],[1,2,3],[1]] length = max(map(len, x)) y=numpy.array([xi+[None]*(length-len(xi)) for xi in x]) y >>>array([[1, 2, None], >>> [1, 2, 3], >>> [1, None, None]], dtype=object)
stackoverflow.com
**np.mean() vs np.average() in Python NumPy?** **Top answer** np.average takes an optional weight parameter. If it is not supplied they are equivalent. Take a look at the source code: Mean, Average np.mean: try: mean = a.mean except AttributeError: return _wrapit(a, 'mean', axis, dtype, out) return mean(axis, dtype, out) np.average: ... if weights is None : avg = a.mean(axis) scl = avg.dtype.type(a.size/avg.size) else: #code that does weighted mean here if returned: #returned is another optional argument scl = np.multiply(avg, 0) + scl return avg, scl else: return avg ...
stackoverflow.com
**How to count the number of true elements in a NumPy bool array** **Top answer** You have multiple options. Two options are the following. boolarr.sum() numpy.count_nonzero(boolarr) Here's an example: >>> import numpy as np >>> boolarr = np.array([[0, 0, 1], [1, 0, 1], [1, 0, 1]], dtype=np.bool) >>> boolarr array([[False, False, True], [ True, False, True], [ True, False, True]], dtype=bool) >>> boolarr.sum() 5 Of course, that is a bool-specific answer. More generally, you can use numpy.count_nonzero. >>> np.count_nonzero(boolarr) 5
stackoverflow.com
**ImportError: numpy.core.multiarray failed to import** **Top answer** I was getting the same error and was able to solve it by updating my numpy installation to 1.8.0: pip install -U numpy
stackoverflow.com
**Find unique rows in numpy.array** **Top answer** As of NumPy 1.13, one can simply choose the axis for selection of unique values in any N-dim array. To get unique rows, use np.unique as follows: unique_rows = np.unique(original_array, axis=0)
stackoverflow.com
**Suppress Scientific Notation in Numpy When Creating Array From Nested List** **Top answer** This is what you need: np.set_printoptions(suppress=True) Here is the documentation which says suppress: bool, optional If True, always print floating point numbers using fixed point notation, in which case numbers equal to zero in the current precision will print as zero. If False, then scientific notation is used when absolute value of the smallest number is < 1e-4 or the ratio of the maximum absolute value to the minimum is > 1e3. The default is False. In the original question, the difference between the array created "directly" and the original "big" array is that the big array contains very large numbers (e.g. 1.44651157e+09), so NumPy chooses the scientific notation for it, unless it's suppressed.
stackoverflow.com
**How do you get the magnitude of a vector in Numpy?** **Top answer** The function you're after is numpy.linalg.norm. (I reckon it should be in base numpy as a property of an array -- say x.norm() -- but oh well). import numpy as np x = np.array([1,2,3,4,5]) np.linalg.norm(x) You can also feed in an optional ord for the nth order norm you want. Say you wanted the 1-norm: np.linalg.norm(x,ord=1) And so on.
stackoverflow.com
**Numpy - add row to array** **Top answer** You can do this: newrow = [1, 2, 3] A = numpy.vstack([A, newrow])
stackoverflow.com
**How to flatten only some dimensions of a numpy array** **Top answer** Take a look at numpy.reshape . >>> arr = numpy.zeros((50,100,25)) >>> arr.shape # (50, 100, 25) >>> new_arr = arr.reshape(5000,25) >>> new_arr.shape # (5000, 25) # One shape dimension can be -1. # In this case, the value is inferred from # the length of the array and remaining dimensions. >>> another_arr = arr.reshape(-1, arr.shape[-1]) >>> another_arr.shape # (5000, 25)
stackoverflow.com
**How to calculate rolling / moving average using python + NumPy / SciPy?** **Top answer** If you just want a straightforward non-weighted moving average, you can easily implement it with np.cumsum, which may be is faster than FFT based methods: EDIT Corrected an off-by-one wrong indexing spotted by Bean in the code. EDIT def moving_average(a, n=3): ret = np.cumsum(a, dtype=float) ret[n:] = ret[n:] - ret[:-n] return ret[n - 1:] / n >>> a = np.arange(20) >>> moving_average(a) array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18.]) >>> moving_average(a, n=4) array([ 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5]) So I guess the answer is: it is really easy to implement, and maybe numpy is already a little bloated with specialized functionality.
stackoverflow.com
**Python - TypeError: Object of type &#39;int64&#39; is not JSON serializable** **Top answer** You can define your own encoder to solve this problem. import json import numpy as np class NpEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.integer): return int(obj) if isinstance(obj, np.floating): return float(obj) if isinstance(obj, np.ndarray): return obj.tolist() return super(NpEncoder, self).default(obj) # Your codes .... json.dumps(data, cls=NpEncoder)
stackoverflow.com
**How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting** **Top answer** For fitting y = A + B log x, just fit y against (log x). >>> x = numpy.array([1, 7, 20, 50, 79]) >>> y = numpy.array([10, 19, 30, 35, 51]) >>> numpy.polyfit(numpy.log(x), y, 1) array([ 8.46295607, 6.61867463]) # y ≈ 8.46 log(x) + 6.62 For fitting y = AeBx, take the logarithm of both side gives log y = log A + Bx. So fit (log y) against x. Note that fitting (log y) as if it is linear will emphasize small values of y, causing large deviation for large y. This is because polyfit (linear regression) works by minimizing ∑i (ΔY)2 = ∑i (Yi − Ŷi)2. When Yi = log yi, the residues ΔYi = Δ(log yi) ≈ Δyi / |yi|. So even if polyfit makes a very bad decision for large y, the "divide-by-|y|" factor will compensate for it, causing polyfit favors small values. This could be alleviated by giving each entry a "weight" proportional
stackoverflow.com
**A column-vector y was passed when a 1d array was expected** **Top answer** Change this line: model = forest.fit(train_fold, train_y) to: model = forest.fit(train_fold, train_y.values.ravel()) Explanation: .values will give the values in a numpy array (shape: (n,1)) .ravel will convert that array shape to (n, ) (i.e. flatten it)
stackoverflow.com
**Concatenate a NumPy array to another NumPy array** **Top answer** In [1]: import numpy as np In [2]: a = np.array([[1, 2, 3], [4, 5, 6]]) In [3]: b = np.array([[9, 8, 7], [6, 5, 4]]) In [4]: np.concatenate((a, b)) Out[4]: array([[1, 2, 3], [4, 5, 6], [9, 8, 7], [6, 5, 4]]) or this: In [1]: a = np.array([1, 2, 3]) In [2]: b = np.array([4, 5, 6]) In [3]: np.vstack((a, b)) Out[3]: array([[1, 2, 3], [4, 5, 6]])
stackoverflow.com
**How do I catch a numpy warning like it&#39;s an exception (not just for testing)?** **Top answer** It seems that your configuration is using the print option for numpy.seterr: >>> import numpy as np >>> np.array([1])/0 #'warn' mode __main__:1: RuntimeWarning: divide by zero encountered in divide array([0]) >>> np.seterr(all='print') {'over': 'warn', 'divide': 'warn', 'invalid': 'warn', 'under': 'ignore'} >>> np.array([1])/0 #'print' mode Warning: divide by zero encountered in divide array([0]) This means that the warning you see is not a real warning, but it's just some characters printed to stdout(see the documentation for seterr). If you want to catch it you can: Use numpy.seterr(all='raise') which will directly raise the exception. This however changes the behaviour of all the operations, so it's a pretty big change in behaviour. Use numpy.seterr(all='warn'), which will transform the printed warning in a real warni
stackoverflow.com
**&quot;Cloning&quot; row or column vectors** **Top answer** Use numpy.tile: >>> tile(array([1,2,3]), (3, 1)) array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) or for repeating columns: >>> tile(array([[1,2,3]]).transpose(), (1, 3)) array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
stackoverflow.com
**NumPy or Pandas: Keeping array type as integer while having a NaN value** **Top answer** NaN can't be stored in an integer array. This is a known limitation of pandas at the moment; I have been waiting for progress to be made with NA values in NumPy (similar to NAs in R), but it will be at least 6 months to a year before NumPy gets these features, it seems: http://pandas.pydata.org/pandas-docs/stable/gotchas.html#support-for-integer-na (This feature has been added beginning with version 0.24 of pandas, but note it requires the use of extension dtype Int64 (capitalized), rather than the default dtype int64 (lower case): https://pandas.pydata.org/pandas-docs/version/0.24/whatsnew/v0.24.0.html#optional-integer-na-support )
stackoverflow.com
**Extracting specific columns in numpy array** **Top answer** I assume you wanted columns 1 and 9? To select multiple columns at once, use X = data[:, [1, 9]] To select one at a time, use x, y = data[:, 1], data[:, 9] With names: data[:, ['Column Name1','Column Name2']] You can get the names from data.dtype.names…
stackoverflow.com
**Numpy first occurrence of value greater than existing value** **Top answer** This is a little faster (and looks nicer) np.argmax(aa>5) Since argmax will stop at the first True ("In case of multiple occurrences of the maximum values, the indices corresponding to the first occurrence are returned.") and doesn't save another list. In [2]: N = 10000 In [3]: aa = np.arange(-N,N) In [4]: timeit np.argmax(aa>N/2) 100000 loops, best of 3: 52.3 us per loop In [5]: timeit np.where(aa>N/2)[0][0] 10000 loops, best of 3: 141 us per loop In [6]: timeit np.nonzero(aa>N/2)[0][0] 10000 loops, best of 3: 142 us per loop
stackoverflow.com
**How to add a new row to an empty numpy array** **Top answer** The way to "start" the array that you want is: arr = np.empty((0,3), int) Which is an empty array but it has the proper dimensionality. >>> arr array([], shape=(0, 3), dtype=int64) Then be sure to append along axis 0: arr = np.append(arr, np.array([[1,2,3]]), axis=0) arr = np.append(arr, np.array([[4,5,6]]), axis=0) But, @jonrsharpe is right.
stackoverflow.com
**ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject** **Top answer** I'm in Python 3.8.5. It sounds too simple to be real, but I had this same issue and all I did was reinstall numpy. Gone. pip install --upgrade numpy or pip uninstall numpy pip install numpy
stackoverflow.com
**Numpy: Get random set of rows from 2D array** **Top answer** >>> A = np.random.randint(5, size=(10,3)) >>> A array([[1, 3, 0], [3, 2, 0], [0, 2, 1], [1, 1, 4], [3, 2, 2], [0, 1, 0], [1, 3, 1], [0, 4, 1], [2, 4, 2], [3, 3, 1]]) >>> idx = np.random.randint(10, size=2) >>> idx array([7, 6]) >>> A[idx,:] array([[0, 4, 1], [1, 3, 1]]) Putting it together for a general case: A[np.random.randint(A.shape[0], size=2), :] For non replacement (numpy 1.7.0+): A[np.random.choice(A.shape[0], 2, replace=False), :] I do not believe there is a good way to generate random list without replacement before 1.7. Perhaps you can setup a small definition that ensures the two values are not the same.
stackoverflow.com
**Python memory usage of numpy arrays** **Top answer** You can use array.nbytes for numpy arrays, for example: import numpy as np from sys import getsizeof a = [0] * 1024 b = np.array(a) print(getsizeof(a)) print(b.nbytes) Output: 8264 8192
stackoverflow.com
**How to install python modules without root access?** **Top answer** In most situations the best solution is to rely on the so-called "user site" location (see the PEP for details) by running: pip install --user package_name Below is a more "manual" way from my original answer, you do not need to read it if the above solution works for you. With easy_install you can do: easy_install --prefix=$HOME/local package_name which will install into $HOME/local/lib/pythonX.Y/site-packages (the 'local' folder is a typical name many people use, but of course you may specify any folder you have permissions to write into). You will need to manually create $HOME/local/lib/pythonX.Y/site-packages and add it to your PYTHONPATH environment variable (otherwise easy_install will complain -- btw run the command above once to find the correct value for X.Y). If you are not using easy_install, look for a prefix option, most install scripts let you specify one. With pip you can use: pip install --install-option="--prefix=$HOME/local" package_name
stackoverflow.com
**Numpy where function multiple conditions** **Top answer** The best way in your particular case would just be to change your two criteria to one criterion: dists[abs(dists - r - dr/2.) <= dr/2.] It only creates one boolean array, and in my opinion is easier to read because it says, is dist within a dr or r? (Though I'd redefine r to be the center of your region of interest instead of the beginning, so r = r + dr/2.) But that doesn't answer your question. The answer to your question: You don't actually need where if you're just trying to filter out the elements of dists that don't fit your criteria: dists[(dists >= r) & (dists <= r+dr)] Because the & will give you an elementwise and (the parentheses are necessary). Or, if you do want to use where
stackoverflow.com
**AttributeError: module &#39;pkgutil&#39; has no attribute &#39;ImpImporter&#39;. Did you mean: &#39;zipimporter&#39;?** **Top answer** Due to the removal of the long-deprecated pkgutil.ImpImporter class, the pip command may not work for Python 3.12. You just have to manually install pip for Python 3.12 python -m ensurepip --upgrade python -m pip install --upgrade setuptools python -m pip install <module> In your virtual environment: pip install --upgrade setuptools Python comes with an ensurepip, which can install pip in a Python environment. https://pip.pypa.io/en/stable/installation/ On Linux/macOS terminal: python -m ensurepip --upgrade On Windows: py -m ensurepip --upgrade also, make sure to upgrade pip: py -m pip install --upgrade pip To install numpy on Python 3.12, you must use numpy version 1.26.4 pip install numpy==1.26.4 https://github.com/numpy/numpy/issues/23808#issuecomment-1722440746 for Ubuntu sudo apt install python3.12-dev or python3.12 -m pip install --upgrade setuptools
stackoverflow.com
**Calculating Pearson correlation and significance in Python** **Top answer** You can have a look at scipy.stats: from pydoc import help from scipy.stats.stats import pearsonr help(pearsonr) Output: >>> Help on function pearsonr in module scipy.stats.stats: pearsonr(x, y) Calculates a Pearson correlation coefficient and the p-value for testing non-correlation. The Pearson correlation coefficient measures the linear relationship between two datasets. Strictly speaking, Pearson's correlation requires that each dataset be normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. Parameters ---------- x : 1D array y : 1D array the same length as x Returns ------- (Pearson's correlation coefficient, 2-tailed p-value) References ---------- http://www.statsoft.com/textbook/glosp.html#Pearson%20Correlation
stackoverflow.com
End of preview. Expand in Data Studio

No dataset card yet

Downloads last month
1