Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

k-means clustering

# Import the kmeans and vq functions
from scipy.cluster.vq import kmeans, vq

# Set up a random seed in numpy
random.seed([1000,2000])

# Fit the data into a k-means algorithm
cluster_centers,distortion = kmeans(fifa[['scaled_def', 'scaled_phy']], 3)

# Assign cluster labels
fifa['cluster_labels'],distortion_list = vq(fifa[['scaled_def', 'scaled_phy']], cluster_centers)

# Display cluster centers 
print(fifa[['scaled_def', 'scaled_phy', 'cluster_labels']].groupby('cluster_labels').mean())

# Create a scatter plot through seaborn
sns.scatterplot(x='scaled_def', y='scaled_phy', hue='cluster_labels', data=fifa)
plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: string template python 
Python :: tuples vs list 
Python :: comparison python 
Python :: python print font size 
Python :: python generators with for 
Python :: concatenate lists 
Python :: python move files 
Python :: help() python 
Python :: python 3 string length 
Python :: mod in python 
Python :: get number of row dataframe pandas 
Python :: what are for loops 
Python :: web3.py failing not installing 
Python :: spotify recommendations 
Python :: Python OPERATORS, Data Types: LIST, SET, TUPLE, DICTIONARY 
Python :: python change font in 1 line 
Python :: TypeError: Object of type DictProxy is not JSON serializable 
Python :: how to plot quantity of each value of a feature in python 
Python :: Python Program to Find sum Factorial of Number Using Recursion 
Python :: iif python 
Python :: image segmentation pyimagesearch 
Python :: loop in coding 1.2 
Python :: how to select specific column with Dimensionality Reduction pyspark 
Python :: pandas impute with mean of grupby 
Python :: "DO_NOTHING" is not defined django 
Python :: for count in range(size): 
Python :: should i learn c++ or python 
Python :: round to nearest multiple of 5 python from both end 
Python :: jet 4 access python password 
Python :: python3 netifaces get current interface 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =