<jupyter_start><jupyter_text>Testing package with Iris dataset  Importing packages<jupyter_code># Import python_som
# Import libraries for plotting results
import matplotlib.pyplot as plt
# Import NumPy and Pandas for storing data
import numpy as np
import seaborn as sns

import python_som

# Import timing tools
import time<jupyter_output><empty_output><jupyter_text>Loading dataset w/ Seaborn<jupyter_code># Load Iris dataset and columns of features and labels
iris = sns.load_dataset('iris')
target = iris.iloc[:, -1].to_numpy()
iris = iris.iloc[:, :-1].to_numpy()
# Transform labels into numeric codes for plotting
tg = np.zeros(len(target), dtype=int)
tg[target == 'setosa'] = 0
tg[target == 'versicolor'] = 1
tg[target == 'virginica'] = 2<jupyter_output><empty_output><jupyter_text>SOM Instantiation<jupyter_code>help(python_som.SOM.__init__)
# Instantiate SOM from  python_som
# Selecting shape automatically (providing dataset for constructor)
# Using default decay and distance functions
# Using gaussian neighborhood function
# Using cyclic arrays in the vertical and horizontal directions
som = python_som.SOM(x=20, y=None, input_len=iris.shape[1], learning_rate=0.5, neighborhood_radius=1.0,
                     neighborhood_function='gaussian', cyclic_x=True, cyclic_y=True, data=iris)
# # Instantiate SOM from  python_som
# # With fixed shape
# # Using default decay and distance functions
# # Using gaussian neighborhood function
# # Not using cyclic arrays
# som = python_som.SOM(x=8, y=8, input_len=iris.shape[1], learning_rate=0.5, neighborhood_radius=1.0,
#                      neighborhood_function='gaussian', cyclic_x=False, cyclic_y=False)<jupyter_output><empty_output><jupyter_text>Weight initialization<jupyter_code>help(som.weight_initialization)
# # Initialize weights of the SOM with random initialization, from uniform distribution
# som.weight_initialization(mode='random', sample_mode='uniform')
# # Initialize weights of the SOM with sampling from dataset
# som.weight_initialization(mode='sample', data=iris)
# Initialize weights of the SOM with linear initialization
som.weight_initialization(mode='linear', data=iris)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>help(som.train)
# # Training with stepwise algorithm, with random sampling from the dataset
# t_start = time.time()
# q_error = som.train(iris, 1000 * len(iris), mode='random', verbose=False)
# t_end = time.time()
# print("Quantization error:", q_error)
# print("Execution time:", t_end-t_start, "s")
# # Training with stepwise algorithm, with sequential sampling from the dataset
# t_start = time.time()
# q_error = som.train(iris, 1000 * len(iris), mode='sequential', verbose=False)
# t_end = time.time()
# print("Quantization error:", q_error)
# print("Execution time:", t_end-t_start, "s")
# Training with batch algorithm
t_start = time.time()
q_error = som.train(iris, len(iris), mode='batch', verbose=False)
t_end = time.time()
print("Quantization error:", q_error)
print("Execution time:", t_end-t_start, "s")<jupyter_output>Quantization error: 0.11333343581048994
Execution time: 26.523814916610718 s<jupyter_text>Plotting U-matrix<jupyter_code>help(som.distance_matrix)
# Calculating distance matrix for plotting
umatrix = som.distance_matrix().T

# Plotting U-matrix with seaborn/matplotlib
plt.figure(figsize=som.get_shape())
plt.pcolor(umatrix, cmap='bone_r')

markers = ['o', 's', 'D']
colors = ['C0', 'C1', 'C2']
for cnt, xx in enumerate(iris):
    w = som.winner(xx)  # getting the winner
    plt.plot(w[0] + .5, w[1] + .5, markers[tg[cnt]], markerfacecolor='None',
             markeredgecolor=colors[tg[cnt]], markersize=12, markeredgewidth=2)
plt.axis([0, som.get_shape()[0], 0, som.get_shape()[1]])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12516538766869542, 'fraction_numerical': 0.020111140513363324, 'mean_word_length': 6.35408560311284, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '377604', 'license': 'MIT', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1240, 'n_words': 312, 'path': 'test_iris.ipynb', 'repo_name': 'andremsouza/python-som', 'size': '3779'}