mean
How to use numpy with ‘None’ value in Python?
You are looking for masked arrays. Here’s an example. import numpy.ma as ma a = ma.array([1, 2, None], mask = [0, 0, 1]) print “average =”, ma.average(a) From the numpy docs linked above, “The numpy.ma module provides a nearly work-alike replacement for numpy that supports data arrays with masks.”
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
typeerror: app.use() requires middleware function
Delete this line, app.use(multer({ dest: ‘./uploads’ })); and use instead var multer = require(‘multer’); var upload = multer({ dest: ‘./uploads’ });
Add a row with means of columns to pandas DataFrame
Use loc for setting with enlargement: df.loc[‘mean’] = df.mean() The resulting output: diode1 diode2 diode3 diode4 Time 0.53 7.0 0.0 10.0 16.0 1.218 17.0 7.0 14.0 19.0 1.895 13.0 8.0 16.0 17.0 2.57 8.0 2.0 16.0 17.0 3.24 14.0 8.0 17.0 19.0 3.91 13.0 6.0 17.0 18.0 4.594 13.0 5.0 16.0 19.0 5.265 9.0 0.0 … 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