pyclustering  0.10.1
pyclustring is a Python, C++ data mining library.
__init__.py
1 """!
2 
3 @brief pyclustering module for samples.
4 
5 @authors Andrei Novikov (pyclustering@yandex.ru)
6 @date 2014-2020
7 @copyright BSD-3-Clause
8 
9 """
10 
11 
13  """!
14  @brief Answer reader for samples that are used by pyclustering library.
15 
16  """
17 
18  def __init__(self, answer_path):
19  """!
20  @brief Creates instance of answer reader to read proper clustering results of samples.
21 
22  @param[in] answer_path (string): Path to clustering results (answers).
23 
24  """
25  self.__answer_path = answer_path
26  self.__clusters = None
27  self.__noise = None
28 
29 
30  def get_clusters(self):
31  """!
32  @brief Read proper clustering results.
33 
34  @return (list) Clusters where each cluster is represented by list of index point from dataset.
35 
36  """
37  self.__read_answer()
38  return self.__clusters
39 
40 
41  def get_noise(self):
42  """!
43  @brief Read proper clustering results
44 
45  @return (list) Noise where each outlier is represented by index point from dataset.
46 
47  """
48  self.__read_answer()
49  return self.__noise
50 
51 
53  """!
54  @brief Read proper cluster lengths.
55  @details Cluster length means amount of point in a cluster.
56 
57  @return (list) Cluster lengths where each length means amount of points in a cluster.
58 
59  """
60  clusters = self.get_clusters()
61  return [len(cluster) for cluster in clusters]
62 
63 
64  def __read_answer_from_line(self, index_point, line):
65  """!
66  @brief Read information about point from the specific line and place it to cluster or noise in line with that
67  information.
68 
69  @param[in] index_point (uint): Index point that should be placed to cluster or noise.
70  @param[in] line (string): Line where information about point should be read.
71 
72  """
73 
74  if line[0] == 'n':
75  self.__noise.append(index_point)
76  else:
77  index_cluster = int(line)
78  if index_cluster >= len(self.__clusters):
79  self.__clusters.append([index_point])
80  else:
81  self.__clusters[index_cluster].append(index_point)
82 
83 
84  def __read_answer(self):
85  """!
86  @brief Read information about proper clusters and noises from the file.
87 
88  """
89 
90  if self.__clusters is not None:
91  return
92 
93  file = open(self.__answer_path, 'r')
94 
95  self.__clusters, self.__noise = [], []
96 
97  index_point = 0
98  for line in file:
99  self.__read_answer_from_line(index_point, line)
100  index_point += 1
101 
102  file.close()
pyclustering.samples.answer_reader.get_clusters
def get_clusters(self)
Read proper clustering results.
Definition: __init__.py:30
pyclustering.samples.answer_reader.__init__
def __init__(self, answer_path)
Creates instance of answer reader to read proper clustering results of samples.
Definition: __init__.py:18
pyclustering.samples.answer_reader.get_noise
def get_noise(self)
Read proper clustering results.
Definition: __init__.py:41
pyclustering.samples.answer_reader.get_cluster_lengths
def get_cluster_lengths(self)
Read proper cluster lengths.
Definition: __init__.py:52
pyclustering.samples.answer_reader.__noise
__noise
Definition: __init__.py:27
pyclustering.samples.answer_reader
Answer reader for samples that are used by pyclustering library.
Definition: __init__.py:12
pyclustering.samples.answer_reader.__read_answer
def __read_answer(self)
Read information about proper clusters and noises from the file.
Definition: __init__.py:84
pyclustering.samples.answer_reader.__read_answer_from_line
def __read_answer_from_line(self, index_point, line)
Read information about point from the specific line and place it to cluster or noise in line with tha...
Definition: __init__.py:64
pyclustering.samples.answer_reader.__answer_path
__answer_path
Definition: __init__.py:25
pyclustering.samples.answer_reader.__clusters
__clusters
Definition: __init__.py:26