top of page

What Is Spearman's Rank Coefficient In Python Machine Learning?

Spearman's rank coefficient is used for discrete/ordinal data.


Example

Dataset:


link_to_data = 'https://github.com/tuliplab/mds/raw/master/Jupyter/data/Auto.csv'
DataSet = wget.download(link_to_data)
data = pd.read_csv('Auto.csv')
data


Find the Spearman's rank between horse power and number of cylinders of the car data.


#horse = np.array([float(dd[4]) for dd in data[1:]])
#cylinder = np.array([float(dd[2]) for dd in data[1:]])
horse = data['Horse power']
cylinder = data['cylinder number']

fig, ax = plt.subplots(figsize=(7, 5), dpi=300)
ax.scatter(horse, cylinder, alpha=0.6, edgecolor='none', s=100)
ax.set_xlabel('Horse power')
ax.set_ylabel('#Cylinders')

print(scipy.stats.spearmanr(horse, cylinder))

Output:


bottom of page