1
$\begingroup$

I ran into a quirk with sklearn's MeanShift that I don't know how to get around.

MeanShift doesn't predictably give the same results on every run, so I wanted to run it multiple times within one program, but the results are the same on every iteration. They are different across program runs, but the same over every loop.

Minimal code sans convert_non_numerical_data(...).

import numpy as np
import pandas as pd
from sklearn.cluster import MeanShift
from sklearn import preprocessing

# EDIT: added on request 
def convert_non_numerical_data_to_numerical(data_frame):
    columns = data_frame.columns.values

    for col in columns:
        text_digit_vals = {}

        if data_frame[col].dtype in [np.int64, np.float64]:
            continue

        x = 0
        for unique in set(data_frame[col].values):
            if unique not in text_digit_vals:
                text_digit_vals[unique] = x
                x += 1

        def convert_to_int(val):
            return text_digit_vals[val]
        data_frame[col] = list(map(convert_to_int, data_frame[col]))

df = pd.read_csv("test_data/titanic.csv")
df.fillna(0, inplace=True)  # bad idea to shove in 0?
convert_non_numerical_data_to_numerical(df)

y = np.array(df["survived"])
X = np.array(df.drop(["survived"], 1))
X = preprocessing.scale(X)

run_data = []
for run_count in range(3):
    clf = MeanShift()
    clf.fit(X)
    run_data.append(clf.labels_.copy())
    clf = None

run_0 = run_data[0]
for other_run in run_data[1:]:
    if np.array_equiv(run_0, other_run):
        print("same")
    else:
        print("different")

Output:

same
same

Any idea what is causing this, or at least how to get around it?

Note:

$\endgroup$

2 Answers 2

0
$\begingroup$

Have you looked at smoothing your dataset with any other methods? What hyper-parameter tuning have you done? Also how did you convert your non_numerical data, just wondering if you can provide pastbin?

$\endgroup$
1
  • $\begingroup$ OP edited to include function. It is a simple indexer. I don't think that this is about data smoothing, but I don't know enough to say for certain. It seems to be an issue of the first run's data sticking around and not changing, or maybe the original clustering centers are static. I don't know why three runs of the program could produce slightly different clustering results, but three iterations in one run of the program produces the same results. $\endgroup$
    – John Cox
    Commented Dec 3, 2018 at 18:59
0
$\begingroup$

A couple of work arounds: 1- Set bin_seeding to True, and/or using random seeds at each iteration. 2- Shuffling the data at each iteration.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.