<jupyter_start><jupyter_text>Species Segmentation with Cluster Analysis The Iris flower dataset is one of the most popular ones for machine learning. You can read a lot about it online and have probably already heard of it: https://en.wikipedia.org/wiki/Iris_flower_data_setWe didn't want to use it in the lectures, but believe that it would be very interesting for you to try it out (and maybe read about it on your own).There are 4 features: sepal length, sepal width, petal length, and petal width.Start by creating 2 clusters. Then standardize the data and try again. Does it make a difference?Use the Elbow rule to determine how many clusters are there.  Import the relevant libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.cluster import KMeans<jupyter_output><empty_output><jupyter_text>Load the data Load data from the csv file:  'iris_dataset.csv'.<jupyter_code>df = pd.read_csv('iris-dataset.csv')
df.head()<jupyter_output><empty_output><jupyter_text>Plot the data For this exercise, try to cluster the iris flowers by the shape of their sepal.  Use the 'sepal_length' and 'sepal_width' variables.<jupyter_code>plt.scatter(df['sepal_length'],df['sepal_width'])<jupyter_output><empty_output><jupyter_text>Clustering (unscaled data) Separate the original data into 2 clusters.<jupyter_code>x=df
kmeans= KMeans(2)
kmeans.fit(x)
df['Cluster'] = kmeans.fit_predict(x)

plt.scatter(df['sepal_length'],df['sepal_width'],c=df['Cluster'],cmap='rainbow')<jupyter_output><empty_output><jupyter_text>Standardize the variables Import and use the  method  function from sklearn to standardize the data.<jupyter_code>from sklearn import preprocessing
xs = preprocessing.scale(df)<jupyter_output><empty_output><jupyter_text>Clustering (scaled data)<jupyter_code>df
kmeans = KMeans(2)
kmeans.fit(xs)
df['Cluster'] = kmeans.fit_predict(xs)

plt.scatter(df['sepal_length'],df['sepal_width'],c=df['Cluster'],cmap='rainbow')<jupyter_output><empty_output><jupyter_text>Take Advantage of the Elbow Method  WCSS<jupyter_code>wcss = []

for i in range(1,10):
    kmeans = KMeans(i)
    kmeans.fit(xs)
    wcssi = kmeans.inertia_
    wcss.append(wcssi)
wcss<jupyter_output><empty_output><jupyter_text>The Elbow Method<jupyter_code>num=range(1,10)
plt.plot(num,wcss)
df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10033726812816189, 'fraction_numerical': 0.00463743676222597, 'mean_word_length': 7.04406779661017, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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': '479490', 'license': 'MIT', 'n_tokens_mistral': 841, 'n_tokens_neox': 809, 'n_words': 224, 'path': 'Resources/Data-Science/Machine-Learning/K-Means/Species Segmentation with Cluster Analysis Part 1.ipynb', 'repo_name': 'nate4bs/DataRepo', 'size': '2372'}