7

scipy で kmeans クラスタリングを使用しようとしています。まさにここに存在するものです。

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

私がやろうとしているのは、次のようなリストのリストを変換することです:

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]
]

Kmeans メソッドで使用するために ndarry に変換します。list のリストを ndarray に変換しようとすると、空の配列が得られるため、分析全体が無効になります。ndarray の長さは可変で、収集されたサンプルの数によって異なります。しかし、len(data_without_x) で簡単に取得できます

空のリストを返すコードのスニペットを次に示します。

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)

これは、単純なログファイルに保存するだけで出力として得られるものです。

___________________________
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]]
___________________________

リストのリストを numpy.array に変換しようとするとどうなるか、誰にも手掛かりがありますか?

ご協力いただきありがとうございます

4

3 に答える 3

6

これはまさに、Python でリストのリストを ndarray に変換する方法です。data_without_x正しく入力されていますか? 私のマシンで:

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

data_arr
array([[1,2,3,4],
       [5,6,7,8]])

あなたが期待していると思う動作はどれですか

入力を見ると、ゼロがたくさんあります...出力にはすべてが表示されないことに注意してください。入力からすべての「ゼロ」が表示されているだけかもしれません。特定の非ゼロ要素を調べて確認する

于 2013-07-03T13:15:56.910 に答える
0

vq.whiten各行が観測値でvq.kmeansある shape の配列が期待されます。だからあなたの転置:(M, N)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)

収量

[[  1.22649791e+00   2.69573144e+00]
 [  3.91943108e-03   5.57406434e-03]
 [  5.73668382e+00   4.83161524e+00]
 [  0.00000000e+00   1.29763133e+00]]
于 2013-07-03T13:41:44.760 に答える