Numpy newbie question: matrix creation
Dear all, I am a Newbie with Numpy and I would need some advice on how to create a matrix with certain characteristics : - Every entry should be minimum 0 maximum 1 with a step of 0.1 (legal values are 0,0.1,0.2,0.3 etc) - The number of columns of the matrix is a parameter of this matrix creation algorithm - Only the rows where the sum is 1 must be kept Would great appreciate your advice and suggestions Best Regards Edmondo
On Wed, Sep 25, 2013 at 9:06 AM, Edmondo Porcu
I am a Newbie with Numpy and I would need some advice on how to create a matrix with certain characteristics :
- Every entry should be minimum 0 maximum 1 with a step of 0.1 (legal values are 0,0.1,0.2,0.3 etc)
You can generate random integers between 0 and 10, and divide them by 10: np.random.random_integers(0, 10, (5, 5)) / 10.
- Only the rows where the sum is 1 must be kept
I'm not sure I understand the exact structure you describe. Perhaps an example would help. Stéfan
On Wed, Sep 25, 2013 at 8:06 AM, Edmondo Porcu
Dear all,
I am a Newbie with Numpy and I would need some advice on how to create a
matrix with certain characteristics :
- Every entry should be minimum 0 maximum 1 with a step of 0.1 (legal
values are 0,0.1,0.2,0.3 etc)
- The number of columns of the matrix is a parameter of this matrix
creation algorithm
- Only the rows where the sum is 1 must be kept
Would great appreciate your advice and suggestions
Use a multinomial distribution with n=10, then divide by 10.0. For example, with 5 columns and 20 samples: [~] |4> np.random.multinomial(10, np.ones(5)/5.0, size=20) / 10.0 array([[ 0.1, 0.1, 0.4, 0.1, 0.3], [ 0.2, 0.4, 0.1, 0.3, 0. ], [ 0.3, 0.2, 0. , 0.2, 0.3], [ 0. , 0.4, 0.4, 0. , 0.2], [ 0.2, 0.3, 0.1, 0.2, 0.2], [ 0.1, 0.5, 0.1, 0.1, 0.2], [ 0.4, 0. , 0.2, 0.3, 0.1], [ 0.3, 0.3, 0.1, 0. , 0.3], [ 0. , 0.2, 0.5, 0.2, 0.1], [ 0.5, 0.1, 0. , 0.2, 0.2], [ 0.1, 0.3, 0.2, 0.2, 0.2], [ 0.3, 0.1, 0.2, 0.2, 0.2], [ 0.2, 0.3, 0. , 0.3, 0.2], [ 0.2, 0.3, 0.2, 0.3, 0. ], [ 0.1, 0.3, 0.2, 0. , 0.4], [ 0.1, 0.1, 0.2, 0.5, 0.1], [ 0.1, 0.6, 0. , 0. , 0.3], [ 0.1, 0.2, 0.4, 0.1, 0.2], [ 0.3, 0.1, 0.2, 0.2, 0.2], [ 0.2, 0.1, 0.4, 0.2, 0.1]]) -- Robert Kern
That's what I was looking for, except that I want to be sure to generate
all the possible combinations, and to have no repeated values.
Thanks
Edmondo
2013/9/25 Robert Kern
On Wed, Sep 25, 2013 at 8:06 AM, Edmondo Porcu
wrote: Dear all,
I am a Newbie with Numpy and I would need some advice on how to create a
matrix with certain characteristics :
- Every entry should be minimum 0 maximum 1 with a step of 0.1 (legal
values are 0,0.1,0.2,0.3 etc)
- The number of columns of the matrix is a parameter of this matrix
creation algorithm
- Only the rows where the sum is 1 must be kept
Would great appreciate your advice and suggestions
Use a multinomial distribution with n=10, then divide by 10.0.
For example, with 5 columns and 20 samples:
[~] |4> np.random.multinomial(10, np.ones(5)/5.0, size=20) / 10.0 array([[ 0.1, 0.1, 0.4, 0.1, 0.3], [ 0.2, 0.4, 0.1, 0.3, 0. ], [ 0.3, 0.2, 0. , 0.2, 0.3], [ 0. , 0.4, 0.4, 0. , 0.2], [ 0.2, 0.3, 0.1, 0.2, 0.2], [ 0.1, 0.5, 0.1, 0.1, 0.2], [ 0.4, 0. , 0.2, 0.3, 0.1], [ 0.3, 0.3, 0.1, 0. , 0.3], [ 0. , 0.2, 0.5, 0.2, 0.1], [ 0.5, 0.1, 0. , 0.2, 0.2], [ 0.1, 0.3, 0.2, 0.2, 0.2], [ 0.3, 0.1, 0.2, 0.2, 0.2], [ 0.2, 0.3, 0. , 0.3, 0.2], [ 0.2, 0.3, 0.2, 0.3, 0. ], [ 0.1, 0.3, 0.2, 0. , 0.4], [ 0.1, 0.1, 0.2, 0.5, 0.1], [ 0.1, 0.6, 0. , 0. , 0.3], [ 0.1, 0.2, 0.4, 0.1, 0.2], [ 0.3, 0.1, 0.2, 0.2, 0.2], [ 0.2, 0.1, 0.4, 0.2, 0.1]])
-- Robert Kern
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Wed, Sep 25, 2013 at 1:12 PM, Edmondo Porcu
That's what I was looking for, except that I want to be sure to generate
all the possible combinations, and to have no repeated values. Okay, then you need to find all of the integer partitions of 10 with `ncols` elements (padding with 0s for those partitions with fewer than `ncols` elements), finding the permutations of each padded partition, then eliminating duplicates. import itertools import numpy as np def gen_partitions(n): """ Generate integer partitions for `n`. http://homepages.ed.ac.uk/jkellehe/partitions.php """ a = [0 for i in range(n + 1)] k = 1 y = n - 1 while k != 0: x = a[k - 1] + 1 k -= 1 while 2*x <= y: a[k] = x y -= x k += 1 l = k + 1 while x <= y: a[k] = x a[l] = y yield a[:k + 2] x += 1 y -= 1 a[k] = x + y y = x + y - 1 yield a[:k + 1] def gen_permuted_partitions(ncols, n=10): for partition in gen_partitions(n): partition = list(partition) if len(partition) > ncols: continue partition.extend([0] * (ncols - len(partition))) for x in itertools.permutations(partition): yield x def compute_matrix(ncols): return np.array(sorted(set(gen_permuted_partitions(ncols)))) / 10.0 -- Robert Kern
On 9/25/2013 3:06 AM, Edmondo Porcu wrote:
advice on how to create a matrix with certain characteristics : - Every entry should be minimum 0 maximum 1 with a step of 0.1 (legal values are 0,0.1,0.2,0.3 etc) - The number of columns of the matrix is a parameter of this matrix creation algorithm - Only the rows where the sum is 1 must be kept
This is not nearly enough information. But since it is all you offer, here is an answer for a square matrix with parameter n: np.identity(n) Alan Isaac
participants (4)
-
Alan G Isaac
-
Edmondo Porcu
-
Robert Kern
-
Stéfan van der Walt