Surprising behavior of numpy.random.shuffle [duplicate] - python

This question already has answers here:
numpy.random.shuffle returns None
(4 answers)
Closed 3 years ago.
while using the numpy.random.shuffle in my own function, it accidentally changed my global variable. I'm not sure if it is there some misunderstanding on its usage.
The numpy version is '1.16.4'
import numpy as np
def shuffle_test(a):
np.random.shuffle(a)
b=a
return b
outer_input = np.array(range(10))
print(outer_input)
outer_output = shuffle(outer_input)
print(outer_input)
Here's the result
input before shuffle:
[0 1 2 3 4 5 6 7 8 9]
input after shuffle:
[5 4 7 1 8 2 6 0 9 3]

It is not accidental; it is the described behavior of np.random.shuffle
Modify a sequence in-place by shuffling its contents
you could do like this:
import numpy as np
outer_input = np.array(range(10))
np.random.shuffle(outer_input)
outer_input
# array([1, 9, 7, 4, 3, 6, 0, 5, 2, 8]) # for instance

Related

What is the meaning of a[..., 4] in python? [duplicate]

This question already has answers here:
What does the Ellipsis object do?
(14 answers)
Closed 6 months ago.
I am sorry if this looks easy but I can't find the meaning of this online.
Below is the original code line in the Non-max suppression function for yoloV5 in general.py:
xc = prediction[..., 4] > conf_thres #candidates
I have understood it now. Below is a code snippet
import numpy as np
n = np.random.randint(9, size=(5,6))
print(n)
check =5
a = n[...,4]>check
print(a)
Output for above code is :
[[1 2 2 2 4 3]
[1 5 4 8 2 0]
[6 6 3 0 7 6]
[4 2 5 1 3 1]
[3 5 2 6 4 2]]
[False False True False False]

Time Limit Exceeded in Sliding Window Maximum(InterviewBit) [duplicate]

This question already has answers here:
Sliding window maximum in O(n) time
(3 answers)
Closed 2 years ago.
Given an array of integers A. There is a sliding window of size B which
is moving from the very left of the array to the very right.
You can only see the w numbers in the window. Each time the sliding window moves
rightwards by one position. You have to find the maximum for each window.
The following example will give you more clarity.
The array A is [1 3 -1 -3 5 3 6 7], and B is 3.
Example:
Window position Max
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Input 1:
A = [1, 3, -1, -3, 5, 3, 6, 7]
B = 3
Output 1:
C = [3, 3, 5, 5, 6, 7]
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified.
My Code:
class Solution:
# #param A : tuple of integers
# #param B : integer
# #return a list of integers
def slidingMaximum(self, A, B):
stack = []
if B>=len(A):
return [max(A)]
else:
for i in range((len(A))+1-B):
stack.append(A[i:i+B])
stack[i]=max(stack[i])
return stack
It's showing me time limit exceeded. Can anyone tell me why?
Time Limit Exceeded. Your submission didn't complete in the allocated time limit
It's because in the branch if B>=len(A): you are returning single value. There should be return [max(A)]

what is the difference between resize and reshape [duplicate]

This question already has answers here:
What is the difference between resize and reshape when using arrays in NumPy?
(5 answers)
What does it mean "The reshape function returns its argument with a modified shape, whereas the ndarray.resize method modifies the array itself"?
(3 answers)
Closed 2 years ago.
I was writing a line of code and I get some strange output of it.
a = np.arange(2,11).resize((3,3))
print(a)
a = np.arange(2,11).reshape((3,3))
print(a)
the first one gives me None but the second one gives me a 3X3 matrix.
but when I write the first code in separate lines it won't give me None
a = np.arange(2,11)
a.resize((3,3))
print(a)
what is the difference between resize and reshape in this case and in what are the differences in general?
That is because ndarray.resize modifies the array shape in-place, and since you're assigning back to a you get None, as an in-place operation does not return anything. reshape instead returns a view of the array:
a = np.arange(2,11)
a.shape
#(10,)
a.resize((3,3))
a.shape
# (3, 3)
np.arange(2,11).reshape((3,3)).shape
# (3, 3)
Both reshape and resize change the shape of the numpy array; the difference is that using resize will affect the original array while using reshape create a new reshaped instance of the array.
Reshape:
import numpy as np
r = np.arange(16)
print('original r: \n',r)
print('\nreshaped array: \n',r.reshape((4,4)))
print('\narray r after reshape was applied: \n',r)
output
original r:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
reshaped array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
array r after reshape was applied:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
Resize:
import numpy as np
r = np.arange(16)
print('original r: \n',r)
print('\nresized array: \n',r.resize((4,4)))
print('\narray r after resize was applied: \n',r)
output:
original r:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
resized array:
None
array r after resize was applied:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
As you can see reshape created a reshaped new instance of the data while the original r stayed unchanged.And As you can see, resize didn’t create a new instance of r, the changes were applied to the original array directly.

Python: reshape matrix to collection of lists

I am new to python, and I am stuck on a basic question. I want to enter a matrix:
[1 2 3
4 5 6
10 9 1]
but I want it to be in the form:
[[1,2,3], [4,5,6],[10,9,1]]
Do these representations have any specific names? What does that comma and double square brackets denote? I know this is a really silly question, but how do you convert it to the desired form?
Two ways:
1) Plain python
First step: Generate a list of 9 numbers
import random
randomlist = []
for i in range(0, 9):
n = random.randint(1, 30) # change for the value you desire here... for the moment it will give you a random numbers btween 0 to 9
randomlist.append(n)
print(randomlist)
Second step: make it to desired reshape (list of three lists) :
desiredoutput = [randomlist[x:x+3] for x in range(0, len(randomlist), 3)]
print(desiredoutput)
2) Using numpy
Reproduce the first step of solution 1 to get an array of random numbers. Then:
import numpy
sourcelist = numpy.asarray(randomlist)
desiredoutput = sourcelist.reshape(3, 3)
What you want to use is python numpy library which allows you to reshape your arrays into a form you want, not python lists
import numpy as np
a = np.asarray([1, 2, 3, 4, 5, 6, 9, 10, 1])
print(a)
# [ 1 2 3 4 5 6 9 10 1]
b = a.reshape(3, 3)
print(b)
# [[ 1 2 3]
# [ 4 5 6]
# [ 9 10 1]]

only one element in numpy array shape [duplicate]

This question already has answers here:
Difference between numpy.array shape (R, 1) and (R,)
(8 answers)
Closed 6 years ago.
I have an array converted from list, when I try to get its shape, I got only one number. like this:
list1=[1,2,3,4,5]
a1=numpy.array(list1)
print a1.shape
and I got
(5,)
and then I tried
list2=[[1,2,3,4,5]]
a2=numpy.array(list2)
list3=[[1],[2],[3],[4],[5]]
a3=numpy.array(list3)
print a1+a2
print a1+a3
I get
[[ 2 4 6 8 10]]
[[ 2 3 4 5 6]
[ 3 4 5 6 7]
[ 4 5 6 7 8]
[ 5 6 7 8 9]
[ 6 7 8 9 10]]
it seems a1 works like a2. Can I think like that way? Will it cause problems if i treat a1 as a2, besides shape method?
Try:
list1=[[1,2,3,4,5]]
a=numpy.array(list1)
print a.shape
This will give you (1, 5), one row, 5 columns.
And this:
list1=[[1],[2],[3],[4],[5]]
a=numpy.array(list1)
print a.shape
Will get you (5, 1)
You can look at this post, it has clarified every thing related to numpy shape. (edit: question marked as duplicate of the same question)
This simple code might also help you get some insights:
import numpy as np
list1=[1,2,3,4,5]
a=np.array(list1)
print a.shape
# (5,)
## set be to be the transpose of a
b = a.T
print b.shape
# (5,)
print np.inner(a,b)
# 55
print np.inner(a,a)
# 55

Categories