r/Numpy • • Sep 14 '20

Numpy NPV result is different from Excel NPV

0 Upvotes

Used Numpy to calculate npv as follows:

values = [14.892954074214174, 12.271794157152481, 12.639947981867053, 26.453332745339317, 30.046338079069223, 28.367044540256813, 30.68134532066973, 27.33150257785883, 28.984058844314813, 30.561504164209893, 32.08904751944876, 26.867960706012994, 28.050288261923118, 51.1465655158229, 53.20618536382071, 55.27620389449583, 49.16942571419325, 50.98033854748804, 88.028551474307, 91.13999744248962, 84.87702361771062, 87.78386024540882, 50.41732638415238]

discount_rate = 0.029

print(np.npv(discount_rate, values))

The output is 670.892546309928

However, using NPV in Excel, the output is 651.9849818366650

Why the outputs are not matching?

Update: Turns out Numpy and Excel calculate npv differently [0]. The workaround for Numpy to get exact results as Excel is to change:

np.npv(discount_rate, values)

to

np.npv(discount_rate, values)/(1+discount_rate)

Note: numpy.npv is deprecated and will be removed from NumPy 1.20. Use numpy_financial.npv [1] instead.

0: https://numpy.org/doc/1.19/reference/generated/numpy.npv.html

1: https://pypi.org/project/numpy-financial/


r/Numpy • • Jul 07 '24

Can you explain np.linalg.det() and np.linalg.inv()

0 Upvotes

Explain the np.linalg.det() and np.linalg.inv() to a person who doesn't know linear algebra

And to a person who doesn't understand inverse and determinant of a matrix thank you in advance.


r/Numpy • • May 15 '20

Please check out our Numpy course for free and beginners. All you need is Python and you can take the course. Please Like and Subsribe.

Thumbnail
youtube.com
0 Upvotes

r/Numpy • • Apr 10 '20

Vectorizing a boolean operation over all rows

0 Upvotes

Given:

num_points = 1000
radius = 25
raw_X = np.random.randn(num_points,2)*100
raw_y = np.zeros((num_points, 1))

I wish to vectorize the following:

for i in range(num_points):
    raw_y[i] = (raw_X[i][0]**2 + raw_X[i][1]**2 >= radius**2).astype(int)

Can it be done?

Thanks.

Edit: If I'm not wrong,

raw_Y = (np.sum(raw_X**2, axis = 1) > radius**2).astype(int)

does the job.


r/Numpy • • Dec 14 '24

Numpy histograms(numpy + matplot)

Post image
0 Upvotes

What topics do I leave to learn in Numpy for machine learning?

Numpy #Matplotlib #AI #ML


r/Numpy • • Sep 27 '21

Full Description Numpy and Compete Numpy Operations NSFW Spoiler

0 Upvotes

r/Numpy • • Sep 03 '21

Where is the 'obfuscated numpy' site?

0 Upvotes

numpy offers (enforces) operations on multi-dimensional arrays in very concise format. Functions like tile, reformat, stack, meshgrid, etc. and other expressions often yield code that is difficult to de-cipher. So, where is the obfuscated numpy site/thread, showing industrial strength examples and explaining step by step how the results are obtained?


r/Numpy • • Jun 16 '20

How to solve "Linear Equation" using Python.

0 Upvotes

r/Numpy • • May 24 '20

Oompah Loompa what does it do? How is Numpy useful to you?

0 Upvotes

hello, all. I apologize if this seems haphazard and like an overload of questions. I am extremely sleep deprived, so I'm going to do my best 4 a noob.brass tacks, I want to know what I can make np do. I want to get into data visualization for abstract/pure mathematics, but have it be versatile enough to where I could input for the variables as well as have unassigned variables. I guess what I'm driving at, is I want to be able to build visual mathematical models I can manipulate easily, look for patterns in numerical data, and be able to explore the heart of mathematical concepts, preferably all of them in one program. if this is not what I'm looking for, I would love it if somebody could point me in the direction of a certain program or plugin for jupyter Notebook that will be what I need. thanks.


r/Numpy • • Feb 27 '22

Probably a very stupid question. How do I solve for X *without making it the subject of the equation* I have A1,A2 and ARE. Thank you in advance.

Post image
0 Upvotes

r/Numpy • • Jul 12 '21

Essentials of NumPy

Thumbnail
youtu.be
0 Upvotes

r/Numpy • • Apr 16 '21

10 Best Online Resources To Learn NumPy

Thumbnail
analyticsindiamag.com
1 Upvotes

r/Numpy • • Jun 21 '21

Numpy Tutorial #1

Thumbnail
youtu.be
4 Upvotes

r/Numpy • • Feb 07 '21

Understanding python/numpy memory management on this extreme example

2 Upvotes

Try this sequence of instructions in a python interpreter and monitor the RAM usage after each instruction:

import numpy as np
# 1: allocates 5000*100000*4 Bytes
a = np.ones(5000*100000, dtype=np.int32)  

# 2: garbage collection free the previous allocation
a = None 

# 3: allocates again but with many small arrays
a = [np.ones(5000, dtype=np.int32) for i in range(100000)] 

# 4: garbage collection does not free the previous allocation !
a = None  

# 5: allocates 5000*100000*4 Bytes on top of the previous allocation
a = np.ones(5000*100000, dtype=np.int32)

What exactly is happening here and is it possible to get back the memory after 3, to use it again during 5 ?

It seems to be a memory fragmentation issue: GC probably does free the memory but it is too fragmented to be used again by a large single block ?

(Using numpy 1.15 and python 3.7)


r/Numpy • • May 10 '20

Numpy array is the worst part of python.

24 Upvotes

I come from HPC (c++/c/Fortran, etc), I can read and use other languages without issue. I like many parts of python, and its nice a lot of the time to not have to program everything from the ground up. I am not the biggest fan of weakly typed languages and python is slow, but it defiantly has a place and it does what it's designed to do pretty well.

However, the way numpy organises its array syntax and the general working of array is probably the most confusing poorly designed thing in the entire language, perhaps in the world of programming languages. I spend 80% of my programming time trying to get things into the right shape or work out what shape they are, and then use the correct functions to do extremely trivial appends/inserts/delete etc. Its is vague and honestly should be rewritten. There is honestly no defence of this poor syntax. There is a reason pretty much every other language deals with arrays in the same way... it makes sense.

I know that the hardcore python fanboys will tell me I am not doing things correctly, or lol, say "I must think 'pythonically'". BS, a good language does not need some gibberish word to defend the poor design.


r/Numpy • • Apr 24 '20

Y parameter in NP.log

0 Upvotes

Hello guys. I’ve seen this in Numpy, what does actually the Y value do in Log as a Numpy array? L = np.log(X[range(2), Y])


r/Numpy • • Apr 17 '20

Used np.linal.solve(A,f) And Got This Error Message?

0 Upvotes

Hi, I used the above np function to solve the linear equations shown below but I kept getting this error message, would anyone be able to explain to me where I'm going wrong?

Thanks,

A burnt out maths student

import numpy as np
z=3
L=0.5
x=np.linspace(-L,L,z)
# ABC coefficient matrix #
def A(n):
 alpha=10
 beta=0.5
 L=0.5
 delx= (len(x)/z)
 a=(1/(delx)**2)
 b=(2/(delx)**2)-(alpha)
 c=(1/(delx)**2)
 lol=(np.eye(n+1, k=1, dtype=int)*c)
 lmao=(np.eye(n+1, k=-1, dtype=int)*a)
 lawl= (lmao+lol)
 lawl[n][n-1]=0
 lawl[0][1]=0
 for i in range (n+1):
   lawl[i][i]=b
 lawl[0][0]=1
 lawl[n][n]=1
 return(lawl)
print(A(z-1))
#############################################################
# f(x) function #
def f(x):
 beta = 0.5
 L = 0.5
 return((-beta)*((x**2)-(L**2)))
L=0.5
print(np.vstack(f(x)))
#############################################################
# This is the y function #
ym= np.linalg.solve(A,f)
print(ym)
###############################################################

r/Numpy • • May 01 '21

basic array from a loop

0 Upvotes
N = 10000   
a=7**5 
c=0 
M=(2**31)-1 
I=1 
L=1 
my_array=np.array(N) 
for i in range(N):     
    my_array[i]=np.array([x]) 
    for x in range (N):     
    In=((a*I)+c) % M     
    x=L*I/M     
    I=In 

I'm trying to do the np.random function but in a different way. My main code is:

for x in range (N):     
    In=((a*I)+c) % M     
    x=L*I/M     
    I=In 

which is a loop of random numbers less than 1. By itself, it works and lists a bunch of numbers, but I'm trying to store these numbers in an array, such as [9,2,1,6]. The numbers don't have to be in order. I just need them to have the brackets and the commas. I really don't know what I'm doing.


r/Numpy • • Aug 28 '22

VIDEOSTREAM opencv +UDP and did a litle compression with numpy (split into tiles, removed jpg header, only send tile when precious img changed much)

0 Upvotes

r/Numpy • • Apr 25 '23

Hot reloading and ChatGPT - Simplify Complex Problems with AI and Reloadium

Thumbnail
gallery
0 Upvotes

r/Numpy • • Jul 18 '22

Question about specifying structured dtype alignment

0 Upvotes

I have looked around for an answer to this, but havent found exactly what I need. I want to be able to create a structured dtype representing a C struct with non-default alignment. An example struct:

struct __attribute__((aligned(8))) float2
{
    float x;
    float y;
};

I can create dtype with two floats easily enough:

float2_dtype = np.dtype( [ ( 'x', 'f4' ), ( 'y', 'f4' ) ], align=True )

but the alignment for this dtype (float2_dtype.alignment) will be 4. This means that if I pack this dtype into another structured dtype I will get alignment errors. What I would really like to do is

float2_dtype.alignment = 8 # gives AttributeError: readonly attribute

or

float2_dtype = np.dtype( [ ( 'x', 'f4' ), ( 'y', 'f4' ) ], align=True, alignment=8 ) # Invalid keyword argument for dtype()

Is there a way to to this? I apologize if I have missed an obvious solution to this issue -- I have grepped around the internet with no success.


r/Numpy • • Apr 06 '22

I need help to transpose

Post image
0 Upvotes

r/Numpy • • Aug 13 '24

numpy 2.0 is slower?

0 Upvotes

I have a code that obtains prime numbers. but it is much slower in numpy 2.0 python 3.12.4 than in numpy 1.26 and python 3.11.1. Does anyone know anything about it? thank you so much


r/Numpy • • Dec 17 '20

Is there a good way to sort arrays for readability?

0 Upvotes

I have a list of integer coordinate points and I really wish I could organize them by x followed by y coordinate, just to make them easy to read. I thought the sort function would do this, but it doesn't.


r/Numpy • • Sep 10 '21

Deterministically random floats from a starting point?

0 Upvotes

Not sure how best to describe what I want here.

Let's say I'm using the made-up function random_floats to generate frames in a video:

for i in range(100_000):
    frame = random_floats(size=(1080, 1920, 3))

That loop will take a long time to run, but for whatever reason I want the value of the last frame. I can easily calculate how many random numbers will have been generated by that point, and therefore how many I need to skip. Is there a way of skipping those 99_999 * 1080 * 1920 * 3 floats and just get the last ones?

I'm thinking if the python RNGs all use previous values to calculate the next ones, then this would be impossible, but I'm hoping they don't do that (that would make loops inevitable anyway, right?).

So, maybe there's an alternative fast RNG that works vaguely like this?:

class Rng:
    def __init__(self, index=0):
        self.index = index

    def __call__(self):
        random_float = hash_int_to_float(self.index)
        self.index += 1
        return random_float

rng = Rng()
for _ in range(100_000):
    rng()
print(rng())
> 0.762194875

rng = Rng(100_000)
print(rng())
> 0.762194875

Hopefully that makes sense...