<jupyter_start><jupyter_text>Kmeans Clustering https://www.kaggle.com/uciml/red-wine-quality-cortez-et-al-2009https://www.javatpoint.com/k-means-clustering-algorithm-in-machine-learning  Importing the libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Importing the data set<jupyter_code>file = "winequality-red.csv"
data = pd.read_csv(file)
data.head()
pd.isnull(data).sum()<jupyter_output><empty_output><jupyter_text>There are no missing values in the data set.<jupyter_code>data.info()
data["quality"].unique()<jupyter_output><empty_output><jupyter_text>We have 6 categories, hence k=**6**  Polarizing the feature columns and dependent variable column<jupyter_code>X = data.drop("quality", axis=1)
y = data["quality"]<jupyter_output><empty_output><jupyter_text>Feature Scaling<jupyter_code>from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
X
X = pd.DataFrame(X)
X<jupyter_output><empty_output><jupyter_text>K means Clustering<jupyter_code>from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=6)
kmeans.fit(X)
clusters = kmeans.predict(X)
clusters
kmeans.cluster_centers_<jupyter_output><empty_output><jupyter_text>Dimension reduction using PCA<jupyter_code>from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_reduced = pd.DataFrame(pca.fit_transform(X), columns=["PCA1","PCA2"])
centers_reduced = pd.DataFrame(pca.transform(kmeans.cluster_centers_), columns=["PCA1","PCA2"])
X_reduced
centers_reduced<jupyter_output><empty_output><jupyter_text>Visualization<jupyter_code>plt.figure(figsize=(7,7))
plt.scatter(x=X_reduced["PCA1"] ,y=X_reduced["PCA2"])
plt.scatter(x=centers_reduced["PCA1"] ,y=centers_reduced["PCA2"], color= "red", marker="*", s=200)
X_reduced["clusters"] = clusters
X_reduced
plt.figure(figsize = (7,7))
plt.scatter(x=X_reduced[X_reduced["clusters"]==0]["PCA1"],y=X_reduced[X_reduced["clusters"]==0]["PCA2"], color="yellow")
plt.scatter(x=X_reduced[X_reduced["clusters"]==1]["PCA1"],y=X_reduced[X_reduced["clusters"]==1]["PCA2"], color="blue")
plt.scatter(x=X_reduced[X_reduced["clusters"]==2]["PCA1"],y=X_reduced[X_reduced["clusters"]==2]["PCA2"], color="purple")
plt.scatter(x=X_reduced[X_reduced["clusters"]==3]["PCA1"],y=X_reduced[X_reduced["clusters"]==3]["PCA2"], color="green")
plt.scatter(x=X_reduced[X_reduced["clusters"]==4]["PCA1"],y=X_reduced[X_reduced["clusters"]==4]["PCA2"], color="cyan")
plt.scatter(x=X_reduced[X_reduced["clusters"]==5]["PCA1"],y=X_reduced[X_reduced["clusters"]==5]["PCA2"], color="violet")
plt.scatter(x=centers_reduced["PCA1"] ,y=centers_reduced["PCA2"], color= "red", marker="*", s=300)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17824663514005093, 'fraction_numerical': 0.019279738086576938, 'mean_word_length': 12.75, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '650268', 'license': 'MIT', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1085, 'n_words': 126, 'path': '5_K_Means_clustering.ipynb', 'repo_name': 'rucha04/example22', 'size': '2749'}