What does # do in numpy matrix manipulations? [duplicate] - python

This question already has answers here:
Difference between numpy dot() and Python 3.5+ matrix multiplication #
(6 answers)
What does the "at" (#) symbol do in Python?
(14 answers)
Closed 2 years ago.
I cam across this function '#' in numpy. But I am not able to figure out what it is doing or able to get documentation for it. Could someone please point me to the documentation or tell what it is doing? Code is below
import numpy as np
w = np.array([[1,2,3,4],[2,3,1,3]])
x1 = np.array([0,1,0,1])
s = w#x1

Related

How could I translate the command find of MATLAB to Python? [duplicate]

This question already has answers here:
MATLAB-style find() function in Python
(9 answers)
Is there a NumPy function to return the first index of something in an array?
(20 answers)
Replacement of matlab find function in python
(1 answer)
Converting find() in matlab to python
(3 answers)
Closed 1 year ago.
I have this iteration in a program in Matlab and want to translate it to Python, but my problem is in the parameters for 'n' and 'direction'.
for i=1:size(labels)
idx_V=[idx_V;find(y(idxUnls(trial,:))==labels(i),l/length(labels),'first')]
end
There isn't a one-to-one swap for MATLAB's find function in Python. Taking inspiration from another answer here, I would propose the following solution:
% MATLAB
inds = find(myarray == condition, n, 'first')
# Python
import numpy as np
inds = [ind for (ind, val) in np.ndenumerate(myarray == condition) if val]
inds = inds[0:n]
I'm sure there is probably some trickery to think about in terms of which dimension find operates over first, compared to ndenumerate. The Python expression could also be constructed as a generator.
If you want a similar implementation, you'll have to write it yourself in Python.

How do I use infinity in python? [duplicate]

This question already has answers here:
How can I represent an infinite number in Python?
(13 answers)
Closed 2 years ago.
I tried making this function to return infinity so I could use it in equations.
but I keep getting an error that says something about stack overflow, so that's why I'm asking you guys.
def returnInfinity(num = 1):
return returnInfinity(num + 1)
INFINITY = returnInfinity()
Use the math library's math.inf if you're on version 3.5 or above.
import math
print(math.inf)
Otherwise, you can do int("inf") or float("inf").

Cant multiply sequence by Float for Python [duplicate]

This question already has answers here:
Python array multiply
(3 answers)
Closed 5 years ago.
I saw this questions a couple other places but all the fixes dont work for me code. If someone could help that would be great.
import random
cvalues=[]
for i in range(50):
cvalues.append(random.randrange(0,16))
float_cvalues=[float(i) for i in cvalues]
print(float_cvalues)
nvalues=[((.4*(float_cvalues)-.8))]
print(nvalues)
Multiplying a sequence in Python is interpreted as an attempt to create multiple sequences, see this post.
You can instead use another list comprehension:
nvalues=[.4*i-.8 for i in float_cvalues]
Or for instance switch to numpy arrays.

python print error, math incorrect (1/10=0) [duplicate]

This question already has answers here:
Script printing all zeros [duplicate]
(2 answers)
Basic python arithmetic - division
(6 answers)
Closed 5 years ago.
when I startup python, I don't import anything and I just enter 1/10, it returns 0. Any idea how to fix this?

incrementing elements of 2d list in python [duplicate]

This question already has answers here:
Python list problem [duplicate]
(2 answers)
Nested List Indices [duplicate]
(2 answers)
Closed 9 years ago.
I've noticed something strange when trying to implement elements of a 2d list in python. Here is my code
n_dk=2*[5*[0]]
n_dk[0][0]+=1
print n_dk
I would expect the output to be
[[1,0,0,0,0],[0,0,0,0,0]]
but the actual output is
[[1,0,0,0,0],[1,0,0,0,0]]
can anyone tell me what I am doing wrong. btw I used a numpy array instead and it worked the way I wanted it to.

Categories

Resources