To write a python program to implement K-Means Clustering Algorithm.
- Hardware – PCs
- Anaconda – Python 3.7 Installation
Load the CSV into a DataFrame.
Print the number of contents to be displayed using df.head().
The number of rows returned is defined in pandas option settings.
Check your system's maximum column with the pd.options.display.max_column statement.
Increase the maximum number of rows to display the entire DataFrame.
#Name:Praneet.S
#Ref. No:21500603
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
x1 = pd.read_csv('clustering.csv.csv')
print(x1.head(2))
x2 = x1.loc[:, ['ApplicantIncome', 'LoanAmount']]
print(x2.head(2))
x = x2.values
sns.scatterplot(x[:,0], x[:, 1])
plt.xlabel('Income')
plt.ylabel('Loan')
plt.show()
kmean=KMeans(n_clusters=4)
kmean.fit(x)
print('Cluster Centers:', kmean.cluster_centers_)
print('Labels:', kmean.labels_)
predicted_class = kmean.predict([[9000,120]])
print('The cluster group for Applicant Income 9000 and loanamount 120'
Thus the K-means clustering algorithm is implemented and predicted the cluster class using python program.