0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

今日の作業記録 python error(言語処理100本ノック:97)未解決

Last updated at Posted at 2019-01-24
./p97.py
Traceback (most recent call last):
  File "./p97.py", line 19, in <module>
    predicts = KMeans(n_clusters = 5).fit_predict(matrix_x300)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/cluster/k_means_.py", line 997, in fit_predict
    return self.fit(X, sample_weight=sample_weight).labels_
  File "/opt/conda/lib/python3.7/site-packages/sklearn/cluster/k_means_.py", line 971, in fit
    return_n_iter=True)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/cluster/k_means_.py", line 311, in k_means
    order=order, copy=copy_x)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py", line 582, in check_array
    context))
ValueError: Found array with 0 sample(s) (shape=(0, 0)) while a minimum of 1 is required.
./p97.py
Traceback (most recent call last):
  File "./p97.py", line 19, in <module>
    predicts = KMeans(n_clusters = 5).fit_predict(matrix_x300)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/cluster/k_means_.py", line 997, in fit_predict
    return self.fit(X, sample_weight=sample_weight).labels_
  File "/opt/conda/lib/python3.7/site-packages/sklearn/cluster/k_means_.py", line 971, in fit
    return_n_iter=True)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/cluster/k_means_.py", line 311, in k_means
    order=order, copy=copy_x)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py", line 582, in check_array
    context))
ValueError: Found array with 0 sample(s) (shape=(0, 0)) while a minimum of 1 is required.
# cat p97.py
#!/usr/bin/env python
# coding: utf-8

#97
import pickle
from collections import OrderedDict
from scipy import io
import numpy as np
from sklearn.cluster import KMeans

fname_dict_index_t = 'dict_index_country'
fname_matrix_x300 = 'matrix_x300_country'

with open(fname_dict_index_t, 'rb') as data_file:
    dict_index_t = pickle.load(data_file)

matrix_x300 = io.loadmat(fname_matrix_x300)['matrix_x300']

predicts = KMeans(n_clusters = 5).fit_predict(matrix_x300)

result = zip(dict_index_t.keys(), predicts)

for country, category in sorted(result, key = lambda x: x[1]):
    print('{}\t{}'.format(category, country))
# ./p98.py
/opt/conda/lib/python3.7/site-packages/scipy/cluster/hierarchy.py:482: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  return linkage(y, method='ward', metric='euclidean')
Traceback (most recent call last):
  File "./p98.py", line 21, in <module>
    ward = ward(matrix_x300)
  File "/opt/conda/lib/python3.7/site-packages/scipy/cluster/hierarchy.py", line 482, in ward
    return linkage(y, method='ward', metric='euclidean')
  File "/opt/conda/lib/python3.7/site-packages/scipy/cluster/hierarchy.py", line 716, in linkage
    n = int(distance.num_obs_y(y))
  File "/opt/conda/lib/python3.7/site-packages/scipy/spatial/distance.py", line 2276, in num_obs_y
    raise ValueError("The number of observations cannot be determined on "
ValueError: The number of observations cannot be determined on an empty distance matrix.
p98.py
#!/usr/bin/env python
# coding: utf-8

#98
import pickle
from collections import OrderedDict
from scipy import io
import numpy as np

from scipy.cluster.hierarchy import ward, dendrogram
from matplotlib import pyplot as plt

fname_dict_index_t = 'dict_index_country'
fname_matrix_x300 = 'matrix_x300_country'

with open(fname_dict_index_t, 'rb') as data_file:
    dict_index_t = pickle.load(data_file)

matrix_x300 = io.loadmat(fname_matrix_x300)['matrix_x300']

ward = ward(matrix_x300)
print(ward)

dendrogram(ward, labels = list(dict_index_t.keys()), leaf_font_size = 8)
plt.show()
#./p99.py
Traceback (most recent call last):
  File "./p99.py", line 21, in <module>
    t_sne = TSNE(perplexity = 30, learning_rate = 500).fit_transform(matrix_x300)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/manifold/t_sne.py", line 894, in fit_transform
    embedding = self._fit(X)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/manifold/t_sne.py", line 693, in _fit
    dtype=[np.float32, np.float64])
  File "/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py", line 582, in check_array
    context))
ValueError: Found array with 0 sample(s) (shape=(0, 0)) while a minimum of 2 is required.
#!/usr/bin/env python
# coding: utf-8
#99
import pickle
from collections import OrderedDict
from scipy import io
import numpy as np

from sklearn.manifold import TSNE
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans

fname_dict_index_t = 'dict_index_country'
fname_matrix_x300 = 'matrix_x300_country'

with open(fname_dict_index_t, 'rb') as data_file:
    dict_index_t = pickle.load(data_file)

matrix_x300 = io.loadmat(fname_matrix_x300)['matrix_x300']

t_sne = TSNE(perplexity = 30, learning_rate = 500).fit_transform(matrix_x300)
print(t_sne)

predicts = KMeans(n_clusters = 5).fit_predict(matrix_x300)

fig, ax = plt.subplots()
cmap = plt.get_cmap('Set1')
for index, label in enumerate(dict_index_t.keys()):
    cval = cmap(predicts[index] / 4)
    ax.scatter(t_sne[index, 0], t_sne[index, 1], marker = '.', color = cval)
    ax.annotate(label, xy = (t_sne[index, 0], t_sne[index, 1]), color = cval)

plt.show()

Reference

Ethernet 記事一覧 Ethernet(0)
https://qiita.com/kaizen_nagoya/items/88d35e99f74aefc98794

Wireshark 一覧 wireshark(0)、Ethernet(48)
https://qiita.com/kaizen_nagoya/items/fbed841f61875c4731d0

線網(Wi-Fi)空中線(antenna)(0) 記事一覧(118/300目標)
https://qiita.com/kaizen_nagoya/items/5e5464ac2b24bd4cd001

C++ Support(0) 
https://qiita.com/kaizen_nagoya/items/8720d26f762369a80514

Coding Rules(0) C Secure , MISRA and so on
https://qiita.com/kaizen_nagoya/items/400725644a8a0e90fbb0

Autosar Guidelines C++14 example code compile list(1-169)
https://qiita.com/kaizen_nagoya/items/8ccbf6675c3494d57a76

Error一覧(C/C++, python, bash...) Error(0)
https://qiita.com/kaizen_nagoya/items/48b6cbc8d68eae2c42b8

なぜdockerで機械学習するか 書籍・ソース一覧作成中 (目標100)
https://qiita.com/kaizen_nagoya/items/ddd12477544bf5ba85e2

言語処理100本ノックをdockerで。python覚えるのに最適。:10+12
https://qiita.com/kaizen_nagoya/items/7e7eb7c543e0c18438c4

プログラムちょい替え(0)一覧:4件
https://qiita.com/kaizen_nagoya/items/296d87ef4bfd516bc394

一覧の一覧( The directory of directories of mine.) Qiita(100)
https://qiita.com/kaizen_nagoya/items/7eb0e006543886138f39

プログラマが知っていると良い「公序良俗」
https://qiita.com/kaizen_nagoya/items/9fe7c0dfac2fbd77a945

小川清最終講義、小川清最終講義(再)計画, Ethernet(100) 英語(100) 安全(100)
https://qiita.com/kaizen_nagoya/items/e2df642e3951e35e6a53

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.01 20230526

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?