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:
- Data set from pythonprogramming.net