The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here

how can ı find to distances of items in the k-means ?

SelimSelim Member Posts: 32 Learner II
hello everybody first off,how are you ? ı have been working on a warehouse lay out projects that ı did clustering with k-means algorthm but ı need to place my items to warehouse so ı need to  find distances rıght now so how can ı find to distances of the items each other ? 
----------------------- xml-------------------------
<?xml version="1.0" encoding="UTF-8"?><process version="9.2.001">
  <context>
    <input/>
    <output/>
    <macros/>
  </context>
  <operator activated="true" class="process" compatibility="9.2.001" expanded="true" name="Process">
    <parameter key="logverbosity" value="init"/>
    <parameter key="random_seed" value="2001"/>
    <parameter key="send_mail" value="never"/>
    <parameter key="notification_email" value=""/>
    <parameter key="process_duration_for_mail" value="30"/>
    <parameter key="encoding" value="SYSTEM"/>
    <process expanded="true">
      <operator activated="true" class="read_excel" compatibility="9.2.001" expanded="true" height="68" name="Read Excel" width="90" x="45" y="85">
        <parameter key="excel_file" value="C:\Users\selimcelebi\Desktop\Yeni klasör\veri.xlsx"/>
        <parameter key="sheet_selection" value="sheet number"/>
        <parameter key="sheet_number" value="1"/>
        <parameter key="imported_cell_range" value="A1"/>
        <parameter key="encoding" value="SYSTEM"/>
        <parameter key="first_row_as_names" value="true"/>
        <list key="annotations"/>
        <parameter key="date_format" value=""/>
        <parameter key="time_zone" value="SYSTEM"/>
        <parameter key="locale" value="English (United States)"/>
        <parameter key="read_all_values_as_polynominal" value="false"/>
        <list key="data_set_meta_data_information">
          <parameter key="0" value="StockCode.true.integer.attribute"/>
          <parameter key="1" value="Description.true.polynominal.attribute"/>
          <parameter key="2" value="weight(gram).true.integer.attribute"/>
          <parameter key="3" value="volume.true.integer.attribute"/>
          <parameter key="4" value="quantity.true.integer.attribute"/>
          <parameter key="5" value="UnitPrice.true.real.attribute"/>
          <parameter key="6" value="fragility.true.integer.attribute"/>
        </list>
        <parameter key="read_not_matching_values_as_missings" value="false"/>
        <parameter key="datamanagement" value="double_array"/>
        <parameter key="data_management" value="auto"/>
      </operator>
      <operator activated="true" class="normalize" compatibility="9.2.001" expanded="true" height="103" name="Normalize" width="90" x="179" y="85">
        <parameter key="return_preprocessing_model" value="false"/>
        <parameter key="create_view" value="false"/>
        <parameter key="attribute_filter_type" value="single"/>
        <parameter key="attribute" value="StockCode"/>
        <parameter key="attributes" value="|fragility|quantity|UnitPrice|volume(cm3)|weight(gram)"/>
        <parameter key="use_except_expression" value="false"/>
        <parameter key="value_type" value="numeric"/>
        <parameter key="use_value_type_exception" value="false"/>
        <parameter key="except_value_type" value="real"/>
        <parameter key="block_type" value="value_series"/>
        <parameter key="use_block_type_exception" value="false"/>
        <parameter key="except_block_type" value="value_series_end"/>
        <parameter key="invert_selection" value="true"/>
        <parameter key="include_special_attributes" value="false"/>
        <parameter key="method" value="range transformation"/>
        <parameter key="min" value="0.0"/>
        <parameter key="max" value="100.0"/>
        <parameter key="allow_negative_values" value="false"/>
      </operator>
      <operator activated="true" class="generate_attributes" compatibility="9.2.001" expanded="true" height="82" name="Generate Attributes" width="90" x="313" y="85">
        <list key="function_descriptions"/>
        <parameter key="keep_all" value="true"/>
      </operator>
      <operator activated="true" class="select_attributes" compatibility="9.2.001" expanded="true" height="82" name="Select Attributes" width="90" x="447" y="85">
        <parameter key="attribute_filter_type" value="subset"/>
        <parameter key="attribute" value="F"/>
        <parameter key="attributes" value="|Description|StockCode"/>
        <parameter key="use_except_expression" value="false"/>
        <parameter key="value_type" value="attribute_value"/>
        <parameter key="use_value_type_exception" value="false"/>
        <parameter key="except_value_type" value="time"/>
        <parameter key="block_type" value="attribute_block"/>
        <parameter key="use_block_type_exception" value="false"/>
        <parameter key="except_block_type" value="value_matrix_row_start"/>
        <parameter key="invert_selection" value="true"/>
        <parameter key="include_special_attributes" value="false"/>
      </operator>
      <operator activated="true" class="select_attributes" compatibility="9.2.001" expanded="true" height="82" name="Select Attributes (2)" width="90" x="648" y="238">
        <parameter key="attribute_filter_type" value="subset"/>
        <parameter key="attribute" value="F"/>
        <parameter key="attributes" value="|Description|StockCode"/>
        <parameter key="use_except_expression" value="false"/>
        <parameter key="value_type" value="attribute_value"/>
        <parameter key="use_value_type_exception" value="false"/>
        <parameter key="except_value_type" value="time"/>
        <parameter key="block_type" value="attribute_block"/>
        <parameter key="use_block_type_exception" value="false"/>
        <parameter key="except_block_type" value="value_matrix_row_start"/>
        <parameter key="invert_selection" value="false"/>
        <parameter key="include_special_attributes" value="false"/>
      </operator>
      <operator activated="true" class="generate_id" compatibility="9.2.001" expanded="true" height="82" name="Generate ID (2)" width="90" x="849" y="238">
        <parameter key="create_nominal_ids" value="false"/>
        <parameter key="offset" value="0"/>
      </operator>
      <operator activated="true" class="set_macros" compatibility="9.2.001" expanded="true" height="82" name="Set Macros" width="90" x="648" y="85">
        <list key="macros">
          <parameter key="cluster_number" value="10"/>
        </list>
      </operator>
      <operator activated="true" class="python_scripting:execute_python" compatibility="9.2.000" expanded="true" height="103" name="Execute Python" width="90" x="782" y="85">
        <parameter key="script" value="import pandas as pd&#10;from operator import itemgetter&#10;import numpy as np&#10;import random&#10;import sys&#10;from scipy.spatial import distance&#10;from sklearn.cluster import KMeans&#10;&#10;&#10;C = %{cluster_number}&#10;&#10;def k_means(X) : &#10;&#10;  kmeans = KMeans(n_clusters=C, random_state=0).fit(X)&#10;  return kmeans.cluster_centers_&#10;&#10;&#10;&#10;&#10;def samesizecluster( D ):&#10;    &quot;&quot;&quot; in: point-to-cluster-centre distances D, Npt x C&#10;            &#10;        out: xtoc, X -&gt; C, equal-size clusters&#10;       &#10;    &quot;&quot;&quot;&#10;       &#10;    Npt, C = D.shape&#10;    clustersize = (Npt + C - 1) // C&#10;    xcd = list( np.ndenumerate(D) )  # ((0,0), d00), ((0,1), d01) ...&#10;    xcd.sort( key=itemgetter(1) )&#10;    xtoc = np.ones( Npt, int ) * -1&#10;    nincluster = np.zeros( C, int )&#10;    nall = 0&#10;    for (x,c), d in xcd:&#10;        if xtoc[x] &lt; 0  and  nincluster[c] &lt; clustersize:&#10;            xtoc[x] = c&#10;            nincluster[c] += 1&#10;            nall += 1&#10;            if nall &gt;= Npt:  break&#10;    return xtoc&#10;&#10;def rm_main(data):&#10; &#10;  data_2 = data.values&#10;  &#10;  centres = k_means(data_2)&#10;  D = distance.cdist( data_2, centres )&#10;  xtoc = samesizecluster( D )&#10;  data['cluster'] = xtoc&#10;&#10;    &#10;  return data"/>
        <parameter key="script_file" value="C:\Users\selimcelebi\Desktop\Yeni klasör\samesizeclustercode.py"/>
        <parameter key="use_default_python" value="true"/>
        <parameter key="package_manager" value="conda (anaconda)"/>
      </operator>
      <operator activated="true" class="set_role" compatibility="9.2.001" expanded="true" height="82" name="Set Role (2)" width="90" x="916" y="85">
        <parameter key="attribute_name" value="cluster"/>
        <parameter key="target_role" value="cluster"/>
        <list key="set_additional_roles"/>
      </operator>
      <operator activated="true" class="generate_id" compatibility="9.2.001" expanded="true" height="82" name="Generate ID" width="90" x="1050" y="85">
        <parameter key="create_nominal_ids" value="false"/>
        <parameter key="offset" value="0"/>
      </operator>
      <operator activated="true" class="concurrency:join" compatibility="9.2.001" expanded="true" height="82" name="Join" width="90" x="1050" y="238">
        <parameter key="remove_double_attributes" value="true"/>
        <parameter key="join_type" value="inner"/>
        <parameter key="use_id_attribute_as_key" value="true"/>
        <list key="key_attributes"/>
        <parameter key="keep_both_join_attributes" value="false"/>
      </operator>
      <operator activated="true" class="multiply" compatibility="9.2.001" expanded="true" height="103" name="Multiply" width="90" x="1251" y="187"/>
      <operator activated="true" class="aggregate" compatibility="9.2.001" expanded="true" height="82" name="Aggregate" width="90" x="1251" y="34">
        <parameter key="use_default_aggregation" value="false"/>
        <parameter key="attribute_filter_type" value="all"/>
        <parameter key="attribute" value=""/>
        <parameter key="attributes" value=""/>
        <parameter key="use_except_expression" value="false"/>
        <parameter key="value_type" value="attribute_value"/>
        <parameter key="use_value_type_exception" value="false"/>
        <parameter key="except_value_type" value="time"/>
        <parameter key="block_type" value="attribute_block"/>
        <parameter key="use_block_type_exception" value="false"/>
        <parameter key="except_block_type" value="value_matrix_row_start"/>
        <parameter key="invert_selection" value="false"/>
        <parameter key="include_special_attributes" value="false"/>
        <parameter key="default_aggregation_function" value="average"/>
        <list key="aggregation_attributes">
          <parameter key="volume" value="sum"/>
        </list>
        <parameter key="group_by_attributes" value="cluster"/>
        <parameter key="count_all_combinations" value="false"/>
        <parameter key="only_distinct" value="false"/>
        <parameter key="ignore_missings" value="true"/>
      </operator>
      <connect from_port="input 1" to_op="Read Excel" to_port="file"/>
      <connect from_op="Read Excel" from_port="output" to_op="Normalize" to_port="example set input"/>
      <connect from_op="Normalize" from_port="example set output" to_op="Generate Attributes" to_port="example set input"/>
      <connect from_op="Generate Attributes" from_port="example set output" to_op="Select Attributes" to_port="example set input"/>
      <connect from_op="Select Attributes" from_port="example set output" to_op="Set Macros" to_port="through 1"/>
      <connect from_op="Select Attributes" from_port="original" to_op="Select Attributes (2)" to_port="example set input"/>
      <connect from_op="Select Attributes (2)" from_port="example set output" to_op="Generate ID (2)" to_port="example set input"/>
      <connect from_op="Generate ID (2)" from_port="example set output" to_op="Join" to_port="left"/>
      <connect from_op="Set Macros" from_port="through 1" to_op="Execute Python" to_port="input 1"/>
      <connect from_op="Execute Python" from_port="output 1" to_op="Set Role (2)" to_port="example set input"/>
      <connect from_op="Set Role (2)" from_port="example set output" to_op="Generate ID" to_port="example set input"/>
      <connect from_op="Generate ID" from_port="example set output" to_op="Join" to_port="right"/>
      <connect from_op="Join" from_port="join" to_op="Multiply" to_port="input"/>
      <connect from_op="Multiply" from_port="output 1" to_op="Aggregate" to_port="example set input"/>
      <connect from_op="Multiply" from_port="output 2" to_port="result 2"/>
      <connect from_op="Aggregate" from_port="example set output" to_port="result 1"/>
      <portSpacing port="source_input 1" spacing="0"/>
      <portSpacing port="source_input 2" spacing="0"/>
      <portSpacing port="sink_result 1" spacing="0"/>
      <portSpacing port="sink_result 2" spacing="0"/>
      <portSpacing port="sink_result 3" spacing="0"/>
    </process>
  </operator>
</process>


Tagged:

Best Answer

Answers

  • David_ADavid_A Administrator, Moderator, Employee-RapidMiner, RMResearcher, Member Posts: 297 RM Research
    Hi @Selim ,

    unfortunately I couldn't check your process without the Excel file and the Python script.
    But if I understood question correctly, you want to know the distance of an item to it's cluster center.

    To do so you can calculate the distance yourself, as I did in the sample process below.

    Best,
    David


    <?xml version="1.0" encoding="UTF-8"?><process version="9.3.000-BETA2"><br>  <context><br>    <input/><br>    <output/><br>    <macros/><br>  </context><br>  <operator activated="true" class="process" compatibility="6.0.002" expanded="true" name="Process" origin="GENERATED_TUTORIAL"><br>    <parameter key="logverbosity" value="init"/><br>    <parameter key="random_seed" value="2001"/><br>    <parameter key="send_mail" value="never"/><br>    <parameter key="notification_email" value=""/><br>    <parameter key="process_duration_for_mail" value="30"/><br>    <parameter key="encoding" value="SYSTEM"/><br>    <process expanded="true"><br>      <operator activated="true" class="retrieve" compatibility="9.3.000-BETA2" expanded="true" height="68" name="Retrieve Iris" origin="GENERATED_TUTORIAL" width="90" x="45" y="187"><br>        <parameter key="repository_entry" value="//Samples/data/Iris"/><br>      </operator><br>      <operator activated="true" class="concurrency:k_means" compatibility="9.0.001" expanded="true" height="82" name="Clustering" origin="GENERATED_TUTORIAL" width="90" x="179" y="187"><br>        <parameter key="add_cluster_attribute" value="true"/><br>        <parameter key="add_as_label" value="false"/><br>        <parameter key="remove_unlabeled" value="false"/><br>        <parameter key="k" value="3"/><br>        <parameter key="max_runs" value="10"/><br>        <parameter key="determine_good_start_values" value="false"/><br>        <parameter key="measure_types" value="BregmanDivergences"/><br>        <parameter key="mixed_measure" value="MixedEuclideanDistance"/><br>        <parameter key="nominal_measure" value="NominalDistance"/><br>        <parameter key="numerical_measure" value="EuclideanDistance"/><br>        <parameter key="divergence" value="SquaredEuclideanDistance"/><br>        <parameter key="kernel_type" value="radial"/><br>        <parameter key="kernel_gamma" value="1.0"/><br>        <parameter key="kernel_sigma1" value="1.0"/><br>        <parameter key="kernel_sigma2" value="0.0"/><br>        <parameter key="kernel_sigma3" value="2.0"/><br>        <parameter key="kernel_degree" value="3.0"/><br>        <parameter key="kernel_shift" value="1.0"/><br>        <parameter key="kernel_a" value="1.0"/><br>        <parameter key="kernel_b" value="0.0"/><br>        <parameter key="max_optimization_steps" value="100"/><br>        <parameter key="use_local_random_seed" value="true"/><br>        <parameter key="local_random_seed" value="1992"/><br>      </operator><br>      <operator activated="true" class="extract_prototypes" compatibility="9.3.000-BETA2" expanded="true" height="82" name="Extract Cluster Prototypes" width="90" x="380" y="34"><br>        <description align="center" color="transparent" colored="false" width="126">Extracts the Cluster Center,&lt;br/&gt;in this case the centroid</description><br>      </operator><br>      <operator activated="true" class="rename_by_replacing" compatibility="9.3.000-BETA2" expanded="true" height="82" name="Rename by Replacing" width="90" x="514" y="34"><br>        <parameter key="attribute_filter_type" value="all"/><br>        <parameter key="attribute" value=""/><br>        <parameter key="attributes" value=""/><br>        <parameter key="use_except_expression" value="false"/><br>        <parameter key="value_type" value="attribute_value"/><br>        <parameter key="use_value_type_exception" value="false"/><br>        <parameter key="except_value_type" value="time"/><br>        <parameter key="block_type" value="attribute_block"/><br>        <parameter key="use_block_type_exception" value="false"/><br>        <parameter key="except_block_type" value="value_matrix_row_start"/><br>        <parameter key="invert_selection" value="false"/><br>        <parameter key="include_special_attributes" value="false"/><br>        <parameter key="replace_what" value="(\w+)"/><br>        <parameter key="replace_by" value="$1_center"/><br>      </operator><br>      <operator activated="true" class="concurrency:join" compatibility="9.3.000-BETA2" expanded="true" height="82" name="Join" width="90" x="648" y="187"><br>        <parameter key="remove_double_attributes" value="true"/><br>        <parameter key="join_type" value="inner"/><br>        <parameter key="use_id_attribute_as_key" value="false"/><br>        <list key="key_attributes"><br>          <parameter key="cluster" value="cluster"/><br>        </list><br>        <parameter key="keep_both_join_attributes" value="false"/><br>      </operator><br>      <operator activated="true" class="generate_attributes" compatibility="9.3.000-BETA2" expanded="true" height="82" name="Generate Attributes" width="90" x="782" y="187"><br>        <list key="function_descriptions"><br>          <parameter key="Cluster Center Distance" value="sqrt((a1 - a1_center)^2+ (a2 - a2_center)^2+ (a3 - a3_center)^2+ (a4 - a4_center)^2)"/><br>        </list><br>        <parameter key="keep_all" value="true"/><br>        <description align="center" color="transparent" colored="false" width="126">Calcute the Euclidean between the examples and the corresponding centroid</description><br>      </operator><br>      <connect from_op="Retrieve Iris" from_port="output" to_op="Clustering" to_port="example set"/><br>      <connect from_op="Clustering" from_port="cluster model" to_op="Extract Cluster Prototypes" to_port="model"/><br>      <connect from_op="Clustering" from_port="clustered set" to_op="Join" to_port="right"/><br>      <connect from_op="Extract Cluster Prototypes" from_port="example set" to_op="Rename by Replacing" to_port="example set input"/><br>      <connect from_op="Rename by Replacing" from_port="example set output" to_op="Join" to_port="left"/><br>      <connect from_op="Join" from_port="join" to_op="Generate Attributes" to_port="example set input"/><br>      <connect from_op="Generate Attributes" from_port="example set output" to_port="result 1"/><br>      <portSpacing port="source_input 1" spacing="0"/><br>      <portSpacing port="sink_result 1" spacing="0"/><br>      <portSpacing port="sink_result 2" spacing="0"/><br>    </process><br>  </operator><br></process><br><br>


Sign In or Register to comment.