I am trying to translate some code from MATLAB to Python. I have been stumped on this part of the MATLAB code:
[L,N] = size(Y);
if (L<p)
error('Insufficient number of columns in y');
end
I understand that [L,N] = size(Y) returns the number of rows and columns when Y is a matrix. However I have limited experience with Python and thus cannot understand how to do the same with Python. This also is part of the reason I do not understand how the MATLAB logic with in the loop can be also fulfilled in Python.
Thank you in advance!
Also, in case the rest of the code is also needed. Here it is.
function [M,Up,my,sing_values] = mvsa(Y,p,varargin)
if (nargin-length(varargin)) ~= 2
error('Wrong number of required parameters');
end
% data set size
[L,N] = size(Y)
if (L<p)
error('Insufficient number of columns in y');
end
I am still unclear as to what p is from your post, however the excerpt below effectively performs the same task as your MATLAB code in Python. Using numpy, you can represent a matrix as an array of arrays and then call .shape to return the number of rows and columns, respectively.
import numpy as np
p = 2
Y = np.matrix([[1, 1, 1, 1],[2, 2, 2, 2],[3, 3, 3, 3]])
L, N = Y.shape
if L < p:
print('Insufficient number of columns in y')
Non-numpy
data = ([[1, 2], [3, 4], [5, 6]])
L, N = len(data), len(data[0])
p = 2
if L < p:
raise ValueError("Insufficient number of columns in y")
number_of_rows = Y.__len__()
number_of_cols = Y[0].__len__()
Related
i was told to Use a Python program to compute ê for every row of the array and store the results in a separate NumPy array.
which example 1 or 2 (image) below being correctly displayed as a seprate Numpy Array?
Consider you have a 3*2 image. Probably "all rows" would mean doing operation e across the columns. Just an example of np.sum()
>>> img=np.array([[1,1],[2,1],[4,1]])
>>> e=np.sum(img,axis=1)
>>> e
array([2, 3, 5])
>>> e.shape
(3,)
>>> img.shape
(3, 2)
>>> img
array([[1, 1],
[2, 1],
[4, 1]])
>>>
However, it depends really on what your ê is which hasn't been posted.
It could be ê is being calculated for each element or 'element-wise' for which you would have to do as Rinshan stated in second part of his answer.
You should refer these diagrams to clear out which axis you need to perform e-hat on. This is only how much I can help you sorry.
EDIT: If e-hat is exp then you could sum across the columns then apply np.exp()
Compute exponent row wise
import numpy as np
ar = np.array(([4.0,0.2,1.16,0.5],[6.0,0.1,0.06,-0.75]))
e = np.exp(np.sum(ar,axis=1)) # O/P array([350.72414402, 223.63158768])
# Take exponent and take some along axis 1
e = np.sum(np.exp(ar),axis=1)
Computing exponent element wise
e = np.exp(ar)
# To convert to single row
e =e.reshape(-1) # or single line e = np.exp(ar).reshape(-1)
print(e)
array([ 54.59815003, 1.22140276, 3.18993328, 1.64872127,
403.42879349, 1.10517092, 1.06183655, 0.47236655])
Compute by multiplying with variable
import numpy as np
ar = np.array(([4.0,0.2,1.16,0.5],[6.0,0.1,0.06,-0.75]))
s = np.sum(a,axis=1)
e_calculated = e ** s # (Where you can assing value of e)
# Calculating with np.power np.power(e,s)
I am trying to split a list into n sublists where the size of each sublist is random (with at least one entry; assume P>I). I used numpy.split function which works fine but does not satisfy my randomness condition. You may ask which distribution the randomness should follow. I think, it should not matter. I checked several posts which were not equivalent to my post as they were trying to split with almost equally sized chunks. If duplicate, let me know. Here is my approach:
import numpy as np
P = 10
I = 5
mylist = range(1, P + 1)
[list(x) for x in np.split(np.array(mylist), I)]
This approach collapses when P is not divisible by I. Further, it creates equal sized chunks, not probabilistically sized chunks. Another constraint: I do not want to use the package random but I am fine with numpy. Don't ask me why; I wish I had a logical response for it.
Based on the answer provided by the mad scientist, this is the code I tried:
P = 10
I = 5
data = np.arange(P) + 1
indices = np.arange(1, P)
np.random.shuffle(indices)
indices = indices[:I - 1]
result = np.split(data, indices)
result
Output:
[array([1, 2]),
array([3, 4, 5, 6]),
array([], dtype=int32),
array([4, 5, 6, 7, 8, 9]),
array([10])]
The problem can be refactored as choosing I-1 random split points from {1,2,...,P-1}, which can be viewed using stars and bars.
Therefore, it can be implemented as follows:
import numpy as np
split_points = np.random.choice(P - 2, I - 1, replace=False) + 1
split_points.sort()
result = np.split(data, split_points)
np.split is still the way to go. If you pass in a sequence of integers, split will treat them as cut points. Generating random cut points is easy. You can do something like
P = 10
I = 5
data = np.arange(P) + 1
indices = np.random.randint(P, size=I - 1)
You want I - 1 cut points to get I chunks. The indices need to be sorted, and duplicates need to be removed. np.unique does both for you. You may end up with fewer than I chunks this way:
result = np.split(data, indices)
If you absolutely need to have I numbers, choose without resampling. That can be implemented for example via np.shuffle:
indices = np.arange(1, P)
np.random.shuffle(indices)
indices = indices[:I - 1]
indices.sort()
I have two numpy arrays, one larger, one smaller:
a = np.array([[0,1,0],[0,0,1],[0,1,1]])
b = np.array([[0],[1]])
Is there a function that I can use to find the indexes of the larger array where there is an in an instance of the smaller?
Ideal result:
instances[0] = [[2, 0], [2, 1]]
instances[1] = [[1, 1], [1,2]]
Many thanks!
As far as I know there is not fast numpy function that will do this, but you can loop through and check pretty quickly.
def find_instances(a,b):
instances = []
for i in range(a.shape[0] - b.shape[0] + 1):
for j in range(a.shape[1] - b.shape[1] + 1):
if np.all(a[i:i+b.shape[0], j:j+b.shape[1]] == b):
instances.append([i,j])
return instances
Here each instance is the spot in the top left corner of a that matches the top left corner of b. Not quite the output you requested but it's easy enough to get the rest of the indices if you really need them from there. Hope that helps!
When we multiply two matrices A of size m x k and B of size k x n we use the following code:
#for resultant matrix rows
for i in range(m):
#for resultant matrix column
for j in range(n):
for l in range(k):
#A's row x B's columns
c[i][j]=c[i][j]+a[i][l]*b[l][j]
are my comments in the code right explanation of the loops? Is there a better explanation of the loops or is there a better thought process to code matrix multiplication?
EDIT1: I am not looking for a better code. My question is about the thought process that goes in when we transform the math of matrix multiplicate into code.
Your code is correct but if you want to add detail comment/explanation like you ask for you can do so:
#for resultant matrix rows
for i in range(m):
#for resultant matrix column
for j in range(n):
#for each entry in resultant matrix we have k entries to sum
for l in range(k):
#where each i, j entry in the result matrix is given by multiplying the
#entries A[i][l] (across row i of A) by the entries B[l][j] (down
#column j of B), for l = 1, 2, ..., k, and summing the results over l:
c[i][j]=c[i][j]+a[i][l]*b[l][j]
EDIT: if you want a better explanation of the loop or thought process than take out #A's row x B's columns comments. and replace it with "where each i, j entry in the result matrix is given by multiplying the entries A[i][l] (across row i of A) by the entries B[l][j] (down column j of B), for l = 1, 2, ..., k, and summing the results over " also don't use l as an iterator it looks like a 1
You can use numpy.dot function. Here's the documentation. Example (extracted from the documentatio):
> a = [[1, 0], [0, 1]]
> b = [[4, 1], [2, 2]]
> np.dot(a, b)
> array([[4, 1],
[2, 2]])
The condition that should always stand in order to do 2 matrices multiplication is that first matrix must have the same amount of rows that the other matrix has columns.
so if matrix_1 is m x n than second matrix_2 should be n x p. The result of the two will have a dimension of m x p
the Pseudocode will be:
multiplyMatrix(matrix1, matrix2)
-- Multiplies rows and columns and sums them
multiplyRowAndColumn(row, column) returns number
var
total: number
begin
for each rval in row and cval in column
begin
total += rval*cval
end
return total
end
begin
-- If the rows don't match up then the function fails
if matrix1:n != matrix2:m return failure;
dim = matrix1:n -- Could also be matrix2:m
newmat = new squarematrix(dim) -- Create a new dim x dim matrix
for each r in matrix1:rows and c in matrix2:columns
begin
end
end
In python either you can do what you did, or you can use ijk-algo, ikj-algo, psyco ikj-algo, Numpy, or SciPy to accomplish this. It appears that Numpy is the fastest and most efficient.
YOUR CODE LOOKS RIGHT AND YOUR COMMENTS ALSO DO LOOK CORRECT
I'm aware that there's itertools.product for loops, but I wanted to write something that would return an arbitrary coordinate in n-space given the iteration number that would yield it in a loop. I've already written something that's similar, viz.
def clock(iteration_number, axis_lengths):
dimension=len(axis_lengths)
coordinate = []
for i in range(dimension):
s = axis_lengths[dimension-i-1:dimension-i][0]
g = iteration_number % s
iteration_number /= s
coordinate += [g]
return tuple(reversed(coordinate))
but I'm hoping that, with the help of the built-in function divmod (or another) it may be compressed to a list comprehension; I've been trying to use lambda functions and map as well, but to no avail, so I'm stuck. For example, running the above function on an array A with axes lengths [6, 14, 9, 13, 17] (i.e. a 5-dimensional array) for iteration number 98000 results in the coordinate (3, 7, 2, 5, 12). How can I do this, i.e. map a specific iteration number to its location in an n-dimensional array? And again, my goal is not to write another function like that above.
I'm not sure about the example you give (your code yields a different result from the one you quote). But the built-in one-liner to do the same operation is np.unravel_index:
import numpy as np
import operator
def product(iterable):
return reduce(operator.mul, iterable)
def clock_1(iteration_number, axis_lengths):
dimension=len(axis_lengths)
coordinate = []
for i in range(dimension):
s = axis_lengths[dimension-i-1:dimension-i][0]
g = iteration_number % s
iteration_number //= s
coordinate += [g]
return tuple(reversed(coordinate))
def clock_2(iteration_number, axis_lengths):
return np.unravel_index(iteration_number % product(axis_lengths), axis_lengths)
print "clock_1:", clock_1(98000, (6,14,9,3,17))
print "clock_2:", clock_2(98000, (6,14,9,3,17))
clock_1: (3, 3, 4, 1, 12)
clock_2: (3, 3, 4, 1, 12)