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 ?
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-------------------------
<?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 from operator import itemgetter import numpy as np import random import sys from scipy.spatial import distance from sklearn.cluster import KMeans C = %{cluster_number} def k_means(X) : kmeans = KMeans(n_clusters=C, random_state=0).fit(X) return kmeans.cluster_centers_ def samesizecluster( D ): """ in: point-to-cluster-centre distances D, Npt x C out: xtoc, X -> C, equal-size clusters """ Npt, C = D.shape clustersize = (Npt + C - 1) // C xcd = list( np.ndenumerate(D) ) # ((0,0), d00), ((0,1), d01) ... xcd.sort( key=itemgetter(1) ) xtoc = np.ones( Npt, int ) * -1 nincluster = np.zeros( C, int ) nall = 0 for (x,c), d in xcd: if xtoc[x] < 0 and nincluster[c] < clustersize: xtoc[x] = c nincluster[c] += 1 nall += 1 if nall >= Npt: break return xtoc def rm_main(data): data_2 = data.values centres = k_means(data_2) D = distance.cdist( data_2, centres ) xtoc = samesizecluster( D ) data['cluster'] = xtoc 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:
0
Best Answer
-
David_A Administrator, Moderator, Employee-RapidMiner, RMResearcher, Member Posts: 297 RM ResearchOk,found an easier solution by using the already existing "Cross Distance" operator (thx @mschmitz for the hint).
<?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="313" y="34"><br> <description align="center" color="transparent" colored="false" width="126">Extracts the Cluster Center,<br/>in this case the centroid</description><br> </operator><br> <operator activated="true" class="cross_distances" compatibility="9.3.000-BETA2" expanded="true" height="103" name="Cross Distances" width="90" x="581" y="187"><br> <parameter key="measure_types" value="MixedMeasures"/><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="GeneralizedIDivergence"/><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="only_top_k" value="false"/><br> <parameter key="k" value="10"/><br> <parameter key="search_for" value="nearest"/><br> <parameter key="compute_similarities" value="false"/><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="Cross Distances" to_port="reference set"/><br> <connect from_op="Extract Cluster Prototypes" from_port="example set" to_op="Cross Distances" to_port="request set"/><br> <connect from_op="Cross Distances" from_port="result set" 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> <portSpacing port="sink_result 3" spacing="0"/><br> <description align="center" color="yellow" colored="false" height="129" resized="true" width="218" x="533" y="298">The &quot;request&quot; column is the cluster ID and the &quot;document&quot; is the example.<br/><br/>cluster_0 is then request 1</description><br> </process><br> </operator><br></process><br><br>
6
Answers
David