# + [markdown] colab_type="text" id="4DbQxcXuzVmc"
# # Scatter plot for EDA (Exploratory Data Analysis)

# + colab={"autoexec": {"startup": false, "wait_interval": 0}, "base_uri": "https://localhost:8080/", "height": 173} colab_type="code" executionInfo={"elapsed": 764, "status": "ok", "timestamp": 1532865915321, "user": {"displayName": "<NAME>", "photoUrl": "//lh5.googleusercontent.com/-AsF_YBOVN9s/AAAAAAAAAAI/AAAAAAAAAAA/qYdp7i1L4LY/s50-c-k-no/photo.jpg", "userId": "112579363612867944767"}, "user_tz": -180} id="nXPMB3fCzUjR" outputId="d202462a-e302-4d3e-b1c5-ae18ba9a2388"
import matplotlib.pyplot as plt
import pandas as pd
import itertools
from sklearn.datasets import load_iris
iris = load_iris()
features = iris.data.T

print("features:", iris.feature_names)
print("target classes:", iris.target_names)

print("features.shape", features.shape)
print("iris.target.shape", iris.target.shape)

# Build dataframe from features and series from target
X = pd.DataFrame(features.T, columns=iris.feature_names)  # note we transpose the feature columns
y = pd.Series(iris.target)

print(y.value_counts())
print(">> 3 target classes, each containing 50 samples")

# + [markdown] colab_type="text" id="iWQk6-KXDVzX"
# ## Scatter plots showing 3 out of 4 features at a time (third feature is the point size)

# + colab={"autoexec": {"startup": false, "wait_interval": 0}, "base_uri": "https://localhost:8080/", "height": 781} colab_type="code" executionInfo={"elapsed": 1921, "status": "ok", "timestamp": 1532865917527, "user": {"displayName": "<NAME>", "photoUrl": "//lh5.googleusercontent.com/-AsF_YBOVN9s/AAAAAAAAAAI/AAAAAAAAAAA/qYdp7i1L4LY/s50-c-k-no/photo.jpg", "userId": "112579363612867944767"}, "user_tz": -180} id="aG5h5-nD5Gfs" outputId="0744ed9e-439a-42fa-d2d3-82c2a614be1b"
all_pairs = list(itertools.combinations(list(X.columns), 3))  # C(4,3) = 4! / (3! * 1!) = 24 / (6 * 1) = 4
print("Feature combinations:")
for i,v in enumerate(all_pairs):
  print(i,v)

print("---")

plt.figure(figsize=(10, 10))
for i,v in enumerate(all_pairs):
  f0 = v[0]
  f1 = v[1]
  f2 = v[2]
  ax = plt.subplot(2, 2, i+1)
  ax.set_title("Point sizes based on: %s" % f2)
  
  plt.scatter(X[f0], X[f1], alpha=0.3, s=100*X[f2], c=iris.target, cmap='jet')
  plt.suptitle('Iris dataset scatter plots of 3 out of 4 features at at time')
  plt.xlabel(f0)
  plt.ylabel(f1)

# + [markdown] colab_type="text" id="L40QRJJ8CK1s"
# **Note how the clusters shown above are separated much better when petal length is used for the y-axis.**

# + [markdown] colab_type="text" id="YnybX2C6Dh_E"
# ## Scatter plots showing 2 out of 4 features at a time

# + colab={"autoexec": {"startup": false, "wait_interval": 0}, "base_uri": "https://localhost:8080/", "height": 693} colab_type="code" executionInfo={"elapsed": 1760, "status": "ok", "timestamp": 1532865919509, "user": {"displayName": "<NAME>", "photoUrl": "//lh5.googleusercontent.com/-AsF_YBOVN9s/AAAAAAAAAAI/AAAAAAAAAAA/qYdp7i1L4LY/s50-c-k-no/photo.jpg", "userId": "112579363612867944767"}, "user_tz": -180} id="LMFU_n5yCO3W" outputId="ede451db-1c0a-4600-d45f-d1e76b0d87a8"
all_pairs = list(itertools.combinations(list(X.columns), 2))  # C(4,2) = 4! / (2! * 2!) = 24 / (2 * 2) = 6
print("Feature combinations:")
for i,v in enumerate(all_pairs):
  print(i,v)

print("---")

plt.figure(figsize=(12, 8))
for i,v in enumerate(all_pairs):
  f0 = v[0]
  f1 = v[1]
  ax = plt.subplot(2, 3, i+1)
  
  plt.scatter(X[f0], X[f1], c=iris.target, cmap='jet')
  plt.suptitle('Iris dataset scatter plots of 2 out of 4 features at at time')
  plt.xlabel(f0)
  plt.ylabel(f1)

# + colab={"autoexec": {"startup": false, "wait_interval": 0}, "base_uri": "https://localhost:8080/", "height": 1236} colab_type="code" executionInfo={"elapsed": 2905, "status": "ok", "timestamp": 1532868731806, "user": {"displayName": "<NAME>", "photoUrl": "//lh5.googleusercontent.com/-AsF_YBOVN9s/<KEY>I/AAAAAAAAAAA/qYdp7i1L4LY/s50-c-k-no/photo.jpg", "userId": "112579363612867944767"}, "user_tz": -180} id="YH-DDcvoE1YF" outputId="e266b131-6ef6-4926-f1e4-fff187f6596b"
# All pair permutations # P(4,2) = 4! / 2! = 24 / 2 = 12
COLS = list(X.columns)
all_pairs = [(f1,f0) for f0 in COLS for f1 in COLS]

print("All feature permuations:")
for i,v in enumerate(all_pairs):
  print(i,v)

print("---")

plt.figure(figsize=(14, 14))
for i,v in enumerate(all_pairs):
  f0 = v[0]
  f1 = v[1]
  ax = plt.subplot(4, 4, i+1)
  
  plt.scatter(X[f0], X[f1], c=iris.target, cmap='jet')
  plt.suptitle('Iris dataset scatter plots of 2 out of 4 features at at time while maintaining feature alignment')
  plt.xlabel(f0)
  plt.ylabel(f1)

# + [markdown] colab_type="text" id="kaE9JazLGwED"
# **Note how the clusters shown above are separated much better based on petal length and/or petal width.**

StarcoderdataJupyterScriptsDedupFiltered