Translating Matlab (Octave) group coloring code into python (numpy, pyplot) -


i want translate following group coloring octave function python , use pyplot.

function input:
x - data matrix (m x n)
a - parameter.
index - vector of size "m" values in range [: a]
(for example if = 4, index can [random.choice(range(4)) in range(m)]

the values in "index" indicate number of group "m"th data point belongs to. function should plot data points x , color them in different colors (number of different colors "a").

the function in octave:

p = hsv(a); % x 3 metrix colors = p(index, :); % ****this m x 3 metrix**** scatter(x(:,1), x(:,2), 10, colors); 

i couldn't find function hsv in python, wrote myself (i think did..):

 p = colors.hsv_to_rgb(numpy.column_stack(( numpy.linspace(0, 1, a), numpy.ones((a ,2)) )) ) 

but can't figure out how matrix selection p(index, :) in python (numpy). specially because size of "index" bigger "a".

thanks in advance help.

so, want take m x 3 of hsv values, , convert each row rgb?

import numpy np import colorsys mymatrix = np.matrix([[11,12,13],                       [21,22,23],                       [31,32,33]])  def to_hsv(x):     return colorsys.rgb_to_hsv(*x)  #apply to_hsv function each matrix row. print np.apply_along_axis(to_hsv, axis=1, arr=mymatrix) 

this produces:

[[  0.5   0.   13. ]  [  0.5   0.   23. ]  [  0.5   0.   33. ]] 

follow through on comment:

if understand have matrix p a x 3 matrix, , want randomly select rows matrix on , on again, until have new matrix m x 3?

ok. let's have matrix p defined follows:

a = 5 p = np.random.randint(5, size=(a, 3)) 

now, make list of random integers between range 0 -> 3 (index starts @ 0 , ends a-1), m in length:

m = 20 index = np.random.randint(a, size=m) 

now access right indexes , plug them new matrix:

p_prime = np.matrix([p[i] in index]) 

produces 20 x 3 matrix.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -