<jupyter_start><jupyter_text>Experiments Projecting the input data in 3-dimensional space<jupyter_code># load training data
dataset = pd.read_csv('customers.csv')
dataset.head()

# Transform gender attribute to numeric in order to calculate PCA
X = preprocess(dataset)

# Plot the input data projection (the ground truth)
projection = calculate_pca(3, X)
my_plot(projection, "PCA projection on the input data set")<jupyter_output><empty_output><jupyter_text>Create clustering models and choose the best one<jupyter_code>def evaluate_models(estimators, data):
    fignum = 1
    titles = ['5 clusters', '3 clusters', '3 clusters, bad initialization']
    for name, est in estimators:
        est.fit(X)
        labels = est.labels_
        fig = plt.figure(fignum, figsize = (7, 4))
        my_plot(data, name, fig, labels)
        print("Estimator: {}".format(name))
        print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(X, labels, sample_size=1000))
        fignum = fignum + 1
        
# create clustering models
estimators = [('k_means_8', KMeans(n_clusters=8, n_init = 30)),
    ('k_means_5', KMeans(n_clusters=5, n_init = 30)),
    ('k_means_3', KMeans(n_clusters=3, n_init = 30)),
    ('k_means_3_bad_init', KMeans(n_clusters=3, n_init=1, init='random'))]

evaluate_models(estimators, projection)<jupyter_output><empty_output><jupyter_text>Find the most appropriate tribe for a new customer<jupyter_code># Create a test dataset
test_df = preprocess(pd.read_csv('customer_test_data.csv'))

# Show the test instance
test_df.head()

# Predict the cluster on the test dataset, using Kmean with k = 3, which is at the index 2 in our estimators array
_, estimator = estimators[2] 
prediction = estimator.predict(test_df)

# show the predicted cluster id for each test instance 
print(prediction)


# From the list of similar profiles (members of the same cluster)
# show some information about

def pears_stats(model, clusterID, dataset):
    similar_profiles = [idx for idx in range(0, len(model)-1) if model[idx] == clusterID]
    pears = dataset.iloc[similar_profiles]

    # Show mean, std, min value and max value of elements in the same cluster as test instance 0
    return pears, pears.describe().iloc[[1,2,3,7]]

model = estimator.labels_
_, stats = pears_stats(model, prediction[0], dataset)
print("Stats of cluster where test instance '{}' is".format(0))
stats
pears, stats = pears_stats(model, prediction[2], dataset)
print("Stats of cluster where test instance '{}' is".format(2))
stats<jupyter_output>Stats of cluster where test instance '2' is<jupyter_text>Check the stocks mostly invested by a given cluster<jupyter_code>folios = pd.read_csv('portfolios.csv')
folios.head()
similar_profiles = pears.index
stocks = folios.loc[similar_profiles]
s = pd.DataFrame(stocks.sum(axis=0).sort_values(0, ascending=False))
s.plot.bar()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11180339217722396, 'fraction_numerical': 0.016268605053651783, 'mean_word_length': 5.21505376344086, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '484705', 'license': 'MIT', 'n_tokens_mistral': 985, 'n_tokens_neox': 942, 'n_words': 274, 'path': 'basic_ml/notebooks/stocks-recommendation/case.ipynb', 'repo_name': 'jmetzz/ml-laboratory', 'size': '2889'}