Question:
Which kind of interpolation best for resizing image? I have a numpy array that I wish to resize using opencv. Its values range from 0 to 255. If I opt to use cv2.INTER_CUBIC, I may get values outside this range. This is undesirable, since the resized array is supposed to still represent an image. One solution is to clip the results to [0, 255]. Another is to use a different interpolation method. It is my understanding that using INTER_AREA is valid for down-sampling an image, but works similar to nearest neighbor for upsampling it, rendering it less than optimal for my purpose. Should I use INTER_CUBIC (and clip), INTER_AREA, or INTER_LINEAR? an example for values outside of range using INTER_CUBIC:
```
a = np.array( [ 0, 10, 20, 0, 5, 2, 255, 0, 255 ] ).reshape( ( 3, 3 ) )
[[  0  10  20]
 [  0   5   2]
 [255   0 255]]

b = cv2.resize( a.astype('float'), ( 4, 4 ), interpolation = cv2.INTER_CUBIC )
[[   0.            5.42489886   15.43670964   21.29199219]
 [ -28.01513672   -2.46422291    1.62949324  -19.30908203]
 [  91.88964844   25.07939219   24.75106835   91.19140625]
 [ 273.30322266   68.20603609   68.13853455  273.15966797]]
```
Edit: As berak pointed out, converting the type to float (from int64) allows for values outside the original range. the cv2.resize() function does not work with the default 'int64' type. However, converting to 'uint8' will automatically saturate the values to [0..255]. Also, as pointed out by SaulloCastro, another related answer demonstrated scipy's interpolation, and that there the defualt method is the cubic interpolation (with saturation).

Response: If you are enlarging the image, you should prefer to use INTER_LINEAR or INTER_CUBIC interpolation. If you are shrinking the image, you should prefer to use INTER_AREA interpolation. Cubic interpolation is computationally more complex, and hence slower than linear interpolation. However, the quality of the resulting image will be higher.


Question:
Flatten numpy array Is there a simple way in NumPy to flatten type object array? I know .flatten() method flattens non-object type arrays constructed from same size arrays:
```
I1 a = np.array([[1],[2],[3]])

I2 a.flatten()
O2 array([1, 2, 3])
```
however, I can't get dtype=object array flattened:
```
I4 b
O4 array([[1], [2, 3], [3]], dtype=object)

I5 b.flatten()
O5 array([[1], [2, 3], [3]], dtype=object)
```
Thanks.

Response:
if you want [1,2,3,3], try this then
```
np.hstack(b)
```


Question: how to explain the decision tree from scikit-learn I have two problems with understanding the result of decision tree from scikit-learn. For example, this is one of my decision trees: My question is that how I can use the tree? The first question is that: if a sample satisfied the condition, then it goes to the LEFT branch (if exists), otherwise it goes RIGHT. In my case, if a sample with X[7] > 63521.3984. Then the sample will go to the green box. Correct? The second question is that: when a sample reaches the leaf node, how can I know which category it belongs? In this example, I have three categories to classify. In the red box, there are 91, 212, and 113 samples are satisfied the condition, respectively. But how can I decide the category? I know there is a function clf.predict(sample) to tell the category. Can I do that from the graph??? Many thanks.

Response: The value line in each box is telling you how many samples at that node fall into each category, in order. That's why, in each box, the numbers in value add up to the number shown in sample. For instance, in your red box, 91+212+113=416. So this means if you reach this node, there were 91 data points in category 1, 212 in category 2, and 113 in category 3. If you were going to predict the outcome for a new data point that reached that leaf in the decision tree, you would predict category 2, because that is the most common category for samples at that node.


Question:
Is the order of a Python dictionary guaranteed over iterations? I'm currently implementing a complex microbial food-web in Python using SciPy.integrate.ode. I need the ability to easily add species and reactions to the system, so I have to code up something quite general. My scheme looks something like this:
```
class Reaction(object):
    def __init__(self):
        #stuff common to all reactions
    def __getReactionRate(self, **kwargs):
        raise NotImplementedError

... Reaction subclasses that
... implement specific types of reactions

class Species(object):
    def __init__(self, reactionsDict):
        self.reactionsDict = reactionsDict
        #reactionsDict looks like {'ReactionName':reactionObject, ...}
        #stuff common to all species

    def sumOverAllReactionsForThisSpecies(self, **kwargs):
        #loop over all the reactions and return the
        #cumulative change in the concentrations of all solutes

...Species subclasses where for each species
... are defined and passed to the superclass constructor

class FermentationChamber(object):
    def __init__(self, speciesList, timeToSolve, *args):
        #do initialization

    def step(self):
        #loop over each species, which in turn loops
        #over each reaction inside it and return a
        #cumulative dictionary of total change for each
        #solute in the whole system

if __name__==__main__:
    f = FermentationChamber(...)

    o  = ode(...) #initialize ode solver

    while o.successful() and o.t<timeToSolve:
         o.integrate()

    #process o.t and o.y (o.t contains the time points
    #and o.y contains the solution matrix)
```
So, the question is, when I iterate over the dictionaries in Species.sumOverAllReactionsForThisSpecies() and FermentationChamber.step(), is the iteration order of the dictionaries guaranteed to be the same if no elements are added or removed from the dictionaries between the first and the last iteration? That is, can I assume that the order of the numpy array created at each iteration from the dictionary will not vary? For example, if a dictionary has the format {'Glucose':10, 'Fructose':12}, if an Array created from this dictionary will always have the same order (it doesn't matter what that order is, as long as it's deterministic). Sorry for the mega-post, I just wanted to let you know where I'm coming from.

Response:
Yes, the same order is guaranteed if it is not modified. See the docs here. Edit: Regarding if changing the value (but not adding/removing a key) will affect the order, this is what the comments in the C-source says:
```c
/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
 * dictionary if it's merely replacing the value for an existing key.
 * This means that it's safe to loop over a dictionary with PyDict_Next()
 * and occasionally replace a value -- but you can't insert new keys or
 * remove them.
 */
```
It seems that its not an implementation detail, but a requirement of the language.


Question: How do I calculate percentiles with python/numpy? 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.

Response:
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.
```
```

```python
print(p)
#Output
#3.0
#```
#SciPy has scipy.stats.scoreatpercentile(), in addition to many other statistical goodies.
```


Question:
Insert element into numpy array Lists have a very simple method to insert elements:
```
a = [1,2,3,4]
a.insert(2,66)
print a
[1, 2, 66, 3, 4]
```
For a numpy array I could do:
```
a = np.asarray([1,2,3,4])
a_l = a.tolist()
a_l.insert(2,66)
a = np.asarray(a_l)
print a
[1 2 66 3 4]
```
but this is very convoluted. Is there an insert equivalent for numpy arrays?

Response:
You can use numpy.insert, though unlike list.insert it returns a new array because arrays in NumPy have fixed size.
```

```python
import numpy as np
```

```python
a = np.asarray([1,2,3,4])
```

```python
np.insert(a, 2, 66)
#Output
#array([ 1,  2, 66,  3,  4])
#```
```


Question:
How can I get descriptive statistics of a NumPy array? I use the following code to create a numpy-ndarray. The file has 9 columns. I explicitly type each column:
```
dataset = np.genfromtxt("data.csv", delimiter=",",dtype=('|S1', float, float,float,float,float,float,float,int))
```
Now I would like to get some descriptive statistics for each column (min, max, stdev, mean, median, etc.). Shouldn't there be an easy way to do this? I tried this:
```
from scipy import stats
stats.describe(dataset)
```
but this returns an error: TypeError: cannot perform reduce with flexible type How can I get descriptive statistics of the created NumPy array?

Response:
```
import pandas as pd
import numpy as np

df_describe = pd.DataFrame(dataset)
df_describe.describe()
```
please note that dataset is your np.array to describe.
```
import pandas as pd
import numpy as np

df_describe = pd.DataFrame('your np.array')
df_describe.describe()
```


Question:
Computing cross-correlation function? In R, I am using ccf or acf to compute the pair-wise cross-correlation function so that I can find out which shift gives me the maximum value. From the looks of it, R gives me a normalized sequence of values. Is there something similar in Python's scipy or am I supposed to do it using the fft module? Currently, I am doing it as follows:
```
xcorr = lambda x,y : irfft(rfft(x)*rfft(y[::-1]))
x = numpy.array([0,0,1,1])
y = numpy.array([1,1,0,0])
print xcorr(x,y)
```

Response: To cross-correlate 1d arrays use numpy.correlate. For 2d arrays, use scipy.signal.correlate2d. There is also scipy.stsci.convolve.correlate2d. There is also matplotlib.pyplot.xcorr which is based on numpy.correlate. See this post on the SciPy mailing list for some links to different implementations. Edit: @user333700 added a link to the SciPy ticket for this issue in a comment.


Question:
How can I retrieve the current seed of NumPy's random number generator? The following imports NumPy and sets the seed.
```
import numpy as np
np.random.seed(42)
```
However, I'm not interested in setting the seed but more in reading it. random.get_state() does not seem to contain the seed. The documentation doesn't show an obvious answer. How do I retrieve the current seed used by numpy.random, assuming I did not set it manually? I want to use the current seed to carry over for the next iteration of a process.

Response:
The short answer is that you simply can't (at least not in general). The Mersenne Twister RNG used by numpy has 219937-1 possible internal states, whereas a single 64 bit integer has only 264 possible values. It's therefore impossible to map every RNG state to a unique integer seed. You can get and set the internal state of the RNG directly using np.random.get_state and np.random.set_state. The output of get_state is a tuple whose second element is a (624,) array of 32 bit integers. This array has more than enough bits to represent every possible internal state of the RNG (2624 * 32 > 219937-1). The tuple returned by get_state can be used much like a seed in order to create reproducible sequences of random numbers. For example:
```
import numpy as np

# randomly initialize the RNG from some platform-dependent source of entropy
np.random.seed(None)

# get the initial state of the RNG
st0 = np.random.get_state()

# draw some random numbers
print(np.random.randint(0, 100, 10))
# [ 8 76 76 33 77 26  3  1 68 21]

# set the state back to what it was originally
np.random.set_state(st0)

# draw again
print(np.random.randint(0, 100, 10))
# [ 8 76 76 33 77 26  3  1 68 21]
```


Question:
numpy get index where value is true
```

```python
ex=np.arange(30)
```

```python
e=np.reshape(ex,[3,10])
```

```python
e
#Output
#array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
#       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
#       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])
```

```python
e>15
#Output
#array([[False, False, False, False, False, False, False, False, False,
#        False],
#       [False, False, False, False, False, False,  True,  True,  True,
#         True],
#       [ True,  True,  True,  True,  True,  True,  True,  True,  True,
#         True]], dtype=bool)
#```
#I need to find the rows that have true or rows in e whose value are more than 15. I could iterate using a for loop, however, I would like to know if there is a way numpy could do this more efficiently?
```

Response:
You can use the nonzero function. it returns the nonzero indices of the given input. Easy Way
```

```python
(e > 15).nonzero()
```

(array([1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]), array([6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
```
to see the indices more cleaner, use transpose method:
```

```python
numpy.transpose((e>15).nonzero())
```

[[1 6]
 [1 7]
 [1 8]
 [1 9]
 [2 0]
 ...
```
Not Bad Way
```

```python
numpy.nonzero(e > 15)
```

(array([1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]), array([6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
```
or the clean way:
```

```python
numpy.transpose(numpy.nonzero(e > 15))
```

[[1 6]
 [1 7]
 [1 8]
 [1 9]
 [2 0]
 ...
```


Question: Find the min/max excluding zeros in a numpy array (or a tuple) in python I have an array. The valid values are not zero (either positive or negetive). I want to find the minimum and maximum within the array which should not take zeros into account. For example if the numbers are only negative. Zeros will be problematic.

Response:
How about:
```
import numpy as np
minval = np.min(a[np.nonzero(a)])
maxval = np.max(a[np.nonzero(a)])
```
where a is your array.


Question: When should I use hstack/vstack vs append vs concatenate vs column_stack? Simple question: what is the advantage of each of these methods. It seems that given the right parameters (and ndarray shapes) they all work seemingly equivalently. Do some work in place? Have better performance? Which functions should I use when?

Response: If you have two matrices, you're good to go with just hstack and vstack: If you're stacking a matrix and a vector, hstack becomes tricky to use, so column_stack is a better option: If you're stacking two vectors, you've got three options: And concatenate in its raw form is useful for 3D and above, see my article Numpy Illustrated for details.


Question:
Indexing numpy array with another numpy array Suppose I have
```
a = array([[1, 2],
           [3, 4]])
```
and
```
b = array([1,1])
```
I'd like to use b in index a, that is to do a[b] and get 4 instead of [[3, 4], [3, 4]] I can probably do
```
a[tuple(b)]
```
Is there a better way of doing it? Thanks

Response:
According the NumPy tutorial, the correct way to do it is:
```py
a[tuple(b)]
```


Question:
Concat DataFrame Reindexing only valid with uniquely valued Index objects I am trying to concat the following dataframes: df1
```
price   side timestamp
timestamp
2016-01-04 00:01:15.631331072   0.7286  2   1451865675631331
2016-01-04 00:01:15.631399936   0.7286  2   1451865675631400
2016-01-04 00:01:15.631860992   0.7286  2   1451865675631861
2016-01-04 00:01:15.631866112   0.7286  2   1451865675631866
```
and: df2
```
bid     bid_size offer  offer_size
timestamp
2016-01-04 00:00:31.331441920   0.7284  4000000 0.7285  1000000
2016-01-04 00:00:53.631324928   0.7284  4000000 0.7290  4000000
2016-01-04 00:01:03.131234048   0.7284  5000000 0.7286  4000000
2016-01-04 00:01:12.131444992   0.7285  1000000 0.7286  4000000
2016-01-04 00:01:15.631364096   0.7285  4000000 0.7290  4000000
```
With
```
data = pd.concat([df1,df2], axis=1)
```
But I get the follwing output:
```
InvalidIndexError                         Traceback (most recent call last)
<ipython-input-38-2e88458f01d7> in <module>()
----> 1 data = pd.concat([df1,df2], axis=1)
      2 data = data.fillna(method='pad')
      3 data = data.fillna(method='bfill')
      4 data['timestamp'] =  data.index.values#converting to datetime
      5 data['timestamp'] = pd.to_datetime(data['timestamp'])#converting to datetime

/usr/local/lib/python2.7/site-packages/pandas/tools/merge.pyc in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
    810                        keys=keys, levels=levels, names=names,
    811                        verify_integrity=verify_integrity,
--> 812                        copy=copy)
    813     return op.get_result()
    814

/usr/local/lib/python2.7/site-packages/pandas/tools/merge.pyc in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity, copy)
    947         self.copy = copy
    948
--> 949         self.new_axes = self._get_new_axes()
    950
    951     def get_result(self):

/usr/local/lib/python2.7/site-packages/pandas/tools/merge.pyc in _get_new_axes(self)
   1013                 if i == self.axis:
   1014                     continue
-> 1015                 new_axes[i] = self._get_comb_axis(i)
   1016         else:
   1017             if len(self.join_axes) != ndim - 1:

/usr/local/lib/python2.7/site-packages/pandas/tools/merge.pyc in _get_comb_axis(self, i)
   1039                 raise TypeError("Cannot concatenate list of %s" % types)
   1040
-> 1041         return _get_combined_index(all_indexes, intersect=self.intersect)
   1042
   1043     def _get_concat_axis(self):

/usr/local/lib/python2.7/site-packages/pandas/core/index.pyc in _get_combined_index(indexes, intersect)
   6120             index = index.intersection(other)
   6121         return index
-> 6122     union = _union_indexes(indexes)
   6123     return _ensure_index(union)
   6124

/usr/local/lib/python2.7/site-packages/pandas/core/index.pyc in _union_indexes(indexes)
   6149
   6150         if hasattr(result, 'union_many'):
-> 6151             return result.union_many(indexes[1:])
   6152         else:
   6153             for other in indexes[1:]:

/usr/local/lib/python2.7/site-packages/pandas/tseries/index.pyc in union_many(self, others)
    959             else:
    960                 tz = this.tz
--> 961                 this = Index.union(this, other)
    962                 if isinstance(this, DatetimeIndex):
    963                     this.tz = tz

/usr/local/lib/python2.7/site-packages/pandas/core/index.pyc in union(self, other)
   1553                 result.extend([x for x in other._values if x not in value_set])
   1554         else:
-> 1555             indexer = self.get_indexer(other)
   1556             indexer, = (indexer == -1).nonzero()
   1557

/usr/local/lib/python2.7/site-packages/pandas/core/index.pyc in get_indexer(self, target, method, limit, tolerance)
   1890
   1891         if not self.is_unique:
-> 1892             raise InvalidIndexError('Reindexing only valid with uniquely'
   1893                                     ' valued Index objects')
   1894

InvalidIndexError: Reindexing only valid with uniquely valued Index objects
```
I have removed additional columns and removed duplicates and NA where there could be a conflict - but I simply do not know what's wrong.

Response: Duplicated column names! In my case the problem was because I had duplicated column names.


Question:
How to "embed" a small numpy array into a predefined block of a large numpy array? I have a small NXN array "block" that I want to plug into a specified region (i.e., a diagonal region at "start") of a large array "wall". Is there an efficient method to achieve this?
```
wall[start:start+N][start:start+N] = block[:][:]
```
currently what I am doing is simply:
```
for i in xrange(N):
    wall[start+i][start:start+N] = block[i][:]
```

Response:
you can use multi dimension index:
```
import numpy as np

wall = np.zeros((10,10),dtype=np.int)
block = np.arange(1,7).reshape(2,3)

x = 2
y = 3
wall[x:x+block.shape[0], y:y+block.shape[1]] = block
```
the output is:
```

```python
wall
#Output
#array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#       [0, 0, 0, 1, 2, 3, 0, 0, 0, 0],
#       [0, 0, 0, 4, 5, 6, 0, 0, 0, 0],
#       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
#```
```


Question: Interpolation over an irregular grid So, I have three numpy arrays which store latitude, longitude, and some property value on a grid -- that is, I have LAT(y,x), LON(y,x), and, say temperature T(y,x), for some limits of x and y. The grid isn't necessarily regular -- in fact, it's tripolar. I then want to interpolate these property (temperature) values onto a bunch of different lat/lon points (stored as lat1(t), lon1(t), for about 10,000 t...) which do not fall on the actual grid points. I've tried matplotlib.mlab.griddata, but that takes far too long (it's not really designed for what I'm doing, after all). I've also tried scipy.interpolate.interp2d, but I get a MemoryError (my grids are about 400x400). Is there any sort of slick, preferably fast way of doing this? I can't help but think the answer is something obvious... Thanks!!

Response:
There is a nice inverse distance example by Roger Veciana i Rovira along with some code using GDAL to write to geotiff if you're into that. This is of coarse to a regular grid, but assuming you project the data first to a pixel grid with pyproj or something, all the while being careful what projection is used for your data. A copy of his algorithm and example script:
```
from math import pow
from math import sqrt
import numpy as np
import matplotlib.pyplot as plt

def pointValue(x,y,power,smoothing,xv,yv,values):
    nominator=0
    denominator=0
    for i in range(0,len(values)):
        dist = sqrt((x-xv[i])*(x-xv[i])+(y-yv[i])*(y-yv[i])+smoothing*smoothing);
        #If the point is really close to one of the data points, return the data point value to avoid singularities
        if(dist<0.0000000001):
            return values[i]
        nominator=nominator+(values[i]/pow(dist,power))
        denominator=denominator+(1/pow(dist,power))
    #Return NODATA if the denominator is zero
    if denominator > 0:
        value = nominator/denominator
    else:
        value = -9999
    return value

def invDist(xv,yv,values,xsize=100,ysize=100,power=2,smoothing=0):
    valuesGrid = np.zeros((ysize,xsize))
    for x in range(0,xsize):
        for y in range(0,ysize):
            valuesGrid[y][x] = pointValue(x,y,power,smoothing,xv,yv,values)
    return valuesGrid

if __name__ == "__main__":
    power=1
    smoothing=20

    #Creating some data, with each coodinate and the values stored in separated lists
    xv = [10,60,40,70,10,50,20,70,30,60]
    yv = [10,20,30,30,40,50,60,70,80,90]
    values = [1,2,2,3,4,6,7,7,8,10]

    #Creating the output grid (100x100, in the example)
    ti = np.linspace(0, 100, 100)
    XI, YI = np.meshgrid(ti, ti)

    #Creating the interpolation function and populating the output matrix value
    ZI = invDist(xv,yv,values,100,100,power,smoothing)

    # Plotting the result
    n = plt.normalize(0.0, 100.0)
    plt.subplot(1, 1, 1)
    plt.pcolor(XI, YI, ZI)
    plt.scatter(xv, yv, 100, values)
    plt.title('Inv dist interpolation - power: ' + str(power) + ' smoothing: ' + str(smoothing))
    plt.xlim(0, 100)
    plt.ylim(0, 100)
    plt.colorbar()

    plt.show()
```


Question:
How do I create an array whose elements are all equal to a specified value? How do I create an array where every entry is the same value? I know numpy.ones() and numpy.zeros() do this for 1's and 0's, but what about -1? For example:
```
>>import numpy as np
>>np.zeros((3,3))
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

>>np.ones((2,5))
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])

>>np.negative_ones((2,5))
???
```

Response:
I don't know if there's a nice one-liner without an arithmetic operation, but probably the fastest approach is to create an uninitialized array using empty and then use .fill() to set the values. For comparison:
```

```python
timeit m = np.zeros((3,3)); m += -1
#Output
#100000 loops, best of 3: 6.9 us per loop
```

```python
timeit m = np.ones((3,3)); m *= -1
#Output
#100000 loops, best of 3: 9.49 us per loop
```

```python
timeit m = np.zeros((3,3)); m.fill(-1)
#Output
#100000 loops, best of 3: 2.31 us per loop
```

```python
timeit m = np.empty((3,3)); m[:] = -1
#Output
#100000 loops, best of 3: 3.18 us per loop
```

```python
timeit m = np.empty((3,3)); m.fill(-1)
#Output
#100000 loops, best of 3: 2.09 us per loop
#```
#but to be honest, I tend to either add to the zero matrix or multiply the ones matrix instead, as initialization is seldom a bottleneck.
```


Question:
Type hint for NumPy ndarray dtype? I would like a function to include a type hint for NumPy ndarray's alongside with its dtype. With lists, for example, one could do the following...
```
def foo(bar: List[int]):
   ...
```
...in order to give a type hint that bar has to be list consisting of int's. Unfortunately, this syntax throws exceptions for NumPy ndarray:
```
def foo(bar: np.ndarray[np.bool]):
   ...

> np.ndarray[np.bool]) (...) TypeError: 'type' object is not subscriptable
```
Is it possible to give dtype-specific type hints for np.ndarray?

Response:
You could check out nptyping:
```py
from nptyping import NDArray, Bool

def foo(bar: NDArray[Bool]):
   ...
```
Or you could just use strings for type hints:
```py
def foo(bar: 'np.ndarray[np.bool]'):
   ...
```


Question: numpy unique without sort [duplicate] This question already has answers here: numpy.unique with order preserved (7 answers) Closed 8 years ago. How can I use numpy unique without sorting the result but just in the order they appear in the sequence? Something like this? a = [4,2,1,3,1,2,3,4] np.unique(a) = [4,2,1,3] rather than np.unique(a) = [1,2,3,4] Use naive solution should be fine to write a simple function. But as I need to do this multiple times, are there any fast and neat way to do this?

Response:
You can do this with the return_index parameter:
```py

```python
import numpy as np
```

```python
a = [4,2,1,3,1,2,3,4]
```

```python
np.unique(a)
#Output
#array([1, 2, 3, 4])
```

```python
indexes = np.unique(a, return_index=True)[1]
```

```python
[a[index] for index in sorted(indexes)]
#Output
#[4, 2, 1, 3]
#```
```


Question: How to create random orthonormal matrix in python numpy Is there a method that I can call to create a random orthonormal matrix in python? Possibly using numpy? Or is there a way to create a orthonormal matrix using multiple numpy methods? Thanks.

Response:
Version 0.18 of scipy has scipy.stats.ortho_group and scipy.stats.special_ortho_group. The pull request where it was added is https://github.com/scipy/scipy/pull/5622 For example,
```

```python
from scipy.stats import ortho_group  # Requires version 0.18 of scipy
```

```python
m = ortho_group.rvs(dim=3)
```

```python
m
#Output
#array([[-0.23939017,  0.58743526, -0.77305379],
#       [ 0.81921268, -0.30515101, -0.48556508],
#       [-0.52113619, -0.74953498, -0.40818426]])
```

```python
np.set_printoptions(suppress=True)
```

```python
m.dot(m.T)
#Output
#array([[ 1.,  0., -0.],
#       [ 0.,  1.,  0.],
#       [-0.,  0.,  1.]])
#```
```


Question:
Is there a Python equivalent of range(n) for multidimensional ranges? On Python, range(3) will return [0,1,2]. Is there an equivalent for multidimensional ranges?
```
range((3,2)) # [(0,0),(0,1),(1,0),(1,1),(2,0),(2,1)]
```
So, for example, looping though the tiles of a rectangular area on a tile-based game could be written as:
```
for x,y in range((3,2)):
```
Note I'm not asking for an implementation. I would like to know if this is a recognized pattern and if there is a built-in function on Python or it's standard/common libraries.

Response:
In numpy, it's numpy.ndindex. Also have a look at numpy.ndenumerate. E.g.
```
import numpy as np
for x, y in np.ndindex((3,2)):
    print(x, y)
```
This yields:
```
0 0
0 1
1 0
1 1
2 0
2 1
```


Question:
how to perform max/mean pooling on a 2d array using numpy Given a 2D(M x N) matrix, and a 2D Kernel(K x L), how do i return a matrix that is the result of max or mean pooling using the given kernel over the image? I'd like to use numpy if possible. Note: M, N, K, L can be both even or odd and they need not be perfectly divisible by each other, eg: 7x5 matrix and 2x2 kernel. eg of max pooling:
```
matrix:
array([[  20,  200,   -5,   23],
       [ -13,  134,  119,  100],
       [ 120,   32,   49,   25],
       [-120,   12,   09,   23]])
kernel: 2 x 2
soln:
array([[  200,  119],
       [  120,   49]])
```

Response:
You could use scikit-image block_reduce:
```py
import numpy as np
import skimage.measure

a = np.array([
      [  20,  200,   -5,   23],
      [ -13,  134,  119,  100],
      [ 120,   32,   49,   25],
      [-120,   12,    9,   23]
])
skimage.measure.block_reduce(a, (2,2), np.max)
```
Gives:
```none
array([[200, 119],
       [120,  49]])
```


Question:
Multiple conditions using 'or' in numpy array So I have these conditions: A = 0 to 10 OR 40 to 60 B = 20 to 50 and I have this code:
```
area1 = N.where((A>0) & (A<10)),1,0)
area2 = N.where((B>20) & (B<50)),1,0)
```
My question is: how do I do 'OR' condition in numpy?

Response:
If numpy overloads & for boolean and you can safely assume that | is boolean or.
```
area1 = N.where(((A>0) & (A<10)) | ((A>40) & (A<60))),1,0)
```


Question: Numpy meshgrid in 3D Numpy's meshgrid is very useful for converting two vectors to a coordinate grid. What is the easiest way to extend this to three dimensions? So given three vectors x, y, and z, construct 3x3D arrays (instead of 2x2D arrays) which can be used as coordinates.

Response:
Numpy (as of 1.8 I think) now supports higher that 2D generation of position grids with meshgrid. One important addition which really helped me is the ability to chose the indexing order (either xy or ij for Cartesian or matrix indexing respectively), which I verified with the following example:
```
import numpy as np

x_ = np.linspace(0., 1., 10)
y_ = np.linspace(1., 2., 20)
z_ = np.linspace(3., 4., 30)

x, y, z = np.meshgrid(x_, y_, z_, indexing='ij')

assert np.all(x[:,0,0] == x_)
assert np.all(y[0,:,0] == y_)
assert np.all(z[0,0,:] == z_)
```


Question: numpy array: replace nan values with average of columns I've got a numpy array filled mostly with real numbers, but there is a few nan values in it as well. How can I replace the nans with averages of columns where they are?

Response:
No loops required:
```
print(a)
[[ 0.93230948         nan  0.47773439  0.76998063]
 [ 0.94460779  0.87882456  0.79615838  0.56282885]
 [ 0.94272934  0.48615268  0.06196785         nan]
 [ 0.64940216  0.74414127         nan         nan]]

#Obtain mean of columns as you need, nanmean is convenient.
col_mean = np.nanmean(a, axis=0)
print(col_mean)
[ 0.86726219  0.7030395   0.44528687  0.66640474]

#Find indices that you need to replace
inds = np.where(np.isnan(a))

#Place column means in the indices. Align the arrays using take
a[inds] = np.take(col_mean, inds[1])

print(a)
[[ 0.93230948  0.7030395   0.47773439  0.76998063]
 [ 0.94460779  0.87882456  0.79615838  0.56282885]
 [ 0.94272934  0.48615268  0.06196785  0.66640474]
 [ 0.64940216  0.74414127  0.44528687  0.66640474]]
```


Question: Convert 2D float array to 2D int array in NumPy How do I convert a float NumPy array into an int NumPy array?

Response:
Use the astype method.
```

```python
x = np.array([[1.0, 2.3], [1.3, 2.9]])
```

```python
x
#Output
#array([[ 1. ,  2.3],
#       [ 1.3,  2.9]])
```

```python
x.astype(int)
#Output
#array([[1, 2],
#       [1, 2]])
#```
```


Question:
fminunc alternate in numpy Is there an alternative to the fminunc function (from octave/matlab) in python? I have a cost function for a binary classifier. Now I want to run gradient descent to get minimum value of theta. The octave/matlab implementation will look like this.
```
%  Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);

%  Run fminunc to obtain the optimal theta
%  This function will return theta and the cost
[theta, cost] = ...
    fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
```
I have converted my costFunction in python using numpy library, and looking for the fminunc or any other gradient descent algorithm implementation in numpy.

Response: There is more information about the functions of interest here: http://docs.scipy.org/doc/scipy-0.10.0/reference/tutorial/optimize.html Also, it looks like you are doing the Coursera Machine Learning course, but in Python. You might check out http://aimotion.blogspot.com/2011/11/machine-learning-with-python-logistic.html; this guy's doing the same thing.


Question:
Start, End and Duration of Maximum Drawdown in Python Given a time series, I want to calculate the maximum drawdown, and I also want to locate the beginning and end points of the maximum drawdown so I can calculate the duration. I want to mark the beginning and end of the drawdown on a plot of the timeseries like this: So far I've got code to generate a random time series, and I've got code to calculate the max drawdown. If anyone knows how to identify the places where the drawdown begins and ends, I'd really appreciate it!
```
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# create random walk which I want to calculate maximum drawdown for:

T = 50
mu = 0.05
sigma = 0.2
S0 = 20
dt = 0.01
N = round(T/dt)
t = np.linspace(0, T, N)
W = np.random.standard_normal(size = N)
W = np.cumsum(W)*np.sqrt(dt) ### standard brownian motion ###
X = (mu-0.5*sigma**2)*t + sigma*W

S = S0*np.exp(X) ### geometric brownian motion ###
plt.plot(S)

# Max drawdown function

def max_drawdown(X):
    mdd = 0
    peak = X[0]
    for x in X:
        if x > peak:
            peak = x
        dd = (peak - x) / peak
        if dd > mdd:
            mdd = dd
    return mdd

drawSeries = max_drawdown(S)
MaxDD = abs(drawSeries.min()*100)
print MaxDD

plt.show()
```

Response:
Just find out where running maximum minus current value is largest:
```
n = 1000
xs = np.random.randn(n).cumsum()
i = np.argmax(np.maximum.accumulate(xs) - xs) # end of the period
j = np.argmax(xs[:i]) # start of period

plt.plot(xs)
plt.plot([i, j], [xs[i], xs[j]], 'o', color='Red', markersize=10)
```


Question:
Numpy array loss of dimension when masking I want to select certain elements of an array and perform a weighted average calculation based on the values. However, using a filter condition, destroys the original structure of the array. arr which was of shape (2, 2, 3, 2) is turned into a 1-dimensional array. This is of no use to me, as not all these elements need to be combined later on with each other (but subarrays of them). How can I avoid this flattening?
```

```python
arr = np.asarray([ [[[1, 11], [2, 22], [3, 33]], [[4, 44], [5, 55], [6, 66]]], [ [[7, 77], [8, 88], [9, 99]], [[0, 32], [1, 33], [2, 34] ]] ])
```

```python
arr
#Output
#array([[[[ 1, 11],
#         [ 2, 22],
#         [ 3, 33]],
```

[[ 4, 44],
         [ 5, 55],
         [ 6, 66]]],

       [[[ 7, 77],
         [ 8, 88],
         [ 9, 99]],

        [[ 0, 32],
         [ 1, 33],
         [ 2, 34]]]])

```python
arr.shape
#Output
#(2, 2, 3, 2)
```

```python
arr[arr>3]
#Output
#array([11, 22, 33,  4, 44,  5, 55,  6, 66,  7, 77,  8, 88,  9, 99, 32, 33,
#       34])
```

```python
arr[arr>3].shape
#Output
#(18,)
#```
```

Response:
Checkout numpy.where http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html To keep the same dimensionality you are going to need a fill value. In the example below I use 0, but you could also use np.nan
```
np.where(arr>3, arr, 0)
```
returns
```
array([[[[ 0, 11],
         [ 0, 22],
         [ 0, 33]],

        [[ 4, 44],
         [ 5, 55],
         [ 6, 66]]],

       [[[ 7, 77],
         [ 8, 88],
         [ 9, 99]],

        [[ 0, 32],
         [ 0, 33],
         [ 0, 34]]]])
```


Question:
Set numpy array elements to zero if they are above a specific threshold Say, I have a numpy array consists of 10 elements, for example: a = np.array([2, 23, 15, 7, 9, 11, 17, 19, 5, 3]) Now I want to efficiently set all a values higher than 10 to 0, so I'll get: [2, 0, 0, 7, 9, 0, 0, 0, 5, 3] Because I currently use a for loop, which is very slow:
```
# Zero values below "threshold value".
def flat_values(sig, tv):
    """
    :param sig: signal.
    :param tv: threshold value.
    :return:
    """
    for i in np.arange(np.size(sig)):
        if sig[i] < tv:
            sig[i] = 0
    return sig
```
How can I achieve that in the most efficient way, having in mind big arrays of, say, 10^6 elements?

Response:
```

```python
a = np.array([2, 23, 15, 7, 9, 11, 17, 19, 5, 3])
```

```python
a[a > 10] = 0
```

```python
a
#Output
#array([2, 0, 0, 7, 9, 0, 0, 0, 5, 3])
#```
```


Question:
convert nan value to zero I have a 2D numpy array. Some of the values in this array are NaN. I want to perform certain operations using this array. For example consider the array:
```
[[   0.   43.   67.    0.   38.]
 [ 100.   86.   96.  100.   94.]
 [  76.   79.   83.   89.   56.]
 [  88.   NaN   67.   89.   81.]
 [  94.   79.   67.   89.   69.]
 [  88.   79.   58.   72.   63.]
 [  76.   79.   71.   67.   56.]
 [  71.   71.   NaN   56.  100.]]
```
I am trying to take each row, one at a time, sort it in reversed order to get max 3 values from the row and take their average. The code I tried is:
```
# nparr is a 2D numpy array
for entry in nparr:
    sortedentry = sorted(entry, reverse=True)
    highest_3_values = sortedentry[:3]
    avg_highest_3 = float(sum(highest_3_values)) / 3
```
This does not work for rows containing NaN. My question is, is there a quick way to convert all NaN values to zero in the 2D numpy array so that I have no problems with sorting and other things I am trying to do.

Response:
Where A is your 2D array:
```
import numpy as np
A[np.isnan(A)] = 0
```
The function isnan produces a bool array indicating where the NaN values are. A boolean array can by used to index an array of the same shape. Think of it like a mask.


Question:
I have need the N minimum (index) values in a numpy array Hi I have an array with X amount of values in it I would like to locate the indexs of the ten smallest values. In this link they calculated the maximum effectively, How to get indices of N maximum values in a numpy array? however I cant comment on links yet so I'm having to repost the question. I'm not sure which indices i need to change to achieve the minimum and not the maximum values. This is their code
```

```python
import numpy as np
```

```python
arr = np.array([1, 3, 2, 4, 5])
```

```python
arr.argsort()[-3:][::-1]
#Output
#array([4, 3, 1])
#```
```

Response:
If you call
```
arr.argsort()[:3]
```
It will give you the indices of the 3 smallest elements.
```
array([0, 2, 1], dtype=int64)
```
So, for n, you should call
```
arr.argsort()[:n]
```


Question: What is the correct way to change image channel ordering between channels first and channels last? I can not for the life of me figure out how to switch the image ordering. images are read in (x,x,3) format, theano requires it to be in (3,x,x) format. I tried changing the order with numpy.array([img[:,:,i] for i in range(3)]) which i guess gets the job done, but it is both ugly and i can't figure out how to reverse it to get the original image back.

Response:
I agree with @Qualia 's comment, np.moveaxis(a, source, destination) is easier to understand. This does the job:
```
x = np.zeros((12, 12, 3))
x.shape
#yields:
(12, 12, 3)

x = np.moveaxis(x, -1, 0)
x.shape
#yields:
(3, 12, 12)
```


Question:
Roll rows of a matrix independently I have a matrix (2d numpy ndarray, to be precise):
```
A = np.array([[4, 0, 0],
              [1, 2, 3],
              [0, 0, 5]])
```
And I want to roll each row of A independently, according to roll values in another array:
```
r = np.array([2, 0, -1])
```
That is, I want to do this:
```
print np.array([np.roll(row, x) for row,x in zip(A, r)])

[[0 0 4]
 [1 2 3]
 [0 5 0]]
```
Is there a way to do this efficiently? Perhaps using fancy indexing tricks?

Response:
You can do it using advanced indexing. Whether or not it is the fastest way likely depends on the array size. For instance, for large rows, this may be slower than other methods.
```
rows, column_indices = np.ogrid[:A.shape[0], :A.shape[1]]

# Always use a negative shift, so that column_indices are valid.
# Alternative: r %= A.shape[1]
r[r < 0] += A.shape[1]
column_indices = column_indices - r[:, np.newaxis]

result = A[rows, column_indices]
```


Question:
Numpy: use reshape or newaxis to add dimensions Either ndarray.reshape or numpy.newaxis can be used to add a new dimension to an array. They both seem to create a view, is there any reason or advantage to use one instead of the other?
```

```python
b
#Output
#array([ 1.,  1.,  1.,  1.])
```

```python
c = b.reshape((1,4))
```

```python
c *= 2
```

```python
c
#Output
#array([[ 2.,  2.,  2.,  2.]])
```

```python
c.shape
#Output
#(1, 4)
```

```python
b
#Output
#array([ 2.,  2.,  2.,  2.])
```

```python
d = b[np.newaxis,...]
```

```python
d
#Output
#array([[ 2.,  2.,  2.,  2.]])
```

```python
d.shape
#Output
#(1, 4)
```

```python
d *= 2
```

```python
b
#Output
#array([ 4.,  4.,  4.,  4.])
```

```python
c
#Output
#array([[ 4.,  4.,  4.,  4.]])
```

```python
d
#Output
#array([[ 4.,  4.,  4.,  4.]])
#>>>
#```
#`
```

Response:
I don't see evidence of much difference. You could do a time test on very large arrays. Basically both fiddle with the shape, and possibly the strides. __array_interface__ is a nice way of accessing this information. For example:
```

```python
b.__array_interface__
#Output
#{'data': (162400368, False),
# 'descr': [('', '<f8')],
# 'shape': (5,),
# 'strides': None,
# 'typestr': '<f8',
# 'version': 3}
```

```python
b[None,:].__array_interface__
#Output
#{'data': (162400368, False),
# 'descr': [('', '<f8')],
# 'shape': (1, 5),
# 'strides': (0, 8),
# 'typestr': '<f8',
# 'version': 3}
```

```python
b.reshape(1,5).__array_interface__
#Output
#{'data': (162400368, False),
# 'descr': [('', '<f8')],
# 'shape': (1, 5),
# 'strides': None,
# 'typestr': '<f8',
# 'version': 3}
#```
#Both create a view, using the same data buffer as the original. Same shape, but reshape doesn't change the strides. reshape lets you specify the order. And .flags shows differences in the C_CONTIGUOUS flag. reshape may be faster because it is making fewer changes. But either way the operation shouldn't affect the time of larger calculations much. e.g. for large b
#```
```

```python
timeit np.outer(b.reshape(1,-1),b)
1 loops, best of 3: 288 ms per loop
```

```python
timeit np.outer(b[None,:],b)
1 loops, best of 3: 287 ms per loop
```
Interesting observation that: b.reshape(1,4).strides -> (32, 8) Here's my guess. .__array_interface__ is displaying an underlying attribute, and .strides is more like a property (though it may all be buried in C code). The default underlying value is None, and when needed for calculation (or display with .strides) it calculates it from the shape and item size. 32 is the distance to the end of the 1st row (4x8). np.ones((2,4)).strides has the same (32,8) (and None in __array_interface__. b[None,:] on the other hand is preparing the array for broadcasting. When broadcasted, existing values are used repeatedly. That's what the 0 in (0,8) does.
```
```

```python
b1=np.broadcast_arrays(b,np.zeros((2,1)))[0]
```

```python
b1.shape
#Output
#(2, 5000)
```

```python
b1.strides
#Output
#(0, 8)
```

```python
b1.__array_interface__
#Output
#{'data': (3023336880L, False),
# 'descr': [('', '<f8')],
# 'shape': (2, 5),
# 'strides': (0, 8),
# 'typestr': '<f8',
# 'version': 3}
#```
#b1 displays the same as np.ones((2,5)) but has only 5 items. np.broadcast_arrays is a function in /numpy/lib/stride_tricks.py. It uses as_strided from the same file. These functions directly play with the shape and strides attributes.
```


Question:
NumPy grouping using itertools.groupby performance I have many large (>35,000,000) lists of integers that will contain duplicates. I need to get a count for each integer in a list. The following code works, but seems slow. Can anyone else better the benchmark using Python and preferably NumPy?
```
def group():
    import numpy as np
    from itertools import groupby
    values = np.array(np.random.randint(0,1<<32, size=35000000), dtype='u4')
    values.sort()
    groups = ((k, len(list(g))) for k,g in groupby(values))
    index = np.fromiter(groups, dtype='u4,u2')

if __name__=='__main__':
    from timeit import Timer
    t = Timer("group()","from __main__ import group")
    print t.timeit(number=1)
```
which returns:
```
$ python bench.py
111.377498865
```
Based on responses:
```
def group_original():
    import numpy as np
    from itertools import groupby
    values = np.array(np.random.randint(0, 1<<32, size=35000000), dtype='u4')
    values.sort()
    groups = ((k, len(list(g))) for k,g in groupby(values))
    index = np.fromiter(groups, dtype='u4,u2')

def group_gnibbler():
    import numpy as np
    from itertools import groupby
    values = np.array(np.random.randint(0, 1<<32, size=35000000), dtype='u4')
    values.sort()
    groups = ((k,sum(1 for i in g)) for k,g in groupby(values))
    index = np.fromiter(groups, dtype='u4,u2')

def group_christophe():
    import numpy as np
    values = np.array(np.random.randint(0, 1<<32, size=35000000), dtype='u4')
    values.sort()
    counts=values.searchsorted(values, side='right') - values.searchsorted(values, side='left')
    index = np.zeros(len(values), dtype='u4,u2')
    index['f0'] = values
    index['f1'] = counts
    # Erroneous result!

def group_paul():
    import numpy as np
    values = np.array(np.random.randint(0, 1<<32, size=35000000), dtype='u4')
    values.sort()
    diff = np.concatenate(([1], np.diff(values)))
    idx = np.concatenate((np.where(diff)[0], [len(values)]))
    index = np.empty(len(idx)-1, dtype='u4,u2')
    index['f0'] = values[idx[:-1]]
    index['f1'] = np.diff(idx)

if __name__=='__main__':
    from timeit import Timer
    timings=[
                ("group_original", "Original"),
                ("group_gnibbler", "Gnibbler"),
                ("group_christophe", "Christophe"),
                ("group_paul", "Paul"),
            ]
    for method,title in timings:
        t = Timer("%s()"%method,"from __main__ import %s"%method)
        print "%s: %s secs"%(title, t.timeit(number=1))
```
which returns:
```none
$ python bench.py
Original: 113.385262966 secs
Gnibbler: 71.7464978695 secs
Christophe: 27.1690568924 secs
Paul: 9.06268405914 secs
```
Although Christophe gives incorrect results currently.

Response:
I get a three times improvement doing something like this:
```
def group():
    import numpy as np
    values = np.array(np.random.randint(0, 3298, size=35000000), dtype='u4')
    values.sort()
    dif = np.ones(values.shape, values.dtype)
    dif[1:] = np.diff(values)
    idx = np.where(dif>0)
    vals = values[idx]
    count = np.diff(idx)
```


Question:
How to select rows that do not start with some str in pandas? I want to select rows that the values do not start with some str. For example, I have a pandas df, and I want to select data do not start with t, and c. In this sample, the output should be mext1 and okl1.
```
import pandas as pd

df=pd.DataFrame({'col':['text1','mext1','cext1','okl1']})
df

    col
0   text1
1   mext1
2   cext1
3   okl1
```
I want this:
```
col
0   mext1
1   okl1
```

Response:
You can use the str accessor to get string functionality. The get method can grab a given index of the string.
```
df[~df.col.str.get(0).isin(['t', 'c'])]

     col
1  mext1
3   okl1
```
Looks like you can use startswith as well with a tuple (and not a list) of the values you want to exclude.
```
df[~df.col.str.startswith(('t', 'c'))]
```


Question:
Averaging over every n elements of a numpy array I have a numpy array. I want to create a new array which is the average over every consecutive triplet of elements. So the new array will be a third of the size as the original. As an example:
```
np.array([1,2,3,1,2,3,1,2,3])
```
should return the array:
```
np.array([2,2,2])
```
Can anyone suggest an efficient way of doing this? I'm drawing blanks.

Response:
If your array arr has a length divisible by 3:
```
np.mean(arr.reshape(-1, 3), axis=1)
```
Reshaping to a higher dimensional array and then performing some form of reduce operation on one of the additional dimensions is a staple of numpy programming.


Question:
What does np.r_ do (numpy)? Following code is taken from numpy function base on github
```
sa = sort(a[i:i+block])
n += np.r_[sa.searchsorted(bins[:-1], 'left'),
           sa.searchsorted(bins[-1], 'right')]
```
So I know that searchsorted finds the position in the array sa where the elements of bins would have to be inserted in order to keep sa sorted (left gives the index left of where we would insert the value and right the right index). What I don't understand is the whole construction around it meaning what is
```
np.r_[array,array]
```
What is np.r_?

Response:
It does row-wise merging. This post has some nice example:
```

```python
V = array([1,2,3,4,5,6])
```

```python
Y = array([7,8,9,10,11,12])
```

```python
np.r_[V[0:2],Y[0],V[3],Y[1:3],V[4:],Y[4:]]
#Output
#array([ 1,  2,  7,  4,  8,  9,  5,  6, 11, 12])
#```
#Read more about it in this , and in the documentation of numpy.
```


Question: Is there a performance difference between Numpy and Pandas? I've written a bunch of code on the assumption that I was going to use Numpy arrays. Turns out the data I am getting is loaded through Pandas. I remember now that I loaded it in Pandas because I was having some problems loading it in Numpy. I believe the data was just too large. Therefore I was wondering, is there a difference in computational ability when using Numpy vs Pandas? If Pandas is more efficient then I would rather rewrite all my code for Pandas but if there is no more efficiency then I'll just use a numpy array...

Response: There can be a significant performance difference, of an order of magnitude for multiplications and multiple orders of magnitude for indexing a few random values. I was actually wondering about the same thing and came across this interesting comparison: http://penandpants.com/2014/09/05/performance-of-pandas-series-vs-numpy-arrays/


Question:
getting the opposite diagonal of a numpy array So in numpy arrays there is the built in function for getting the diagonal indices, but I can't seem to figure out how to get the diagonal starting from the top right rather than top left. This is the normal code to get starting from the top left:
```

```python
import numpy as np
```

```python
array = np.arange(25).reshape(5,5)
```

```python
diagonal = np.diag_indices(5)
```

```python
array
#Output
#array([[ 0,  1,  2,  3,  4],
#   [ 5,  6,  7,  8,  9],
#   [10, 11, 12, 13, 14],
#   [15, 16, 17, 18, 19],
#   [20, 21, 22, 23, 24]])
```

```python
array[diagonal]
#Output
#array([ 0,  6, 12, 18, 24])
#```
#so what do I use if I want it to return:
#```
#array([ 4,  8, 12, 16, 20])
#```
```

Response:
There is
```

```python
np.diag(np.fliplr(array))
#Output
#array([ 4,  8, 12, 16, 20])
#```
#or
#```
```

```python
np.diag(np.rot90(array))
#Output
#array([ 4,  8, 12, 16, 20])
#```
#Of the two, np.diag(np.fliplr(array)) is faster:
#```
```

```python
%timeit np.diag(np.fliplr(array))
100000 loops, best of 3: 4.29 us per loop
```

```python
%timeit np.diag(np.rot90(array))
100000 loops, best of 3: 6.09 us per loop
```
```


Question:
Converting between datetime, Timestamp and datetime64 How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)? In the following code, I create a datetime, timestamp and datetime64 objects.
```
import datetime
import numpy as np
import pandas as pd
dt = datetime.datetime(2012, 5, 1)
# A strange way to extract a Timestamp object, there's surely a better way?
ts = pd.DatetimeIndex([dt])[0]
dt64 = np.datetime64(dt)

```python
dt
#Output
#datetime.datetime(2012, 5, 1, 0, 0)
```

```python
ts
#Output
#<Timestamp: 2012-05-01 00:00:00>
```

```python
dt64
#Output
#numpy.datetime64('2012-05-01T01:00:00.000000+0100')
#```
#Note: it's easy to get the datetime from the Timestamp:
#```
```

```python
ts.to_datetime()
#Output
#datetime.datetime(2012, 5, 1, 0, 0)
#```
#But how do we extract the datetime or Timestamp from a numpy.datetime64 (dt64)? . Update: a somewhat nasty example in my dataset (perhaps the motivating example) seems to be:
#```
#dt64 = numpy.datetime64('2002-06-28T01:00:00.000000000+0100')
#```
#which should be datetime.datetime(2002, 6, 28, 1, 0), and not a long (!) (1025222400000000000L)...
```

Response: You can just use the pd.Timestamp constructor. The following diagram may be useful for this and related questions.


Question:
How to deal with PyCharm's "Expected type X, got Y instead" When using PyCharm, Pycharm's code style inspection gives me the warning Expected type 'Union[ndarray, Iterable]', got 'float' instead in the editor if I write np.array(0.0). When I write np.array([0.0]) I get no warning. When coding
```
from scipy.special import expit
expit(0.0)
```
I get Expected type 'ndarray', got 'float' instead, while
```
expit(np.array([0.0]))
```
solves that. What I think Pycharm's code style inspection wants to tell me is there's a possibility of a type error, but I am not sure how I should react to that in the sense of good programming. Is PyCharm right to scold me and should I use the long versions or should I keep my short versions for readability and speed of coding? If I should not change my code to the long versions - can I get rid of the Pycharm's code style inspection warning, or is that a bad idea, because they may be correct in other cases, and I am not able to tune the warnings that specifically?

Response:
If you want to ignore it for that specific line, then you can put
```
# noinspection PyTypeChecker
```
above the line of code that is being marked.


Question:
How do you do natural logs (e.g. "ln()") with numpy in Python? Using numpy, how can I do the following:
```
ln(x)
```
Is it equivalent to:
```
np.log(x)
```
I apologise for such a seemingly trivial question, but my understanding of the difference between log and ln is that ln is logspace e?

Response: np.log is ln, whereas np.log10 is your standard base 10 log.


Question:
Get week start date (Monday) from a date column in Python (pandas)? I have seen a lot of posts about how you can do it with a date string but I am trying something for a dataframe column and haven't got any luck so far. My current method is : Get the weekday from 'myday' and then offset to get monday.
```
df['myday'] is column of dates.
mydays = pd.DatetimeIndex(df['myday']).weekday
df['week_start'] = pd.DatetimeIndex(df['myday']) - pd.DateOffset(days=mydays)
```
But I get TypeError: unsupported type for timedelta days component: numpy.ndarray How can I get week start date from a df column?

Response:
Another alternative:
```
df['week_start'] = df['myday'].dt.to_period('W').apply(lambda r: r.start_time)
```
This will set 'week_start' to be the first Monday before the time in 'myday'. You can choose different week starts via anchored offsets e.g. ’W-THU’ to start the week on Thursday instead. (Thanks @Henry Ecker for that suggestion)


Question:
Numpy slicing from variable I'm trying to slice a numpy array using a slice that is predefined in a variable. This works:
```
b = fromfunction(lambda x,y: 10*x+y, (5,4),dtype=int) # Just some matrix

b[1:3,1:3]
# Output:
# array([[11, 12],
#       [21, 22]])
```
But what I want to do is somthing like this:
```
slice = "1:3,1:3"
b[slice]
# Output:
# array([[11, 12],
#       [21, 22]])
```
It is not important to me what type the slice-variable has, I'm just using a string as an example. How do I save a slice-specifier like that?

Response:
You can use the built-in slice function
```
s = slice(1,3)
b[s,s]

ds = (s,s)
b[ds]
```


Question: changing the values of the diagonal of a matrix in numpy how can I change the values of the diagonal of a matrix in numpy? I checked Numpy modify ndarray diagonal, but the function there is not implemented in numpy v 1.3.0. lets say we have a np.array X and I want to set all values of the diagonal to 0.

Response: Did you try numpy.fill_diagonal? See the following answer and this discussion. Or the following from the documentation: https://numpy.org/doc/stable/reference/generated/numpy.fill_diagonal.html


Question:
Efficient method of calculating density of irregularly spaced points I am attempting to generate map overlay images that would assist in identifying hot-spots, that is areas on the map that have high density of data points. None of the approaches that I've tried are fast enough for my needs. Note: I forgot to mention that the algorithm should work well under both low and high zoom scenarios (or low and high data point density). I looked through numpy, pyplot and scipy libraries, and the closest I could find was numpy.histogram2d. As you can see in the image below, the histogram2d output is rather crude. (Each image includes points overlaying the heatmap for better understanding) My second attempt was to iterate over all the data points, and then calculate the hot-spot value as a function of distance. This produced a better looking image, however it is too slow to use in my application. Since it's O(n), it works ok with 100 points, but blows out when I use my actual dataset of 30000 points. My final attempt was to store the data in an KDTree, and use the nearest 5 points to calculate the hot-spot value. This algorithm is O(1), so much faster with large dataset. It's still not fast enough, it takes about 20 seconds to generate a 256x256 bitmap, and I would like this to happen in around 1 second time. Edit The boxsum smoothing solution provided by 6502 works well at all zoom levels and is much faster than my original methods. The gaussian filter solution suggested by Luke and Neil G is the fastest. You can see all four approaches below, using 1000 data points in total, at 3x zoom there are around 60 points visible. Complete code that generates my original 3 attempts, the boxsum smoothing solution provided by 6502 and gaussian filter suggested by Luke (improved to handle edges better and allow zooming in) is here:
```
import matplotlib
import numpy as np
from matplotlib.mlab import griddata
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import math
from scipy.spatial import KDTree
import time
import scipy.ndimage as ndi

def grid_density_kdtree(xl, yl, xi, yi, dfactor):
    zz = np.empty([len(xi),len(yi)], dtype=np.uint8)
    zipped = zip(xl, yl)
    kdtree = KDTree(zipped)
    for xci in range(0, len(xi)):
        xc = xi[xci]
        for yci in range(0, len(yi)):
            yc = yi[yci]
            density = 0.
            retvalset = kdtree.query((xc,yc), k=5)
            for dist in retvalset[0]:
                density = density + math.exp(-dfactor * pow(dist, 2)) / 5
            zz[yci][xci] = min(density, 1.0) * 255
    return zz

def grid_density(xl, yl, xi, yi):
    ximin, ximax = min(xi), max(xi)
    yimin, yimax = min(yi), max(yi)
    xxi,yyi = np.meshgrid(xi,yi)
    #zz = np.empty_like(xxi)
    zz = np.empty([len(xi),len(yi)])
    for xci in range(0, len(xi)):
        xc = xi[xci]
        for yci in range(0, len(yi)):
            yc = yi[yci]
            density = 0.
            for i in range(0,len(xl)):
                xd = math.fabs(xl[i] - xc)
                yd = math.fabs(yl[i] - yc)
                if xd < 1 and yd < 1:
                    dist = math.sqrt(math.pow(xd, 2) + math.pow(yd, 2))
                    density = density + math.exp(-5.0 * pow(dist, 2))
            zz[yci][xci] = density
    return zz

def boxsum(img, w, h, r):
    st = [0] * (w+1) * (h+1)
    for x in xrange(w):
        st[x+1] = st[x] + img[x]
    for y in xrange(h):
        st[(y+1)*(w+1)] = st[y*(w+1)] + img[y*w]
        for x in xrange(w):
            st[(y+1)*(w+1)+(x+1)] = st[(y+1)*(w+1)+x] + st[y*(w+1)+(x+1)] - st[y*(w+1)+x] + img[y*w+x]
    for y in xrange(h):
        y0 = max(0, y - r)
        y1 = min(h, y + r + 1)
        for x in xrange(w):
            x0 = max(0, x - r)
            x1 = min(w, x + r + 1)
            img[y*w+x] = st[y0*(w+1)+x0] + st[y1*(w+1)+x1] - st[y1*(w+1)+x0] - st[y0*(w+1)+x1]

def grid_density_boxsum(x0, y0, x1, y1, w, h, data):
    kx = (w - 1) / (x1 - x0)
    ky = (h - 1) / (y1 - y0)
    r = 15
    border = r * 2
    imgw = (w + 2 * border)
    imgh = (h + 2 * border)
    img = [0] * (imgw * imgh)
    for x, y in data:
        ix = int((x - x0) * kx) + border
        iy = int((y - y0) * ky) + border
        if 0 <= ix < imgw and 0 <= iy < imgh:
            img[iy * imgw + ix] += 1
    for p in xrange(4):
        boxsum(img, imgw, imgh, r)
    a = np.array(img).reshape(imgh,imgw)
    b = a[border:(border+h),border:(border+w)]
    return b

def grid_density_gaussian_filter(x0, y0, x1, y1, w, h, data):
    kx = (w - 1) / (x1 - x0)
    ky = (h - 1) / (y1 - y0)
    r = 20
    border = r
    imgw = (w + 2 * border)
    imgh = (h + 2 * border)
    img = np.zeros((imgh,imgw))
    for x, y in data:
        ix = int((x - x0) * kx) + border
        iy = int((y - y0) * ky) + border
        if 0 <= ix < imgw and 0 <= iy < imgh:
            img[iy][ix] += 1
    return ndi.gaussian_filter(img, (r,r))  ## gaussian convolution

def generate_graph():
    n = 1000
    # data points range
    data_ymin = -2.
    data_ymax = 2.
    data_xmin = -2.
    data_xmax = 2.
    # view area range
    view_ymin = -.5
    view_ymax = .5
    view_xmin = -.5
    view_xmax = .5
    # generate data
    xl = np.random.uniform(data_xmin, data_xmax, n)
    yl = np.random.uniform(data_ymin, data_ymax, n)
    zl = np.random.uniform(0, 1, n)

    # get visible data points
    xlvis = []
    ylvis = []
    for i in range(0,len(xl)):
        if view_xmin < xl[i] < view_xmax and view_ymin < yl[i] < view_ymax:
            xlvis.append(xl[i])
            ylvis.append(yl[i])

    fig = plt.figure()

    # plot histogram
    plt1 = fig.add_subplot(221)
    plt1.set_axis_off()
    t0 = time.clock()
    zd, xe, ye = np.histogram2d(yl, xl, bins=10, range=[[view_ymin, view_ymax],[view_xmin, view_xmax]], normed=True)
    plt.title('numpy.histogram2d - '+str(time.clock()-t0)+"sec")
    plt.imshow(zd, origin='lower', extent=[view_xmin, view_xmax, view_ymin, view_ymax])
    plt.scatter(xlvis, ylvis)

    # plot density calculated with kdtree
    plt2 = fig.add_subplot(222)
    plt2.set_axis_off()
    xi = np.linspace(view_xmin, view_xmax, 256)
    yi = np.linspace(view_ymin, view_ymax, 256)
    t0 = time.clock()
    zd = grid_density_kdtree(xl, yl, xi, yi, 70)
    plt.title('function of 5 nearest using kdtree\n'+str(time.clock()-t0)+"sec")
    cmap=cm.jet
    A = (cmap(zd/256.0)*255).astype(np.uint8)
    #A[:,:,3] = zd
    plt.imshow(A , origin='lower', extent=[view_xmin, view_xmax, view_ymin, view_ymax])
    plt.scatter(xlvis, ylvis)

    # gaussian filter
    plt3 = fig.add_subplot(223)
    plt3.set_axis_off()
    t0 = time.clock()
    zd = grid_density_gaussian_filter(view_xmin, view_ymin, view_xmax, view_ymax, 256, 256, zip(xl, yl))
    plt.title('ndi.gaussian_filter - '+str(time.clock()-t0)+"sec")
    plt.imshow(zd , origin='lower', extent=[view_xmin, view_xmax, view_ymin, view_ymax])
    plt.scatter(xlvis, ylvis)

    # boxsum smoothing
    plt3 = fig.add_subplot(224)
    plt3.set_axis_off()
    t0 = time.clock()
    zd = grid_density_boxsum(view_xmin, view_ymin, view_xmax, view_ymax, 256, 256, zip(xl, yl))
    plt.title('boxsum smoothing - '+str(time.clock()-t0)+"sec")
    plt.imshow(zd, origin='lower', extent=[view_xmin, view_xmax, view_ymin, view_ymax])
    plt.scatter(xlvis, ylvis)

if __name__=='__main__':
    generate_graph()
    plt.show()
```

Response:
This approach is along the lines of some previous answers: increment a pixel for each spot, then smooth the image with a gaussian filter. A 256x256 image runs in about 350ms on my 6-year-old laptop.
```
import numpy as np
import scipy.ndimage as ndi

data = np.random.rand(30000,2)           ## create random dataset
inds = (data * 255).astype('uint')       ## convert to indices

img = np.zeros((256,256))                ## blank image
for i in xrange(data.shape[0]):          ## draw pixels
    img[inds[i,0], inds[i,1]] += 1

img = ndi.gaussian_filter(img, (10,10))
```


Question:
Numpy - Replace a number with NaN I am looking to replace a number with NaN in numpy and am looking for a function like numpy.nan_to_num, except in reverse. The number is likely to change as different arrays are processed because each can have a uniquely define NoDataValue. I have seen people using dictionaries, but the arrays are large and filled with both positive and negative floats. I suspect that it is not efficient to try to load all of these into anything to create keys. I tried using the following but numpy requires that I use any() or all(). I realize that I need to iterate element wise, but hope that a built-in function can achieve this.
```py
def replaceNoData(scanBlock, NDV):
    for n, i in enumerate(array):
        if i == NDV:
            scanBlock[n] = numpy.nan
```
NDV is GDAL's no data value and array is a numpy array. Is a masked array the way to go perhaps?

Response:
```
A[A==NDV]=numpy.nan
```
A==NDV will produce a boolean array that can be used as an index for A


Question:
Obtaining values used in boxplot, using python and matplotlib I can draw a boxplot from data:
```
import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(100)
plt.boxplot(data)
```
Then, the box will range from the 25th-percentile to 75th-percentile, and the whisker will range from the smallest value to the largest value between (25th-percentile - 1.5*IQR, 75th-percentile + 1.5*IQR), where the IQR denotes the inter-quartile range. (Of course, the value 1.5 is customizable). Now I want to know the values used in the boxplot, i.e. the median, upper and lower quartile, the upper whisker end point and the lower whisker end point. While the former three are easy to obtain by using np.median() and np.percentile(), the end point of the whiskers will require some verbose coding:
```
median = np.median(data)
upper_quartile = np.percentile(data, 75)
lower_quartile = np.percentile(data, 25)

iqr = upper_quartile - lower_quartile
upper_whisker = data[data<=upper_quartile+1.5*iqr].max()
lower_whisker = data[data>=lower_quartile-1.5*iqr].min()
```
I was wondering, while this is acceptable, would there be a neater way to do this? It seems that the values should be ready to pull-out from the boxplot, as it's already drawn.

Response:
Why do you want to do so? what you are doing is already pretty direct. Yeah, if you want to fetch them for the plot, when the plot is already made, simply use the get_ydata() method.
```
B = plt.boxplot(data)
[item.get_ydata() for item in B['whiskers']]
```
It returns an array of the shape (2,) for each whiskers, the second element is the value we want:
```
[item.get_ydata()[1] for item in B['whiskers']]
```


Question: scipy.stats seed? I am trying to generate scipy.stats.pareto.rvs(b, loc=0, scale=1, size=1) with different seed. In numpy we can seed using numpy.random.seed(seed=233423). Is there any way to seed the random number generated by scipy stats. Note: I am not using numpy pareto because I want to give different values for scale.

Response:
scipy.stats just uses numpy.random to generate its random numbers, so numpy.random.seed() will work here as well. E.g.,
```
import numpy as np
from scipy.stats import pareto
b = 0.9
np.random.seed(seed=233423)
print pareto.rvs(b, loc=0, scale=1, size=5)
np.random.seed(seed=233423)
print pareto.rvs(b, loc=0, scale=1, size=5)
```
will print [ 9.7758784 10.78405752 4.19704602 1.19256849 1.02750628] twice.


Question: Partition array into N chunks with Numpy There is this How do you split a list into evenly sized chunks? for splitting an array into chunks. Is there anyway to do this more efficiently for giant arrays using Numpy?

Response:
Try numpy.array_split. From the documentation:
```

```python
x = np.arange(8.0)
```

```python
np.array_split(x, 3)
#Output
#    [array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])]
#```
#Identical to numpy.split, but won't raise an exception if the groups aren't equal length. If number of chunks > len(array) you get blank arrays nested inside, to address that - if your split array is saved in a, then you can remove empty arrays by:
#```
#[x for x in a if x.size > 0]
#```
#Just save that back in a if you wish.
```


Question: Convert ndarray from float64 to integer I've got an ndarray in python with a dtype of float64. I'd like to convert the array to be an array of integers. How should I do this? int() won't work, as it says it can't convert it to a scalar. Changing the dtype field itself obviously doesn't work, as the actual bytes haven't changed. I can't seem to find anything on Google or in the documentation - what's the best way to do this?

Response:
Use .astype.
```

```python
a = numpy.array([1, 2, 3, 4], dtype=numpy.float64)
```

```python
a
#Output
#array([ 1.,  2.,  3.,  4.])
```

```python
a.astype(numpy.int64)
#Output
#array([1, 2, 3, 4])
#```
#See the documentation for more options.
```


Question:
Pandas dataframe: how to apply describe() to each group and add to new columns? df:
```
name score
A      1
A      2
A      3
A      4
A      5
B      2
B      4
B      6
B      8
```
Want to get the following new dataframe in the form of below:
```
name count mean std min 25% 50% 75% max
    A     5    3    .. ..  ..  ..  ..  ..
    B     4    5    .. ..  ..  ..  ..  ..
```
How to exctract the information from df.describe() and reformat it? Thanks

Response:
there is even a shorter one :)
```
print df.groupby('name').describe().unstack(1)
```
Nothing beats one-liner:

```python
print df.groupby('name').describe().reset_index().pivot(index='name', values='score', columns='level_1')
```


Question:
Time difference in seconds from numpy.timedelta64 How to get time difference in seconds from numpy.timedelta64 variable?
```
time1 = '2012-10-05 04:45:18'
time2 = '2012-10-05 04:44:13'
dt = np.datetime64(time1) - np.datetime64(time2)
print dt

0:01:05
```
I'd like to convert dt to number (int or float) representing time difference in seconds.

Response:
To get number of seconds from numpy.timedelta64() object using numpy 1.7 experimental datetime API:
```
seconds = dt / np.timedelta64(1, 's')
```


Question: What is the difference between size and count in pandas? That is the difference between groupby("x").count and groupby("x").size in pandas ? Does size just exclude nil ?

Response:
size includes NaN values, count does not:
```

```python
df = pd.DataFrame({'a':[0,0,1,2,2,2], 'b':[1,2,3,4,np.NaN,4], 'c':np.random.randn(6)})
df
#Output
#a   b         c
#0  0   1  1.067627
#1  0   2  0.554691
#2  1   3  0.458084
#3  2   4  0.426635
#4  2 NaN -2.238091
#5  2   4  1.256943
```

```python
print(df.groupby(['a'])['b'].count())
print(df.groupby(['a'])['b'].size())

a
0    2
1    1
2    2
Name: b, dtype: int64

a
0    2
1    1
2    3
dtype: int64
```
```


Question:
Numpy - add row to array How does one add rows to a numpy array? I have an array A:
```
A = array([[0, 1, 2], [0, 2, 0]])
```
I wish to add rows to this array from another array X if the first element of each row in X meets a specific condition. Numpy arrays do not have a method 'append' like that of lists, or so it seems. If A and X were lists I would merely do:
```
for i in X:
    if i[0] < 3:
        A.append(i)
```
Is there a numpythonic way to do the equivalent? Thanks, S ;-)

Response:
You can do this:
```
newrow = [1, 2, 3]
A = numpy.vstack([A, newrow])
```


Question:
efficient python array to numpy array conversion I get a big array (image with 12 Mpix) in the array format from the python standard lib. Since I want to perform operations on those array, I wish to convert it to a numpy array. I tried the following:
```
import numpy
import array
from datetime import datetime
test = array.array('d', [0]*12000000)
t = datetime.now()
numpy.array(test)
print datetime.now() - t
```
I get a result between one or two seconds: equivalent to a loop in python. Is there a more efficient way of doing this conversion?

Response:
```
np.array(test)                                       # 1.19s

np.fromiter(test, dtype=int)                         # 1.08s

np.frombuffer(test)                                  # 459ns !!!
```


Question:
What is dtype('O'), in pandas? I have a dataframe in pandas and I'm trying to figure out what the types of its values are. I am unsure what the type is of column 'Test'. However, when I run myFrame['Test'].dtype, I get;
```
dtype('O')
```
What does this mean?

Response:
It means:
```
'O'     (Python) objects
```
Source. The first character specifies the kind of data and the remaining characters specify the number of bytes per item, except for Unicode, where it is interpreted as the number of characters. The item size must correspond to an existing type, or an error will be raised. The supported kinds are to an existing type, or an error will be raised. The supported kinds are:
```
'b'       boolean
'i'       (signed) integer
'u'       unsigned integer
'f'       floating-point
'c'       complex-floating point
'O'       (Python) objects
'S', 'a'  (byte-)string
'U'       Unicode
'V'       raw data (void)
```
Another answer helps if need check types.


Question: Multivariate normal density in Python? Is there any python package that allows the efficient computation of the PDF (probability density function) of a multivariate normal distribution? It doesn't seem to be included in Numpy/Scipy, and surprisingly a Google search didn't turn up any useful thing.

Response:
The multivariate normal is now available on SciPy 0.14.0.dev-16fc0af:
```
from scipy.stats import multivariate_normal
var = multivariate_normal(mean=[0,0], cov=[[1,0],[0,1]])
var.pdf([1,0])
```


Question:
Add single element to array in numpy I have a numpy array containing:
```
[1, 2, 3]
```
I want to create an array containing:
```
[1, 2, 3, 1]
```
That is, I want to add the first element on to the end of the array. I have tried the obvious:
```
np.concatenate((a, a[0]))
```
But I get an error saying ValueError: arrays must have same number of dimensions I don't understand this - the arrays are both just 1d arrays.

Response:
append() creates a new array which can be the old array with the appended element. I think it's more normal to use the proper method for adding an element:
```
a = numpy.append(a, a[0])
```


Question: What is the difference between np.array() and np.asarray()? What is the difference between NumPy's np.array and np.asarray? When should I use one rather than the other? They seem to generate identical output.

Response: Since other questions are being redirected to this one which ask about asanyarray or other array creation routines, it's probably worth having a brief summary of what each of them does. The differences are mainly about when to return the input unchanged, as opposed to making a new array as a copy. array offers a wide variety of options (most of the other functions are thin wrappers around it), including flags to determine when to copy. A full explanation would take just as long as the docs (see Array Creation, but briefly, here are some examples: Assume a is an ndarray, and m is a matrix, and they both have a dtype of float32: np.array(a) and np.array(m) will copy both, because that's the default behavior. np.array(a, copy=False) and np.array(m, copy=False) will copy m but not a, because m is not an ndarray. np.array(a, copy=False, subok=True) and np.array(m, copy=False, subok=True) will copy neither, because m is a matrix, which is a subclass of ndarray. np.array(a, dtype=int, copy=False, subok=True) will copy both, because the dtype is not compatible. Most of the other functions are thin wrappers around array that control when copying happens: asarray: The input will be returned uncopied iff it's a compatible ndarray (copy=False). asanyarray: The input will be returned uncopied iff it's a compatible ndarray or subclass like matrix (copy=False, subok=True). ascontiguousarray: The input will be returned uncopied iff it's a compatible ndarray in contiguous C order (copy=False, order='C'). asfortranarray: The input will be returned uncopied iff it's a compatible ndarray in contiguous Fortran order (copy=False, order='F'). require: The input will be returned uncopied iff it's compatible with the specified requirements string. copy: The input is always copied. fromiter: The input is treated as an iterable (so, e.g., you can construct an array from an iterator's elements, instead of an object array with the iterator); always copied. There are also convenience functions, like asarray_chkfinite (same copying rules as asarray, but raises ValueError if there are any nan or inf values), and constructors for subclasses like matrix or for special cases like record arrays, and of course the actual ndarray constructor (which lets you create an array directly out of strides over a buffer).


Question:
How to loop through 2D numpy array using x and y coordinates without getting out of bounds error? I have tried the following:
```
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print a
rows = a.shape[0]
cols = a.shape[1]
print rows
print cols

for x in range(0, cols - 1):
    for y in range(0, rows -1):
        print a[x,y]
```
This only prints the numbers 1 through 6. I have also tried only subtracting 1 from either rows or cols in the range, but that either leads to an out-of-bounds error or not all numbers being printed.

Response:
You get prettier code with:
```
for iy, ix in np.ndindex(a.shape):
    print(a[iy, ix])
```
resulting in:
```
1
2
3
4
5
6
7
8
9
10
11
12
```


Question:
Multiple linear regression in Python I can't seem to find any python libraries that do multiple regression. The only things I find only do simple regression. I need to regress my dependent variable (y) against several independent variables (x1, x2, x3, etc.). For example, with this data:
```
print 'y        x1      x2       x3       x4      x5     x6       x7'
for t in texts:
    print "{:>7.1f}{:>10.2f}{:>9.2f}{:>9.2f}{:>10.2f}{:>7.2f}{:>7.2f}{:>9.2f}" /
   .format(t.y,t.x1,t.x2,t.x3,t.x4,t.x5,t.x6,t.x7)
```
(output for above:)
```
y        x1       x2       x3        x4     x5     x6       x7
   -6.0     -4.95    -5.87    -0.76     14.73   4.02   0.20     0.45
   -5.0     -4.55    -4.52    -0.71     13.74   4.47   0.16     0.50
  -10.0    -10.96   -11.64    -0.98     15.49   4.18   0.19     0.53
   -5.0     -1.08    -3.36     0.75     24.72   4.96   0.16     0.60
   -8.0     -6.52    -7.45    -0.86     16.59   4.29   0.10     0.48
   -3.0     -0.81    -2.36    -0.50     22.44   4.81   0.15     0.53
   -6.0     -7.01    -7.33    -0.33     13.93   4.32   0.21     0.50
   -8.0     -4.46    -7.65    -0.94     11.40   4.43   0.16     0.49
   -8.0    -11.54   -10.03    -1.03     18.18   4.28   0.21     0.55
```
How would I regress these in python, to get the linear regression formula: Y = a1x1 + a2x2 + a3x3 + a4x4 + a5x5 + a6x6 + +a7x7 + c

Response:
sklearn.linear_model.LinearRegression will do it:
```
from sklearn import linear_model
clf = linear_model.LinearRegression()
clf.fit([[getattr(t, 'x%d' % i) for i in range(1, 8)] for t in texts],
        [t.y for t in texts])
```
Then clf.coef_ will have the regression coefficients. sklearn.linear_model also has similar interfaces to do various kinds of regularizations on the regression.


Question:
Python Numpy TypeError: ufunc 'isfinite' not supported for the input types Here's my code:
```py
def topK(dataMat,sensitivity):
    meanVals = np.mean(dataMat, axis=0)
    meanRemoved = dataMat - meanVals
    covMat = np.cov(meanRemoved, rowvar=0)
    eigVals,eigVects = np.linalg.eig(np.mat(covMat))
```
I get the error in the title on the last line above. I suspect it has something to do with the datatype, so, here's an image of the variable and datatype from the Variable Explorer in Spyder: I've tried changing np.linalg.eig(np.mat(covMat)) to np.linalg.eig(np.array(np.mat(covMat))) and to np.linalg.eig(np.array(covMat)), nothing works. Any ideas?

Response:
Your array has a dtype of object, but this should be some floating point dtype. Use e.g.
```
covMat = np.array(covMat, dtype=float)
```
to convert the dtype


Question:
What is the best way to convert a SymPy matrix to a numpy array/matrix I am not sure if the approach I've been using in sympy to convert a MutableDenseMatrix to a numpy.array or numpy.matrix is a good current practice. I have a symbolic matrix like:
```
g = sympy.Matrix( [[   x,  2*x,  3*x,  4*x,  5*x,  6*x,  7*x,  8*x,   9*x,  10*x],
                   [x**2, x**3, x**4, x**5, x**6, x**7, x**8, x**9, x**10, x**11]] )
```
and I am converting to a numpy.array doing:
```
g_func = lambda val: numpy.array( g.subs( {x:val} ).tolist(), dtype=float )
```
where I get an array for a given value of x. Is there a better built-in solution in SymPy to do that? Thank you!

Response:
This looks like the most straightforward:
```
np.array(g).astype(np.float64)
```
If you skip the astype method, numpy will create a matrix of type 'object', which won't work with common array operations.


Question:
Interleaving two NumPy arrays efficiently Assume the following arrays are given:
```
a = array([1, 3, 5])
b = array([2, 4, 6])
```
How would one interleave them efficiently so that one gets a third array like the following?
```
c = array([1, 2, 3, 4, 5, 6])
```
It can be assumed that length(a) == length(b).

Response:
I like Josh's answer. I just wanted to add a more mundane, usual, and slightly more verbose solution. I don't know which is more efficient. I expect they will have similar performance.
```
import numpy as np

a = np.array([1,3,5])
b = np.array([2,4,6])

c = np.empty((a.size + b.size,), dtype=a.dtype)
c[0::2] = a
c[1::2] = b
```


Question:
How to transform negative elements to zero without a loop? If I have an array like
```
a = np.array([2, 3, -1, -4, 3])
```
I want to set all the negative elements to zero: [2, 3, 0, 0, 3]. How to do it with numpy without an explicit for? I need to use the modified a in a computation, for example
```
c = a * b
```
where b is another array with the same length of the original a Conclusion
```
import numpy as np
from time import time

a = np.random.uniform(-1, 1, 20000000)
t = time(); b = np.where(a>0, a, 0); print ("1. ", time() - t)
a = np.random.uniform(-1, 1, 20000000)
t = time(); b = a.clip(min=0); print ("2. ", time() - t)
a = np.random.uniform(-1, 1, 20000000)
t = time(); a[a < 0] = 0; print ("3. ", time() - t)
a = np.random.uniform(-1, 1, 20000000)
t = time(); a[np.where(a<0)] = 0; print ("4. ", time() - t)
a = np.random.uniform(-1, 1, 20000000)
t = time(); b = [max(x, 0) for x in a]; print ("5. ", time() - t)
```
1.38629984856 0.516846179962 <- faster a.clip(min=0); 0.615426063538 0.944557905197 51.7364809513

Response:
```
a = a.clip(min=0)
```


Question:
Weighted random sample without replacement in python I need to obtain a k-sized sample without replacement from a population, where each member of the population has a associated weight (W). Numpy's random.choices will not perform this task without replacement, and random.sample won't take a weighted input. Currently, this is what I am using:
```
P = np.zeros((1,Parent_number))
n=0
while n < Parent_number:
    draw = random.choices(population,weights=W,k=1)
    if draw not in P:
        P[0,n] = draw[0]
        n=n+1
P=np.asarray(sorted(P[0]))
```
While this works, it reqires switching back and forth from arrays, to lists and back to arrays and is, therefore, less than ideal. I am looking for the simplest and easiest to understand solution as this code will be shared with others.

Response:
You can use np.random.choice with replace=False as follows:
```
np.random.choice(vec,size,replace=False, p=P)
```
where vec is your population and P is the weight vector. For example:
```
import numpy as np
vec=[1,2,3]
P=[0.5,0.2,0.3]
np.random.choice(vec,size=2,replace=False, p=P)
```


Question:
Numpy matrix binarization using only one expression I am looking for a way to binarize numpy N-d array based on the threshold using only one expression. So I have something like this:
```
np.random.seed(0)
np.set_printoptions(precision=3)
a = np.random.rand(4, 4)
threshold, upper, lower = 0.5, 1, 0
```
a is now:
```
array([[ 0.02 ,  0.833,  0.778,  0.87 ],
       [ 0.979,  0.799,  0.461,  0.781],
       [ 0.118,  0.64 ,  0.143,  0.945],
       [ 0.522,  0.415,  0.265,  0.774]])
```
Now I can fire these 2 expressions:
```
a[a>threshold] = upper
a[a<=threshold] = lower
```
and achieve what I want:
```
array([[ 0.,  1.,  1.,  1.],
       [ 1.,  1.,  0.,  1.],
       [ 0.,  1.,  0.,  1.],
       [ 1.,  0.,  0.,  1.]])
```
But is there a way to do this with just one expression?

Response:
We may consider np.where:
```
np.where(a>threshold, upper, lower)
Out[6]:
array([[0, 1, 1, 1],
       [1, 1, 0, 1],
       [0, 1, 0, 1],
       [1, 0, 0, 1]])
```


Question:
How to get value counts for multiple columns at once in Pandas DataFrame? Given a Pandas DataFrame that has multiple columns with categorical values (0 or 1), is it possible to conveniently get the value_counts for every column at the same time? For example, suppose I generate a DataFrame as follows:
```
import numpy as np
import pandas as pd
np.random.seed(0)
df = pd.DataFrame(np.random.randint(0, 2, (10, 4)), columns=list('abcd'))
```
I can get a DataFrame like this:
```
a  b  c  d
0  0  1  1  0
1  1  1  1  1
2  1  1  1  0
3  0  1  0  0
4  0  0  0  1
5  0  1  1  0
6  0  1  1  1
7  1  0  1  0
8  1  0  1  1
9  0  1  1  0
```
How do I conveniently get the value counts for every column and obtain the following conveniently?
```
a  b  c  d
0  6  3  2  6
1  4  7  8  4
```
My current solution is:
```
pieces = []
for col in df.columns:
    tmp_series = df[col].value_counts()
    tmp_series.name = col
    pieces.append(tmp_series)
df_value_counts = pd.concat(pieces, axis=1)
```
But there must be a simpler way, like stacking, pivoting, or groupby?

Response:
Just call apply and pass pd.Series.value_counts:
```

```python
df = pd.DataFrame(np.random.randint(0, 2, (10, 4)), columns=list('abcd'))
df.apply(pd.Series.value_counts)
#Output
#a  b  c  d
#0  4  6  4  3
#1  6  4  6  7
#```
```


Question:
How to get the element-wise mean of an ndarray I'd like to calculate element-wise average of numpy ndarray.
```

```python
a = np.array([10, 20, 30])
```

```python
b = np.array([30, 20, 20])
```

```python
c = np.array([50, 20, 40])
```
What I want:
```
[30, 20, 30]
```
Is there any in-built function for this operation, other than vectorized sum and dividing?
```

Response:
You can just use np.mean directly:
```py

```python
np.mean([a, b, c], axis=0)
#Output
#array([ 30.,  20.,  30.])
#```
```


Question:
Linear regression of arrays containing NAN values I have two arrays, say varx and vary. Both contain NaN values at various positions. However, I would like to do a linear regression on both to show how much the two arrays correlate. This was very helpful so far. However, using the following
```py
slope, intercept, r_value, p_value, std_err = stats.linregress(varx, vary)
```
results in NaNs for every output variable. What is the most convenient way to take only valid values from both arrays as input to the linear regression? I heard about masking arrays, but am not sure how it works exactly.

Response:
You can remove NaNs using a mask:
```
mask = ~np.isnan(varx) & ~np.isnan(vary)
slope, intercept, r_value, p_value, std_err = stats.linregress(varx[mask], vary[mask])
```


Question: plus/minus operator for python ± I am looking for a way to do a plus/minus operation in python 2 or 3. I do not know the command or operator, and I cannot find a command or operator to do this. Am I missing something?

Response:
If you are looking to print the ± symbol, just use:
```
print(u"\u00B1")
```


Question:
Pandas: convert dtype 'object' to int I've read an SQL query into Pandas and the values are coming in as dtype 'object', although they are strings, dates and integers. I am able to convert the date 'object' to a Pandas datetime dtype, but I'm getting an error when trying to convert the string and integers. Here is an example:
```

```python
import pandas as pd
```

```python
df = pd.read_sql_query('select * from my_table', conn)
```

```python
df
#Output
#    id    date          purchase
# 1  abc1  2016-05-22    1
# 2  abc2  2016-05-29    0
# 3  abc3  2016-05-22    2
# 4  abc4  2016-05-22    0
```

```python
df.dtypes
#Output
# id          object
# date        object
# purchase    object
# dtype: object
#```
#Converting the df['date'] to a datetime works:
#```
```

```python
pd.to_datetime(df['date'])
#Output
# 1  2016-05-22
# 2  2016-05-29
# 3  2016-05-22
# 4  2016-05-22
# Name: date, dtype: datetime64[ns]
#```
#But I get an error when trying to convert the df['purchase'] to an integer:
#```
```

```python
df['purchase'].astype(int)
#Output
# ....
# pandas/lib.pyx in pandas.lib.astype_intsafe (pandas/lib.c:16667)()
# pandas/src/util.pxd in util.set_value_at (pandas/lib.c:67540)()
```

TypeError: long() argument must be a string or a number, not 'java.lang.Long'
```
NOTE: I get a similar error when I tried .astype('float') And when trying to convert to a string, nothing seems to happen.
```

```python
df['id'].apply(str)
#Output
# 1 abc1
# 2 abc2
# 3 abc3
# 4 abc4
# Name: id, dtype: object
#```
```

Response:
Documenting the answer that worked for me based on the comment by @piRSquared. I needed to convert to a string first, then an integer.
```

```python
df['purchase'].astype(str).astype(int)
#Output
#```
```


Question:
Error "TypeError: type numpy.ndarray doesn't define __round__ method"
```
import numpy

......

# Prediction
predictions = model.predict(X_test)
# round predictions
rounded = [round(x) for x in predictions]
print(rounded)

"predictions" is a list of decimals between [0,1] with sigmoid output.
```
Why does it always report this error:
```
File "/home/abigail/workspace/ml/src/network.py", line 41, in <listcomp>
    rounded = [round(x) for x in predictions]
TypeError: type numpy.ndarray doesn't define __round__ method
```
If i don't use the 'round', it prints decimals correctly. This "round" should be the Python built-in function. Why does it have anything to do with numpy? Edited:
```
for x in predictions:
    print(x, end=' ')
```
The output is:
```
[ 0.79361773] [ 0.10443521] [ 0.90862566] [ 0.10312044] [ 0.80714297]
[ 0.23282401] [ 0.1730803] [ 0.55674052] [ 0.94095331] [ 0.11699325]
[ 0.1609294]
```

Response:
TypeError: type numpy.ndarray doesn't define round method You tried applying round to numpy.ndarray. Apparently, this isn't supported. Try this, use numpy.round:
```
rounded = [numpy.round(x) for x in predictions]
```
x is numpy array. You can also try this:
```
rounded = [round(y) for y in x for x in predictions]
```


Question:
Difference between Python float and numpy float32 What is the difference between the built in float and numpy.float32? Example
```
a = 58682.7578125
print type(a)
print a
print type(numpy.float32(a))
print numpy.float32(a)
```
Output:
```
<type 'float'>
58682.7578125
<type 'numpy.float32'>
58682.8
```
I've found here that numpy.float32 is: float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa didn't find what the built in float format is.

Response: Python's standard float type is a C double: http://docs.python.org/2/library/stdtypes.html#typesnumeric NumPy's standard numpy.float is the same, and is also the same as numpy.float64.


Question: How do I read CSV data into a record array in NumPy? Is there a direct way to import the contents of a CSV file into a record array, just like how R's read.table(), read.delim(), and read.csv() import data into R dataframes? Or should I use csv.reader() and then apply numpy.core.records.fromrecords()?

Response:
Use numpy.genfromtxt() by setting the delimiter kwarg to a comma:
```
from numpy import genfromtxt
my_data = genfromtxt('my_file.csv', delimiter=',')
```


Question:
Removing elements from an array that are in another array Say I have these 2D arrays A and B. How can I remove elements from A that are in B. (Complement in set theory: A-B)
```
A=np.asarray([[1,1,1], [1,1,2], [1,1,3], [1,1,4]])
B=np.asarray([[0,0,0], [1,0,2], [1,0,3], [1,0,4], [1,1,0], [1,1,1], [1,1,4]])
#output = [[1,1,2], [1,1,3]]
```
To be more precise, I would like to do something like this.
```
data = some numpy array
label = some numpy array
A = np.argwhere(label==0) #[[1 1 1], [1 1 2], [1 1 3], [1 1 4]]
B = np.argwhere(data>1.5) #[[0 0 0], [1 0 2], [1 0 3], [1 0 4], [1 1 0], [1 1 1], [1 1 4]]
out = np.argwhere(label==0 and data>1.5) #[[1 1 2], [1 1 3]]
```

Response:
there is an easy solution with a list comprehension,
```
A = [i for i in A if i not in B]
```
Result
```
[[1, 1, 2], [1, 1, 3]]
```
List comprehension is not removing the elements from the array, it's just reassigning - if you want to remove the elements, use this method:
```
for i in B:
     if i in A:
     A.remove(i)
```


Question:
Binary random array with a specific proportion of ones? What is the efficient(probably vectorized with Matlab terminology) way to generate random number of zeros and ones with a specific proportion? Specially with Numpy? As my case is special for 1/3, my code is:
```
import numpy as np
a=np.mod(np.multiply(np.random.randomintegers(0,2,size)),3)
```
But is there any built-in function that could handle this more effeciently at least for the situation of K/N where K and N are natural numbers?

Response:
Yet another approach, using np.random.choice:
```

```python
np.random.choice([0, 1], size=(10,), p=[1./3, 2./3])
#Output
#array([0, 1, 1, 1, 1, 0, 0, 0, 0, 0])
#```
```


Question: How to solve a pair of nonlinear equations using Python? What's the (best) way to solve a pair of non linear equations using Python. (Numpy, Scipy or Sympy) eg: x+y^2 = 4 e^x+ xy = 3 A code snippet which solves the above pair will be great

Response:
for numerical solution, you can use fsolve: http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html#scipy.optimize.fsolve
```
from scipy.optimize import fsolve
import math

def equations(p):
    x, y = p
    return (x+y**2-4, math.exp(x) + x*y - 3)

x, y =  fsolve(equations, (1, 1))

print equations((x, y))
```


Question:
How to calculate the statistics "t-test" with numpy I'm looking to generate some statistics about a model I created in python. I'd like to generate the t-test on it, but was wondering if there was an easy way to do this with numpy/scipy. Are there any good explanations around? For example, I have three related datasets that look like this:
```
[55.0, 55.0, 47.0, 47.0, 55.0, 55.0, 55.0, 63.0]
```
Now, I would like to do the student's t-test on them.

Response:
In a scipy.stats package there are few ttest_... functions. See example from here:
```

```python
print 't-statistic = %6.3f pvalue = %6.4f' %  stats.ttest_1samp(x, m)
#Output
#t-statistic =  0.391 pvalue = 0.6955
#```
```


Question:
Replace negative values in an numpy array Is there a simple way of replacing all negative values in an array with 0? I'm having a complete block on how to do it using a NumPy array. E.g.
```
a = array([1, 2, 3, -4, 5])
```
I need to return
```
[1, 2, 3, 0, 5]
```
a < 0 gives:
```
[False, False, False, True, False]
```
This is where I'm stuck - how to use this array to modify the original array.

Response:
You are halfway there. Try:
```

```python
a[a < 0] = 0
```

```python
a
#Output
#array([1, 2, 3, 0, 5])
#```
```


Question:
Perform a reverse cumulative sum on a numpy array Can anyone recommend a way to do a reverse cumulative sum on a numpy array? Where 'reverse cumulative sum' is defined as below (I welcome any corrections on the name for this procedure): if
```
x = np.array([0,1,2,3,4])
```
then
```
np.cumsum(x)
```
gives
```
array([0,1,3,6,10])
```
However, I would like to get
```
array([10,10,9,7,4]
```
Can anyone suggest a way to do this?

Response:
This does it:
```
np.cumsum(x[::-1])[::-1]
```


Question: How to calculate a Fourier series in Numpy? I have a periodic function of period T and would like to know how to obtain the list of the Fourier coefficients. I tried using fft module from numpy but it seems more dedicated to Fourier transforms than series. Maybe it a lack of mathematical knowledge, but I can't see how to calculate the Fourier coefficients from fft. Help and/or examples appreciated.

Response:
In the end, the most simple thing (calculating the coefficient with a riemann sum) was the most portable/efficient/robust way to solve my problem:
```
import numpy as np
def cn(n):
   c = y*np.exp(-1j*2*n*np.pi*time/period)
   return c.sum()/c.size

def f(x, Nh):
   f = np.array([2*cn(i)*np.exp(1j*2*i*np.pi*x/period) for i in range(1,Nh+1)])
   return f.sum()

y2 = np.array([f(t,50).real for t in time])

plot(time, y)
plot(time, y2)
```
gives me:


Question:
How do I select elements of an array given condition? Suppose I have a numpy array x = [5, 2, 3, 1, 4, 5], y = ['f', 'o', 'o', 'b', 'a', 'r']. I want to select the elements in y corresponding to elements in x that are greater than 1 and less than 5. I tried
```
x = array([5, 2, 3, 1, 4, 5])
y = array(['f','o','o','b','a','r'])
output = y[x > 1 & x < 5] # desired output is ['o','o','a']
```
but this doesn't work. How would I do this?

Response:
Your expression works if you add parentheses:
```

```python
y[(1 < x) & (x < 5)]
#Output
#array(['o', 'o', 'a'],
#      dtype='|S1')
#```
```


Question:
Getting only those values that fulfill a condition in a numpy array There must a be a (very) quick and efficient way to get only elements from a numpy array, or even more interestingly from a slice of it. Suppose I have a numpy array:
```
import numpy as np
a = np.arange(-10,10)
```
Now if I have a list:
```
s = [9, 12, 13, 14]
```
I can select elements from a:
```
a[s]  #array([-1,  2,  3,  4])
```
How can I have an (numpy) array made of the elements from a[s] that fulfill a condition, i.e. are positive (or negative)? It should result
```
np.ifcondition(a[s]>0, a[s])  #array([2,  3,  4])
```
It looks trivial but I was not able to find a simple and condensed expression. I'm sure masks do but it's doesn't look really direct to me. However, neither:
```
a[a[s]>0]
a[s[a[s]>0]]
```
are in fact good choices.

Response:
How about:
```

```python
b = a[s]
```

```python
b[b > 0]
#Output
#array([2, 3, 4])
#```
```


Question:
Numpy argsort - what is it doing? Why is numpy giving this result:
```
x = numpy.array([1.48,1.41,0.0,0.1])
print x.argsort()

>[2 3 1 0]
```
when I'd expect it to do this: [3 2 0 1] Clearly my understanding of the function is lacking.

Response: According to the documentation Returns the indices that would sort an array. 2 is the index of 0.0. 3 is the index of 0.1. 1 is the index of 1.41. 0 is the index of 1.48.


Question: How to print a Numpy array without brackets? I want to convert a = [1,2,3,4,5] into a_string = "1 2 3 4 5". The real numpy array is quite big (50000x200) so I assume using for loops is too slow.

Response:
You can use the join method from string:
```

```python
a = [1,2,3,4,5]
```

```python
' '.join(map(str, a))
#Output
#"1 2 3 4 5"
#```
```