Is the numpy.irr function correct?
This question may have an obvious answer that I am just missing. I am looking through the source code for numpy.irr and getting a bit confused about how they are going about solving it. IRR is found by solving
for irr. However, in the numpy code it solves the polynomial
for x, then returns 1/x  1 as the IRR. I might be mistaken, but these don't seem to be equivalent operations. They are close, but it seems that there would be situations in which the two numbers could be very different. Is there another library that has a more accurate IRR, or would I be better off creating my own function?
1 answer

I might be mistaken, but these don't seem to be equivalent operations.
You're mistaken. Those are equivalent operations.
See also questions close to this topic

Get Information from Microsoft Academic Graph API
I am trying to use the Microsoft Academic Graph API with Python, to get information about the Author's Affiliations. However, the information provided in
https://docs.microsoft.com/enus/azure/cognitiveservices/academicknowledge/graphsearchmethod
is not clear to me.
I have also read Microsoft Academic Graph Search  retrieving all papers from a journal within a timeframe?
I am trying with something like this:
import requests url = "https://westus.api.cognitive.microsoft.com/academic/v1.0/graph/search" querystring = {"mode":"json%0A"} payload = "{}" response = requests.request("POST", url, data=payload, params=querystring) print(response.text)
What should I put in "payload" to retrieve the affiliation of, for example, the Author "John Doe" ?

Chunking in gRPC with Python
Can anyone share any Python server and client code that demonstrates the use of chunking in gRPC similar to https://jbrandhorst.com/post/grpcbinaryblobstream/? Thanks.

How To Read Health (In Game) Using Python
I am looking for a way for python to read the health, in text format, and then execute a command (e.i. press of a key). I was looking at a series by the youtuber sentdex: (https://www.youtube.com/playlist?list=PLQVvvaa0QuDeETZEOy4VdocT7TOjfSA8a). He programmed python to capture his screen and "read" the lanes in order to accomplish a self driving car. I want to base off my goal from the first part of the series, using numpy, PIL, and OpenCV, to capture my screen using the following code.
import numpy as np from PIL import ImageGrab import cv2 import time last_time = time.time() while(True): screen = np.array(ImageGrab.grab(bbox=(0,0, 1360, 768))) printscreen_numpy = np.array(printscreen_pil.getdata(),dtype='uint8') print('Loop took {} seconds'.format(time.time()last_time)) last_time = time.time() cv2.imshow('window', cv2.cvtColor (screen, cv2.COLOR_BGR2RGB)) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break
(Code to print how long it takes a loop can be ignored)
However, I have no idea how to use opencv or PIL to recognize text in the specified area of my screen, or what to do after to execute a command.
All I need is some advice, not looking for a complete answer. Any help is appreciated, thanks.

My LSTM learns, loss decreases, but Numerical Gradients don't match Analytical Gradients
The following is selfcontained, and when you run it it will:
1. print the loss to verify it's decreasing (learning a
sin
wave),2. Check the numeric gradients against my handderived gradient function.
The two gradients tend to match within
1e1 to 1e2
(which is still bad, but shows it's trying) and there are occasional extreme outliers.I have spent all saturday backing out to a normal FFNN, getting that to work (yay, gradients match!), and now sunday on this LSTM, and well, I can't find the bug in my logic. Oh, and it's heavily depends on my random seed, sometimes it's great, sometimes awful.
I've hand checked my implementation against hand derived derivatives for the LSTM equations (i did the calculus), and against the implementations in these 3 blogs/gist:
And tried the (amazing) debugging methods suggested here: https://blog.slavv.com/37reasonswhyyourneuralnetworkisnotworking4020854bd607
import numpy as np np.set_printoptions(precision=3, suppress=True) def check_grad(params, In, Target, f, df_analytical, delta=1e5, tolerance=1e7, num_checks=10): """ delta : how far on either side of the param value to go tolerance : how far the analytical and numerical values can diverge """ h_n = params['Wf'].shape[1] # TODO: h & c should be passed in (?) h = np.zeros(h_n) c = np.zeros(h_n) y, outputs, loss, h, c, caches = f(params, h, c, inputs, targets) dparams = df_analytical(params, inputs, targets, outputs, caches) passes = True for _ in range(num_checks): print() for pname, p, dpname, dp in zip(params.keys(), params.values(), dparams.keys(), dparams.values()): pix = np.random.randint(0, p.size) old_val = p.flat[pix] # d = delta * abs(old_val) if old_val != 0 else 1e5 d = delta p.flat[pix] = old_val + d _, _, loss_plus, _, _, _ = f(params, h, c, In, Target) # note `_` is the cache p.flat[pix] = old_val  d _, _, loss_minus, _, _, _ = f(params, h, c, In, Target) p.flat[pix] = old_val grad_analytic = dp.flat[pix] grad_numeric = (loss_plus  loss_minus) / (2 * d) denom = abs(grad_numeric + grad_analytic) + 1e12 # max((abs(grad_numeric), abs(grad_analytic))) relative_error = abs(grad_analytic  grad_numeric) / denom if relative_error > tolerance: print(("fails: %s % 4d  r: % 3.4f, a: % 3.4f, n: % 3.4f, a/n: %0.2f") % (pname, pix, relative_error, grad_analytic, grad_numeric, grad_analytic/grad_numeric)) passes &= relative_error <= tolerance return passes #  def lstm(params, inp, h_old, c_old): Wf, Wi, Wg, Wo, Wy = params['Wf'], params['Wi'], params['Wg'], params['Wo'], params['Wy'] bf, bi, bg, bo, by = params['bf'], params['bi'], params['bg'], params['bo'], params['by'] xh = np.concatenate([inp, h_old]) f = np.dot(xh, Wf) + bf f_sigm = 1 / (1 + np.exp(f)) i = np.dot(xh, Wi) + bi i_sigm = 1 / (1 + np.exp(i)) g = np.dot(xh, Wg) + bg # Ctilde or Cbar in the literature g_tanh = np.tanh(g) o = np.dot(xh, Wo) + bo o_sigm = 1 / (1 + np.exp(o)) c = f_sigm * c_old + i_sigm * g_tanh c_tanh = np.tanh(c) h = o_sigm * c_tanh y = np.dot(h, Wy) + by # NOTE: this is a dense layer bolted on after a normal LSTM # TODO: should it have a nonlinearity after it? MSE would not work well with, for ex, a sigmoid cache = (xh, f, f_sigm, i, i_sigm, g, g_tanh, o, o_sigm, c, c_tanh, c_old, h) return y, h, c, cache def dlstm(params, dy, dh_next, dc_next, cache): Wf, Wi, Wg, Wo, Wy = params['Wf'], params['Wi'], params['Wg'], params['Wo'], params['Wy'] bf, bi, bg, bo, by = params['bf'], params['bi'], params['bg'], params['bo'], params['by'] xh, f, f_sigm, i, i_sigm, g, g_tanh, o, o_sigm, c, c_tanh, c_old, h = cache dby = dy.copy() dWy = np.outer(h, dy) dh = np.dot(dy, Wy.T) + dh_next.copy() do = c_tanh * dh * o_sigm * (1  o_sigm) dc = dc_next.copy() + o_sigm * dh * (1  c_tanh ** 2) # TODO: copy? dg = i_sigm * dc * (1  g_tanh ** 2) di = g_tanh * dc * i_sigm * (1  i_sigm) df = c_old * dc * f_sigm * (1  f_sigm) # ERROR FIXED: ??? c_old > c?, c>c_old? dWo = np.outer(xh, do); dbo = do; dXo = np.dot(do, Wo.T) dWg = np.outer(xh, dg); dbg = dg; dXg = np.dot(dg, Wg.T) dWi = np.outer(xh, di); dbi = di; dXi = np.dot(di, Wi.T) dWf = np.outer(xh, df); dbf = df; dXf = np.dot(df, Wf.T) dX = dXo + dXg + dXi + dXf dh_next = dX[h.size:] dc_next = f_sigm * dc dparams = dict(Wf = dWf, Wi = dWi, Wg = dWg, Wo = dWo, Wy = dWy, bf = dbf, bi = dbi, bg = dbg, bo = dbo, by = dby) return dparams, dh_next, dc_next def lstm_loss(params, h, c, inputs, targets): loss = 0 outputs = [] caches = [] for inp, target in zip(inputs, targets): y, h, c, cache = lstm(params, inp, h, c) loss += np.mean((y  target) ** 2) outputs.append(y) caches.append(cache) loss = loss # / inputs.shape[0] return y, outputs, loss, h, c, caches def dlstm_loss(params, inputs, targets, outputs, caches): h_shape = caches[0][1].shape dparams = {k:np.zeros_like(v) for k, v in params.items()} dh = np.zeros(h_shape) dc = np.zeros(h_shape) for inp, out, target, cache in reversed(list(zip(inputs, outputs, targets, caches))): dy = 2 * (out  target) dps, dh, dc = dlstm(params, dy, dh, dc, cache) for dpk, dpv in dps.items(): dparams[dpk] += dpv return dparams #  # setup x_n = 1 h_n = 5 o_n = 1 params = dict( Wf = np.random.normal(size=(x_n + h_n, h_n)), Wi = np.random.normal(size=(x_n + h_n, h_n)), Wg = np.random.normal(size=(x_n + h_n, h_n)), Wo = np.random.normal(size=(x_n + h_n, h_n)), Wy = np.random.normal(size=(h_n, o_n)), bf = np.zeros(h_n) + np.random.normal(size=h_n) * 0.1, bi = np.zeros(h_n) + np.random.normal(size=h_n) * 0.1, bg = np.zeros(h_n) + np.random.normal(size=h_n) * 0.1, bo = np.zeros(h_n) + np.random.normal(size=h_n) * 0.1, by = np.zeros(o_n) + np.random.normal(size=o_n) * 0.1, ) for name in ['Wf', 'Wi', 'Wg', 'Wo', 'Wy']: W = params[name] W *= np.sqrt(2 / (W.shape[0] + W.shape[1])) # Xavier initialization for name in params: params[name] = params[name].astype('float64') #  # Sanity check, learn sin wave def test_sin(): emaloss = 1 # EMA average emak = 0.99 for t in range(5000): data = np.sin(np.linspace(0, 3 * np.pi, 30)) start = np.random.randint(0, data.size // 4) end = np.random.randint((data.size * 3) // 4, data.size) inputs = data[start:end, None] targets = np.roll(inputs, 1, axis=0) h_n = params['Wf'].shape[1] # TODO: h & c should be passed in h = np.random.normal(size=h_n) c = np.random.normal(size=h_n) y, outputs, loss, h, c, caches = lstm_loss(params, h, c, inputs, targets) dparams = dlstm_loss(params, inputs, targets, outputs, caches) for k in params.keys(): params[k] = dparams[k] * 0.01 emaloss = emaloss * emak + loss * (1  emak) if t % 100 == 0: print('%.4f' % emaloss) test_sin() #  data = np.sin(np.linspace(0, 4 * np.pi, 90)) start = np.random.randint(0, data.size // 4) end = np.random.randint((data.size * 3) // 4, data.size) inputs = data[start:end, None] targets = np.roll(inputs, 1, axis=0) for inp, targ in zip(inputs, targets): assert(check_grad(params, inputs, targets, lstm_loss, dlstm_loss, delta=1e5, tolerance=1e7, num_checks=10)) print('grads are ok') # < i never reach here

How do I prebroadcast a numpy array?
I have a numpy array
a = np.array([1, 2, 3])
I want the array repeated across n dimensions
f(a, 2) == np.array([[1, 2, 3], [1, 2, 3]])
How do I do this with numpy?

Is there any way to run np.where on multiple values rather than just one?
I'm wondering if I have an image in a numpy array, say 250x250x3 (3 channels), is it possible to use np.where to quickly find out if any of the 250x250 arrays of size 3 are equal to [143, 255, 0] or another color represented by rgb and get a 250x250 bool array?
When I try it in code with a 4x4x3, I get a 3x3 array as a result and I'm not totally sure where that shape is coming from.
import numpy as np test = np.arange(4,52).reshape(4,4,3) print(np.where(test == [4,5,6]))  Result: array([[0, 0, 0], [0, 0, 0], [0, 1, 2]]) What I'm trying to get: array([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])

Box Plots in Statistics, should I consider median or outliers?
That's the question. I'm not sure. My answer is that there is no clear choice for the traveller because JetBlue has outliers that are closer to the median, having consistently shorter delays. But Delta has the smallest median delay compared to the other 4 airlines.

Why am I getting output for 2//4 as 1?
I'm getting
1
as output for2//4
. Output should be0
as per python.print(2//4)

Pointer of pointer arithmetic?
I understand how the arithmetic of pointers works, but I have doubts about the one of the pointer of pointer: If I have this code:
int x; int* ptr = &x; int** ptrptr = &ptr;
On a standard Windows system, if I do:
printf("%d",ptr); ptr++; printf("%d",ptr);
ptr will have a "+4" value than before, obviously because integer needs 4 bytes.
Now, if I do:
printf("%d",ptrptr); ptrptr++; printf("%d",ptrptr);
ptrptr will have a "+16" value than before, why?
I apologize if this question has already been posted but I couldn't find out, thank you.Code:
#include<stdio.h> int main(void){ int x; int* ptr = &x; int** ptrptr = &ptr; printf("Pointer: %p\nPointer of pointer: %p\n",(void*)ptr,(void*)ptrptr); ptr++; ptrptr++; printf("Pointer: %p\nPointer of pointer: %p\n",(void*)ptr,(void*)ptrptr); return 0; }
Output:
Pointer: 000000000062FE44 Pointer of pointer: 000000000062FE38 Pointer: 000000000062FE48 Pointer of pointer: 000000000062FE40