Ndarray list list

I am trying to use kmeans clusters in scipy, exactly the one that is present here:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html#scipy.cluster.vq.kmeans

I am trying to convert a list of a list, for example:

data without_x[ [0, 0, 0, 0, 0, 0, 0, 20.0, 1.0, 48.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1224.0, 125.5, 3156.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 22.5, 56.0, 41.5, 85.5, 0, 0, 0, 0, 0, 0, 0, 0, 1495.0, 3496.5, 2715.0, 5566.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] 

in ndarry to use it with the Kmeans method. When I try to convert the list of the list to ndarray, I get an empty array, thereby invalidating the whole analysis. The length of the ndarray is variable and depends on the number of samples collected. But I can easily figure it out with Len (data_without_x)

Here is a snippet of code that returns an empty list.

 import numpy as np import "other functions" data, data_without_x = data_preparation.generate_sampled_pdf() nodes_stats, k, list_of_list= result_som.get_number_k() data_array = np.array(data_without_x) whitened = whiten(data_array) centroids, distortion = kmeans(whitened, int(k), iter=100000) 

and this is what I get as output by simply storing in a simple log file:

 ___________________________ this is the data array[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]] ___________________________ This is the whitened array[[ nan nan nan ..., nan nan nan] [ nan nan nan ..., nan nan nan] [ nan nan nan ..., nan nan nan] ..., [ nan nan nan ..., nan nan nan] [ nan nan nan ..., nan nan nan] [ nan nan nan ..., nan nan nan]] ___________________________ 

Does anyone know what happens when I try to convert a list of a list to numpy.array?

thanks for the help

+4
source share
3 answers

Exactly how to convert list of lists to ndarray in python. Are you sure your_without_x data is populated correctly? On my car:

 data = [[1,2,3,4],[4,5,6,7,8]] data_arr = np.array(data) data_arr array([[1,2,3,4], [5,6,7,8]]) 

What kind of behavior i think you expect

Looking at your input, you have a lot of zeros ... keep in mind that the listing does not display all of this. You can just see all the zeros from your input. Examine a specific non-zero element to be sure

+3
source

vq.whiten and vq.kmeans expect an array of form (M, N) , where each row is an observation. So transpose your data_array :

 import numpy as np import scipy.cluster.vq as vq np.random.seed(2013) data_without_x = [ [0, 0, 0, 0, 0, 0, 0, 20.0, 1.0, 48.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1224.0, 125.5, 3156.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 22.5, 56.0, 41.5, 85.5, 0, 0, 0, 0, 0, 0, 0, 0, 1495.0, 3496.5, 2715.0, 5566.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] data_array = np.array(data_without_x).T whitened = vq.whiten(data_array) centroids, distortion = vq.kmeans(whitened, 5) print(centroids) 

gives

 [[ 1.22649791e+00 2.69573144e+00] [ 3.91943108e-03 5.57406434e-03] [ 5.73668382e+00 4.83161524e+00] [ 0.00000000e+00 1.29763133e+00]] 
0
source

use asarray numpy function. It is simple: Link: https://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html

0
source

Source: https://habr.com/ru/post/1489495/


All Articles