def jenks(data, num_breaks):
data = numpy.ma.compressed(data)<EOL>if len(data) > <NUM_LIT:1000>:<EOL><INDENT>data.sort()<EOL>ls = numpy.linspace(<NUM_LIT:0>, len(data)-<NUM_LIT:1>, <NUM_LIT:1000>)<EOL>ls = [int(round(x)) for x in ls]<EOL>data_list = data[ls]<EOL><DEDENT>else:<EOL><INDENT>data_list = data<EOL><DEDENT>data_list.sort()<EOL>mat1 = []<EOL>for i in range(<NUM_LIT:0>, len(data_list) + <NUM_LIT:1>):<EOL><INDENT>temp = []<EOL>for j in range(<NUM_LIT:0>, num_breaks + <NUM_LIT:1>):<EOL><INDENT>temp.append(<NUM_LIT:0>)<EOL><DEDENT>mat1.append(temp)<EOL><DEDENT>mat2 = []<EOL>for i in range(<NUM_LIT:0>, len(data_list) + <NUM_LIT:1>):<EOL><INDENT>temp = []<EOL>for j in range(<NUM_LIT:0>, num_breaks + <NUM_LIT:1>):<EOL><INDENT>temp.append(<NUM_LIT:0>)<EOL><DEDENT>mat2.append(temp)<EOL><DEDENT>for i in range(<NUM_LIT:1>, num_breaks + <NUM_LIT:1>):<EOL><INDENT>mat1[<NUM_LIT:1>][i] = <NUM_LIT:1><EOL>mat2[<NUM_LIT:1>][i] = <NUM_LIT:0><EOL>for j in range(<NUM_LIT:2>, len(data_list) + <NUM_LIT:1>):<EOL><INDENT>mat2[j][i] = float('<STR_LIT>')<EOL><DEDENT><DEDENT>v = <NUM_LIT:0.0><EOL>for l in range(<NUM_LIT:2>, len(data_list) + <NUM_LIT:1>):<EOL><INDENT>s1 = <NUM_LIT:0.0><EOL>s2 = <NUM_LIT:0.0><EOL>w = <NUM_LIT:0.0><EOL>for m in range(<NUM_LIT:1>, l + <NUM_LIT:1>):<EOL><INDENT>i3 = l - m + <NUM_LIT:1><EOL>val = float(data_list[i3-<NUM_LIT:1>])<EOL>s2 += val * val<EOL>s1 += val<EOL>w += <NUM_LIT:1><EOL>v = s2 - (s1 * s1) / w<EOL>i4 = i3 - <NUM_LIT:1><EOL>if i4 != <NUM_LIT:0>:<EOL><INDENT>for j in range(<NUM_LIT:2>, num_breaks + <NUM_LIT:1>):<EOL><INDENT>if mat2[l][j] >= (v + mat2[i4][j - <NUM_LIT:1>]):<EOL><INDENT>mat1[l][j] = i3<EOL>mat2[l][j] = v + mat2[i4][j - <NUM_LIT:1>]<EOL><DEDENT><DEDENT><DEDENT><DEDENT>mat1[l][<NUM_LIT:1>] = <NUM_LIT:1><EOL>mat2[l][<NUM_LIT:1>] = v<EOL><DEDENT>k = len(data_list)<EOL>kclass = []<EOL>for i in range(<NUM_LIT:0>, num_breaks + <NUM_LIT:1>):<EOL><INDENT>kclass.append(<NUM_LIT:0>)<EOL><DEDENT>kclass[num_breaks] = float(data_list[len(data_list) - <NUM_LIT:1>])<EOL>count_num = num_breaks<EOL>while count_num >= <NUM_LIT:2>:<EOL><INDENT>id = int((mat1[k][count_num]) - <NUM_LIT:2>)<EOL>kclass[count_num - <NUM_LIT:1>] = data_list[id]<EOL>k = int((mat1[k][count_num] - <NUM_LIT:1>))<EOL>count_num -= <NUM_LIT:1><EOL><DEDENT>return [float(x) for x in kclass][<NUM_LIT:1>:]<EOL>
Calculate Jenks natural breaks.

Adapted from http://danieljlewis.org/files/2010/06/Jenks.pdf
Credit: Daniel Lewis

Arguments:
data -- Array of values to classify.
num_breaks -- Number of breaks to perform.