Numpy mean of nonzero values

Get the count of non-zeros in each row and use that for averaging the summation along each row. Thus, the implementation would look something like this – np.true_divide(matrix.sum(1),(matrix!=0).sum(1)) If you are on an older version of NumPy, you can use float conversion of the count to replace np.true_divide, like so – matrix.sum(1)/(matrix!=0).sum(1).astype(float) Sample run – … Read more

Middle point of each pair of an numpy.array

Even shorter, slightly sweeter: (x[1:] + x[:-1]) / 2 This is faster: >>> python -m timeit -s “import numpy; x = numpy.random.random(1000000)” “x[:-1] + numpy.diff(x)/2” 100 loops, best of 3: 6.03 msec per loop >>> python -m timeit -s “import numpy; x = numpy.random.random(1000000)” “(x[1:] + x[:-1]) / 2” 100 loops, best of 3: 4.07 … Read more

How to efficiently get the mean of the elements in two list of lists in Python

You can do it in O(n) (single pass over each list) by converting 1 to a dict, then per item in the 2nd list access that dict (in O(1)), like this: mylist1 = [[“lemon”, 0.1], [“egg”, 0.1], [“muffin”, 0.3], [“chocolate”, 0.5]] mylist2 = [[“chocolate”, 0.5], [“milk”, 0.2], [“carrot”, 0.8], [“egg”, 0.8]] l1_as_dict = dict(mylist1) myoutput … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)