admin管理员组

文章数量:1241159

Why are outputs in the two cases different. I am new to this library

Case 1

import numpy as np


np.random.seed(2)

array = np.random.random((3,1))
print('Printing array : \n', array)
print('printing array - 1 : \n',array-1)
Output :
Printing array : 
[[0.4359949 ]
[0.02592623]
[0.54966248]]
printing array - 1 : 
[[-0.5640051 ]
[-0.97407377]
[-0.45033752]]

This is ok as 1 is subtracted from each element

Case 2

print('Printing array : \n', np.random.random ((3,1))-1)

Output:

Printing array : 
 [[-0.56467761]
 [-0.5796322 ]
 [-0.66966518]]

Whay are the two outputs different? np.random.random ((3,1)) should be same in both cases ( same seed) and so subtracting 1 should produce the same output. what am I messing up?

I ran the code as was expecting the same output in both cases

Why are outputs in the two cases different. I am new to this library

Case 1

import numpy as np


np.random.seed(2)

array = np.random.random((3,1))
print('Printing array : \n', array)
print('printing array - 1 : \n',array-1)
Output :
Printing array : 
[[0.4359949 ]
[0.02592623]
[0.54966248]]
printing array - 1 : 
[[-0.5640051 ]
[-0.97407377]
[-0.45033752]]

This is ok as 1 is subtracted from each element

Case 2

print('Printing array : \n', np.random.random ((3,1))-1)

Output:

Printing array : 
 [[-0.56467761]
 [-0.5796322 ]
 [-0.66966518]]

Whay are the two outputs different? np.random.random ((3,1)) should be same in both cases ( same seed) and so subtracting 1 should produce the same output. what am I messing up?

I ran the code as was expecting the same output in both cases

Share Improve this question edited 2 days ago ThomasIsCoding 102k9 gold badges36 silver badges101 bronze badges asked 2 days ago srajansrajan 2053 silver badges9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 5

In your case two, you're calling np.random.random again - even though you've started with the same seed, the internal state will change each time you call random.

To demonstrate this, get rid of the subtraction entirely, and just print the results of two calls:

import numpy as np

np.random.seed(2)
array1 = np.random.random((3,1))
print('First call: \n', array1)
array2 = np.random.random((3,1))
print('Second call: \n', array2)

The code above still uses a variable to make it more like your first code, but you could equally just write:

import numpy as np

np.random.seed(2)
print('First call: \n', np.random.random((3,1)))
print('Second call: \n', np.random.random((3,1)))

The reason why you got different arrays has been explained elaborately by @Jon Skeet.


One workaround is to customize a function by packing the random seed together with the random number generator function, e.g.,

def runif(shape, seed = 2): 
  np.random.seed(seed)
  return np.random.random(shape)

for iter in range(2):
  print(f'Print array{iter}: \n {runif((3,1))-iter} \n')

and you will see

Print array0: 
 [[0.4359949 ]
 [0.02592623]
 [0.54966248]] 

Print array1: 
 [[-0.5640051 ]
 [-0.97407377]
 [-0.45033752]] 

本文标签: pythonSubtracting 1 from a numpy array gives two different answersStack Overflow