# history

source:
  def describe(data):
      format = "%-30s %-10s %6s %6s %s"
      print(format %("Column Name", "Data Type", "Null", "Unique", "Unique"))
      print(format %("", "", "Count", "Count", "Values"))
      print('-' * 65)
      for col in data.columns:
          print(format %(col, data[col].dtype, data[col].isnull().sum(),
                        data[col].nunique(), data[col].unique()))

source_raw:
  def describe(data):
      format = "%-30s %-10s %6s %6s %s"
      print(format %("Column Name", "Data Type", "Null", "Unique", "Unique"))
      print(format %("", "", "Count", "Count", "Values"))
      print('-' * 65)
      for col in data.columns:
          print(format %(col, data[col].dtype, data[col].isnull().sum(),
                        data[col].nunique(), data[col].unique()))


source:
  def count_plot(data):
      for col in data.columns:
          sns.countplot(data, x=col)
          plt.show()

source_raw:
  def count_plot(data):
      for col in data.columns:
          sns.countplot(data, x=col)
          plt.show()


source:
  from sklearn.preprocessing import StandardScaler
  scaling = StandardScaler()
  scaling.fit_transform(df)

source_raw:
  from sklearn.preprocessing import StandardScaler
  scaling = StandardScaler()
  scaling.fit_transform(df)


source:
  from sklearn.preprocessing import LabelEncoder
  le = LabelEncoder()
  iris['Code'] = le.fit_transform(iris.species)
  iris.head()

source_raw:
  from sklearn.preprocessing import LabelEncoder
  le = LabelEncoder()
  iris['Code'] = le.fit_transform(iris.species)
  iris.head()


source:
  def count_plot(data):
      for col in data.columns:
          sns.countplot(data[col])  #TODO: fix the function
          plt.show()

source_raw:
  def count_plot(data):
      for col in data.columns:
          sns.countplot(data[col])  #TODO: fix the function
          plt.show()


source:
  def count_plot(data):
      for col in data.columns:
          # sns.countplot(data[col])  #TODO: fix the function
          sns.countplot(data, x=col)
          plt.show()

source_raw:
  def count_plot(data):
      for col in data.columns:
          # sns.countplot(data[col])  #TODO: fix the function
          sns.countplot(data, x=col)
          plt.show()


source:
  sample1 = np.random.normal(loc=10, scale=2, size=30)
  sample2 = np.random.normal(loc=12, scale=2, size=30)

source_raw:
  sample1 = np.random.normal(loc=10, scale=2, size=30)
  sample2 = np.random.normal(loc=12, scale=2, size=30)


source:
  alpha = 0.05

  print("Result of two-sample t-test")
  print("T-statistic:", t_statistic)
  print("P-value:", p_value)
  print("Degree of freedom:", len(sample1))

source_raw:
  alpha = 0.05

  print("Result of two-sample t-test")
  print("T-statistic:", t_statistic)
  print("P-value:", p_value)
  print("Degree of freedom:", len(sample1))


source:
  plt.figure(figsize=(10,6))
  plt.hist(sample1, alpha=0.5, label='Sample1', color='Blue')
  plt.hist(sample2, alpha=0.5, label='Sample2', color='Orange')
  plt.axvline(np.mean(sample1), color='blue', linestyle='dashed', linewidth=2)
  plt.axvline(np.mean(sample2), color='orange', linestyle='dashed', linewidth=2)
  plt.title('Distributions of sample1 and sample2')
  plt.xlabel('Values')
  plt.ylabel('Frequency')
  plt.legend()

  ## Highlight the critical region if null hypothesis is rejected
  if p_value < alpha:
      critical_region = np.linspace(min(sample1.min(), sample2.min()),
                                    max(sample1.max(), sample2.max()))
      plt.fill_between(critical_region, 0, 5, color='red', alpha=0.3, label='Critical Region')
      plt.text(11, 5, "T-statistic: %.2f" %t_statistic, ha='center', va='center', color='black', backgroundcolor='white')

  ## Draw conclusions
  if p_value < alpha:
      print("Conclusion: There is significant evidence to reject the null hypothesis.")
      if np.mean(sample1) > np.mean(sample2):
          print("Interpretation: The mean of sample 1 is significantly higher than that of sample 2.")
      else:
          print("Interpretation: The mean of sample 2 is significantly higher than that of sample 1.")
  else:
      print("Conclusion: There is no significant evidence to reject the null hypothesis.")
      print("Interpretation: There is no significant difference in means between the two samples.")

  plt.show()

source_raw:
  plt.figure(figsize=(10,6))
  plt.hist(sample1, alpha=0.5, label='Sample1', color='Blue')
  plt.hist(sample2, alpha=0.5, label='Sample2', color='Orange')
  plt.axvline(np.mean(sample1), color='blue', linestyle='dashed', linewidth=2)
  plt.axvline(np.mean(sample2), color='orange', linestyle='dashed', linewidth=2)
  plt.title('Distributions of sample1 and sample2')
  plt.xlabel('Values')
  plt.ylabel('Frequency')
  plt.legend()

  ## Highlight the critical region if null hypothesis is rejected
  if p_value < alpha:
      critical_region = np.linspace(min(sample1.min(), sample2.min()),
                                    max(sample1.max(), sample2.max()))
      plt.fill_between(critical_region, 0, 5, color='red', alpha=0.3, label='Critical Region')
      plt.text(11, 5, "T-statistic: %.2f" %t_statistic, ha='center', va='center', color='black', backgroundcolor='white')

  ## Draw conclusions
  if p_value < alpha:
      print("Conclusion: There is significant evidence to reject the null hypothesis.")
      if np.mean(sample1) > np.mean(sample2):
          print("Interpretation: The mean of sample 1 is significantly higher than that of sample 2.")
      else:
          print("Interpretation: The mean of sample 2 is significantly higher than that of sample 1.")
  else:
      print("Conclusion: There is no significant evidence to reject the null hypothesis.")
      print("Interpretation: There is no significant difference in means between the two samples.")

  plt.show()


source:
  import pandas as pd
  import numpy as np
  import matplotlib as plt
  import seaborn as sb
  import warnings
  from scipy import stats

source_raw:
  import pandas as pd
  import numpy as np
  import matplotlib as plt
  import seaborn as sb
  import warnings
  from scipy import stats


source:
  import pandas as pd
  import numpy as np
  import matplotlib as plt
  import seaborn as sb
  import warnings
  from scipy import stats
  warnings.filterwarnings('ignore')

source_raw:
  import pandas as pd
  import numpy as np
  import matplotlib as plt
  import seaborn as sb
  import warnings
  from scipy import stats
  warnings.filterwarnings('ignore')


source:
  import pandas as pd
  import numpy as np
  import matplotlib as plt
  import seaborn as sb
  import warnings
  from scipy import stats
  warnings.filterwarnings("ignore")

source_raw:
  import pandas as pd
  import numpy as np
  import matplotlib as plt
  import seaborn as sb
  import warnings
  from scipy import stats
  warnings.filterwarnings("ignore")


source:
  bins = [0, 75, 150, 240]
  df['horsepower_new'] = pd.cut(df['horsepower'], bins=bins, labels=['l', 'm', 'h'])
  c = df['horsepower_new']
  print(c)

source_raw:
  bins = [0, 75, 150, 240]
  df['horsepower_new'] = pd.cut(df['horsepower'], bins=bins, labels=['l', 'm', 'h'])
  c = df['horsepower_new']
  print(c)


source:
  bins = [0, 75, 150, 240]
  df['horsepower_new'] = pd.cut(df['horsepower'], bins=bins, labels=['l', 'm', 'h'])
  df['horsepower_new']

source_raw:
  bins = [0, 75, 150, 240]
  df['horsepower_new'] = pd.cut(df['horsepower'], bins=bins, labels=['l', 'm', 'h'])
  df['horsepower_new']


source:
  ybins = [69, 72, 74, 84]
  label = ['t1', 't2', 't3']
  df['modelyear_new'] = pd.cut(df['model_year'], bins=ybins, labels=label)
  df['modelyear_new']

source_raw:
  ybins = [69, 72, 74, 84]
  label = ['t1', 't2', 't3']
  df['modelyear_new'] = pd.cut(df['model_year'], bins=ybins, labels=label)
  df['modelyear_new']


source:
  print("Conclusion: There is sufficient evidence to reject the null hypothesis,
        indicating that there is a significant association between 'horsepower_new'
        and 'modelyear_new' categories.")

source_raw:
  print("Conclusion: There is sufficient evidence to reject the null hypothesis,
        indicating that there is a significant association between 'horsepower_new'
        and 'modelyear_new' categories.")


source:
  print("Conclusion: There is sufficient evidence to reject the null hypothesis,\
        indicating that there is a significant association between 'horsepower_new'
        and 'modelyear_new' categories.")

source_raw:
  print("Conclusion: There is sufficient evidence to reject the null hypothesis,\
        indicating that there is a significant association between 'horsepower_new'
        and 'modelyear_new' categories.")


source:
  print("Conclusion: There is sufficient evidence to reject the null hypothesis,", \
        "indicating that there is a significant association between 'horsepower_new'", \
        "and 'modelyear_new' categories.")

source_raw:
  print("Conclusion: There is sufficient evidence to reject the null hypothesis,", \
        "indicating that there is a significant association between 'horsepower_new'", \
        "and 'modelyear_new' categories.")


source:
  print("Conclusion: There is sufficient evidence to reject the null hypothesis,",
        "indicating that there is a significant association between 'horsepower_new'",
        "and 'modelyear_new' categories.")

source_raw:
  print("Conclusion: There is sufficient evidence to reject the null hypothesis,",
        "indicating that there is a significant association between 'horsepower_new'",
        "and 'modelyear_new' categories.")


source:
  import pandas as pd
  import scipy.stats as stats
  from statsmodels.stats.multicomp import pairwise_tukeyhsd

source_raw:
  import pandas as pd
  import scipy.stats as stats
  from statsmodels.stats.multicomp import pairwise_tukeyhsd


source:
  groups = [[23, 25, 29, 34, 30]
            [19, 20, 22, 24, 25]
            [15, 18, 20, 21, 17]
            [28, 24, 26, 30, 29]]
  labels = [("Group"+str(i))*len(groups[i]) for i in len(groups)]
  labels

source_raw:
  groups = [[23, 25, 29, 34, 30]
            [19, 20, 22, 24, 25]
            [15, 18, 20, 21, 17]
            [28, 24, 26, 30, 29]]
  labels = [("Group"+str(i))*len(groups[i]) for i in len(groups)]
  labels


source:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [("Group"+str(i))*len(groups[i]) for i in len(groups)]
  labels

source_raw:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [("Group"+str(i))*len(groups[i]) for i in len(groups)]
  labels


source:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [("Group"+str(i))*len(groups[i]) for i in range(len(groups))]
  labels

source_raw:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [("Group"+str(i))*len(groups[i]) for i in range(len(groups))]
  labels


source:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [["Group"+str(i))*len(groups[i]] for i in range(len(groups))]
  labels

source_raw:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [["Group"+str(i))*len(groups[i]] for i in range(len(groups))]
  labels


source:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [["Group"+str(i)]*len(groups[i]) for i in range(len(groups))]
  labels

source_raw:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [["Group"+str(i)]*len(groups[i]) for i in range(len(groups))]
  labels


source:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [["Group"+str(i)]*len(groups[i]) for i in range(len(groups))]

source_raw:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [["Group"+str(i)]*len(groups[i]) for i in range(len(groups))]


source:
  f_statistics, p_value = stats.f_oneway(*groups)
  print("One-way ANOVA\n\n",
        "F-statistics", f_statistics,
        "p-value", p-value)

source_raw:
  f_statistics, p_value = stats.f_oneway(*groups)
  print("One-way ANOVA\n\n",
        "F-statistics", f_statistics,
        "p-value", p-value)


source:
  f_statistics, p_value = stats.f_oneway(*groups)
  print("One-way ANOVA\n\n",
        "F-statistics", f_statistics,
        "p-value", p_value)

source_raw:
  f_statistics, p_value = stats.f_oneway(*groups)
  print("One-way ANOVA\n\n",
        "F-statistics", f_statistics,
        "p-value", p_value)


source:
  f_statistics, p_value = stats.f_oneway(*groups)
  print("\nOne-way ANOVA",
        "\nF-statistics:", f_statistics,
        "\np-value:", p_value)

source_raw:
  f_statistics, p_value = stats.f_oneway(*groups)
  print("\nOne-way ANOVA",
        "\nF-statistics:", f_statistics,
        "\np-value:", p_value)


source:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [["Group"+str(i+1)] * len(groups[i]) for i in range(len(groups))]

source_raw:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]
  labels = [["Group"+str(i+1)] * len(groups[i]) for i in range(len(groups))]


source:
  group1 = [23, 25, 29, 34, 30]
  group2 = [19, 20, 22, 24, 25]
  group3 = [15, 18, 20, 21, 17]
  group4 = [28, 24, 26, 30, 29]

source_raw:
  group1 = [23, 25, 29, 34, 30]
  group2 = [19, 20, 22, 24, 25]
  group3 = [15, 18, 20, 21, 17]
  group4 = [28, 24, 26, 30, 29]


source:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]

source_raw:
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]


source:
  f_statistics, p_value = stats.f_oneway(groups)
  print("\nOne-way ANOVA",
        "\nF-statistics:", f_statistics,
        "\np-value:", p_value)

source_raw:
  f_statistics, p_value = stats.f_oneway(groups)
  print("\nOne-way ANOVA",
        "\nF-statistics:", f_statistics,
        "\np-value:", p_value)


source:
  # 4 groups of datasets
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]

  # labels for each datapoint, like Group1, Group2, etc...
  labels = [["Group"+str(i+1)] * len(groups[i]) for i in range(len(groups))]

source_raw:
  # 4 groups of datasets
  groups = [[23, 25, 29, 34, 30],
            [19, 20, 22, 24, 25],
            [15, 18, 20, 21, 17],
            [28, 24, 26, 30, 29]]

  # labels for each datapoint, like Group1, Group2, etc...
  labels = [["Group"+str(i+1)] * len(groups[i]) for i in range(len(groups))]


source:
  # *variable - unfolds the list in python
  # func([a, b]) becomes func(a, b)
  f_statistics, p_value = stats.f_oneway(*groups)

  print("\nOne-way ANOVA",
        "\nF-statistics:", f_statistics,
        "\np-value:", p_value)

source_raw:
  # *variable - unfolds the list in python
  # func([a, b]) becomes func(a, b)
  f_statistics, p_value = stats.f_oneway(*groups)

  print("\nOne-way ANOVA",
        "\nF-statistics:", f_statistics,
        "\np-value:", p_value)


source:
  # concatenate all the groups into one
  # [[a, b], [c, d]] becomes [a, b, c, d]
  all_data = sum(groups, [])

  print("\nTukey-Kramer post-hoc test\n")
  print(pairwise_tukeyhsd(groups, labels))

source_raw:
  # concatenate all the groups into one
  # [[a, b], [c, d]] becomes [a, b, c, d]
  all_data = sum(groups, [])

  print("\nTukey-Kramer post-hoc test\n")
  print(pairwise_tukeyhsd(groups, labels))


source:
  # concatenate all the groups into one
  # [[a, b], [c, d]] becomes [a, b, c, d]
  all_data = sum(groups, [])

  print("\nTukey-Kramer post-hoc test\n")
  print(pairwise_tukeyhsd(all_data, labels))

source_raw:
  # concatenate all the groups into one
  # [[a, b], [c, d]] becomes [a, b, c, d]
  all_data = sum(groups, [])

  print("\nTukey-Kramer post-hoc test\n")
  print(pairwise_tukeyhsd(all_data, labels))


source:
  # concatenate all the groups into one
  # [[a, b], [c, d]] becomes [a, b, c, d]
  all_groups = sum(groups, [])
  all_labels = sum(labels, [])

  print("\nTukey-Kramer post-hoc test\n")
  print(pairwise_tukeyhsd(all_groups, all_labels))

source_raw:
  # concatenate all the groups into one
  # [[a, b], [c, d]] becomes [a, b, c, d]
  all_groups = sum(groups, [])
  all_labels = sum(labels, [])

  print("\nTukey-Kramer post-hoc test\n")
  print(pairwise_tukeyhsd(all_groups, all_labels))


source:
  # concatenate all the groups into one
  # [[a, b], [c, d]] becomes [a, b, c, d]
  all_groups_data = sum(groups, [])
  all_labels_data = sum(labels, [])

  print("\nTukey-Kramer post-hoc test\n")
  print(pairwise_tukeyhsd(all_groups_data, all_labels_data))

source_raw:
  # concatenate all the groups into one
  # [[a, b], [c, d]] becomes [a, b, c, d]
  all_groups_data = sum(groups, [])
  all_labels_data = sum(labels, [])

  print("\nTukey-Kramer post-hoc test\n")
  print(pairwise_tukeyhsd(all_groups_data, all_labels_data))


source:
  import numpy as np
  from sklearn.datasets import fetch_california_housing
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LinearRegression
  from sklearn.metrics import mean_suared_error, r2_score

source_raw:
  import numpy as np
  from sklearn.datasets import fetch_california_housing
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LinearRegression
  from sklearn.metrics import mean_suared_error, r2_score


source:
  import numpy as np
  from sklearn.datasets import fetch_california_housing
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LinearRegression
  from sklearn.metrics import mean_squared_error, r2_score

source_raw:
  import numpy as np
  from sklearn.datasets import fetch_california_housing
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LinearRegression
  from sklearn.metrics import mean_squared_error, r2_score


source:
  import numpy as np
  import pandas as pd
  from sklearn.datasets import fetch_california_housing
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LinearRegression
  from sklearn.metrics import mean_squared_error, r2_score

source_raw:
  import numpy as np
  import pandas as pd
  from sklearn.datasets import fetch_california_housing
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LinearRegression
  from sklearn.metrics import mean_squared_error, r2_score


source:
  housing = fetch_california_housing()
  df = pd.DataFrame(housing.data, columns=housing.feature_names)
  df

source_raw:
  housing = fetch_california_housing()
  df = pd.DataFrame(housing.data, columns=housing.feature_names)
  df


source:
  y = housing.target

  # Single linear regression
  x = df["AveRooms"]
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)jk
  model = LinearRegression()
  model.fit(x_train, y_train)
  y_pred = model.predict(x_test)

  print("Single Linear Regression")
  print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
  print("R-squared:", r2_score(y_test, y_pred))
  print("Intercept:", model.intercept_)
  print("Coefficient:", model.coef_)

source_raw:
  y = housing.target

  # Single linear regression
  x = df["AveRooms"]
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)jk
  model = LinearRegression()
  model.fit(x_train, y_train)
  y_pred = model.predict(x_test)

  print("Single Linear Regression")
  print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
  print("R-squared:", r2_score(y_test, y_pred))
  print("Intercept:", model.intercept_)
  print("Coefficient:", model.coef_)


source:
  y = housing.target

  # Single linear regression
  x = df["AveRooms"]
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
  model = LinearRegression()
  model.fit(x_train, y_train)
  y_pred = model.predict(x_test)

  print("Single Linear Regression")
  print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
  print("R-squared:", r2_score(y_test, y_pred))
  print("Intercept:", model.intercept_)
  print("Coefficient:", model.coef_)

source_raw:
  y = housing.target

  # Single linear regression
  x = df["AveRooms"]
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
  model = LinearRegression()
  model.fit(x_train, y_train)
  y_pred = model.predict(x_test)

  print("Single Linear Regression")
  print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
  print("R-squared:", r2_score(y_test, y_pred))
  print("Intercept:", model.intercept_)
  print("Coefficient:", model.coef_)


source:
  housing = fetch_california_housing()
  y = housing.target
  df = pd.DataFrame(housing.data, columns=housing.feature_names)
  df

source_raw:
  housing = fetch_california_housing()
  y = housing.target
  df = pd.DataFrame(housing.data, columns=housing.feature_names)
  df


source:
  # Single linear regression
  x = df["AveRooms"]
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
  model = LinearRegression()
  model.fit(x_train, y_train)
  y_pred = model.predict(x_test)

  print("Single Linear Regression")
  print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
  print("R-squared:", r2_score(y_test, y_pred))
  print("Intercept:", model.intercept_)
  print("Coefficient:", model.coef_)

source_raw:
  # Single linear regression
  x = df["AveRooms"]
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
  model = LinearRegression()
  model.fit(x_train, y_train)
  y_pred = model.predict(x_test)

  print("Single Linear Regression")
  print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
  print("R-squared:", r2_score(y_test, y_pred))
  print("Intercept:", model.intercept_)
  print("Coefficient:", model.coef_)


source:
  # Single linear regression
  x = df[["AveRooms"]]
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
  model = LinearRegression()
  model.fit(x_train, y_train)
  y_pred = model.predict(x_test)

  print("Single Linear Regression")
  print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
  print("R-squared:", r2_score(y_test, y_pred))
  print("Intercept:", model.intercept_)
  print("Coefficient:", model.coef_)

source_raw:
  # Single linear regression
  x = df[["AveRooms"]]
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
  model = LinearRegression()
  model.fit(x_train, y_train)
  y_pred = model.predict(x_test)

  print("Single Linear Regression")
  print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
  print("R-squared:", r2_score(y_test, y_pred))
  print("Intercept:", model.intercept_)
  print("Coefficient:", model.coef_)


source:
  def evaluate(x, y)
      x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
      model = LinearRegression()
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      
      print("Single Linear Regression")
      print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
      print("R-squared:", r2_score(y_test, y_pred))
      print("Intercept:", model.intercept_)
      print("Coefficient:", model.coef_)

source_raw:
  def evaluate(x, y)
      x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
      model = LinearRegression()
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      
      print("Single Linear Regression")
      print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
      print("R-squared:", r2_score(y_test, y_pred))
      print("Intercept:", model.intercept_)
      print("Coefficient:", model.coef_)


source:
  def evaluate(x, y):
      x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
      model = LinearRegression()
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      
      print("Single Linear Regression")
      print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
      print("R-squared:", r2_score(y_test, y_pred))
      print("Intercept:", model.intercept_)
      print("Coefficient:", model.coef_)

source_raw:
  def evaluate(x, y):
      x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
      model = LinearRegression()
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      
      print("Single Linear Regression")
      print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
      print("R-squared:", r2_score(y_test, y_pred))
      print("Intercept:", model.intercept_)
      print("Coefficient:", model.coef_)


source:
  def evaluate(x, y):
      x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
      model = LinearRegression()
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      
      print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
      print("R-squared:", r2_score(y_test, y_pred))
      print("Intercept:", model.intercept_)
      print("Coefficient:", model.coef_)

source_raw:
  def evaluate(x, y):
      x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
      model = LinearRegression()
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      
      print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
      print("R-squared:", r2_score(y_test, y_pred))
      print("Intercept:", model.intercept_)
      print("Coefficient:", model.coef_)


source:
  import numpy as np
  import pandas as pd
  from sklearn.datasets import load_iris
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LinearRegression
  from sklearn.tree import DecisionTreeClassifier
  from sklearn.metrics import accuracy_score, precision_score, recall_score, classification_report

source_raw:
  import numpy as np
  import pandas as pd
  from sklearn.datasets import load_iris
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LinearRegression
  from sklearn.tree import DecisionTreeClassifier
  from sklearn.metrics import accuracy_score, precision_score, recall_score, classification_report


source:
  def evaluate(x, y):
      x_train, x_test, y_train, y_test = train_test_split(
          x, y, test_size=0.2, random_state=42)
      model = LinearRegression()
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)

      print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
      print("R-squared:", r2_score(y_test, y_pred))
      print("Intercept:", model.intercept_)
      print("Coefficient:", model.coef_)

source_raw:
  def evaluate(x, y):
      x_train, x_test, y_train, y_test = train_test_split(
          x, y, test_size=0.2, random_state=42)
      model = LinearRegression()
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)

      print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
      print("R-squared:", r2_score(y_test, y_pred))
      print("Intercept:", model.intercept_)
      print("Coefficient:", model.coef_)


source:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(iris.data, columns=iris.feature_names)
  df

source_raw:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(iris.data, columns=iris.feature_names)
  df


source:
  import numpy as np
  import pandas as pd
  from sklearn.datasets import load_iris
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LogisticRegression
  from sklearn.tree import DecisionTreeClassifier
  from sklearn.metrics import accuracy_score, precision_score, recall_score, classification_report

source_raw:
  import numpy as np
  import pandas as pd
  from sklearn.datasets import load_iris
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LogisticRegression
  from sklearn.tree import DecisionTreeClassifier
  from sklearn.metrics import accuracy_score, precision_score, recall_score, classification_report


source:
  x_train, x_test, y_train, y_test = train_test_split(
      iris.data, iris.target, test_size=0.2, random_state=42
  )
  model = LogisticRegression()
  evaluate(model)

source_raw:
  x_train, x_test, y_train, y_test = train_test_split(
      iris.data, iris.target, test_size=0.2, random_state=42
  )
  model = LogisticRegression()
  evaluate(model)


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %(
          "Accuracy", accuracy_score(y_test, y_pred),
          "Precision", precision_score(y_test, y_pred),
          "Recall", recall_score(y_test, y_pred),
      ))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %(
          "Accuracy", accuracy_score(y_test, y_pred),
          "Precision", precision_score(y_test, y_pred),
          "Recall", recall_score(y_test, y_pred),
      ))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  x_train, x_test, y_train, y_test = train_test_split(
      df, y, test_size=0.2, random_state=42
  )
  model = LogisticRegression()
  evaluate(model)

source_raw:
  x_train, x_test, y_train, y_test = train_test_split(
      df, y, test_size=0.2, random_state=42
  )
  model = LogisticRegression()
  evaluate(model)


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("Accuracy:", accuracy_score(y_test, y_pred),
          "Precision:", precision_score(y_test, y_pred),
          "Recall:", recall_score(y_test, y_pred)))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("Accuracy:", accuracy_score(y_test, y_pred),
          "Precision:", precision_score(y_test, y_pred),
          "Recall:", recall_score(y_test, y_pred)))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("Accuracy:", accuracy_score(y_test, y_pred),
          "Precision:", precision_score(y_test, y_pred),
          "Recall:", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("Accuracy:", accuracy_score(y_test, y_pred),
          "Precision:", precision_score(y_test, y_pred),
          "Recall:", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy:", accuracy_score(y_test, y_pred),
          "\nPrecision:", precision_score(y_test, y_pred),
          "\nRecall:", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy:", accuracy_score(y_test, y_pred),
          "\nPrecision:", precision_score(y_test, y_pred),
          "\nRecall:", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%(s)s: %(s)s\n" %(
          "Accuracy", accuracy_score(y_test, y_pred),
          "Precision", precision_score(y_test, y_pred),
          "Recall", recall_score(y_test, y_pred),
      ))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%(s)s: %(s)s\n" %(
          "Accuracy", accuracy_score(y_test, y_pred),
          "Precision", precision_score(y_test, y_pred),
          "Recall", recall_score(y_test, y_pred),
      ))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %("Accuracy", accuracy_score(y_test, y_pred))
          %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
      ))
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %("Accuracy", accuracy_score(y_test, y_pred))
          %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
      ))
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %("Accuracy", accuracy_score(y_test, y_pred))
          %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
           )
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %("Accuracy", accuracy_score(y_test, y_pred))
          %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
           )
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %("Accuracy", accuracy_score(y_test, y_pred))
          # %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
           )
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %("Accuracy", accuracy_score(y_test, y_pred))
          # %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
           )
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %"Accuracy", %accuracy_score(y_test, y_pred)
          # %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
           )
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %"Accuracy", %accuracy_score(y_test, y_pred)
          # %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
           )
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %"Accuracy" %accuracy_score(y_test, y_pred)
          # %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
           )
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s: %s\n" %"Accuracy" %accuracy_score(y_test, y_pred)
          # %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
           )
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s:\n" %"Accuracy" %accuracy_score(y_test, y_pred)
          # %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
           )
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("%-10s:\n" %"Accuracy" %accuracy_score(y_test, y_pred)
          # %("Precision", precision_score(y_test, y_pred))
          # "Recall", recall_score(y_test, y_pred),
           )
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred)))
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred)))
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n", classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred),
            "\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred),
            "\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  x_train, x_test, y_train, y_test = train_test_split(
      df, y, test_size=0.2, random_state=42
  )

  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred),
            "\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  x_train, x_test, y_train, y_test = train_test_split(
      df, y, test_size=0.2, random_state=42
  )

  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred),
            "\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  x_train, x_test, y_train, y_test = train_test_split(
      df, y, test_size=0.2, random_state=42
  )

  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred),
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  x_train, x_test, y_train, y_test = train_test_split(
      df, y, test_size=0.2, random_state=42
  )

  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred),
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  x_train, x_test, y_train, y_test = train_test_split(
      df, y, test_size=0.2, random_state=42
  )

  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  x_train, x_test, y_train, y_test = train_test_split(
      df, y, test_size=0.2, random_state=42
  )

  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy", accuracy_score(y_test, y_pred),
            "\nPrecision", precision_score(y_test, y_pred),
            "\nRecall", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  x_train, x_test, y_train, y_test = train_test_split(
      df, y, test_size=0.2, random_state=42
  )

  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy:", accuracy_score(y_test, y_pred),
            "\nPrecision:", precision_score(y_test, y_pred),
            "\nRecall:", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  x_train, x_test, y_train, y_test = train_test_split(
      df, y, test_size=0.2, random_state=42
  )

  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy:", accuracy_score(y_test, y_pred),
            "\nPrecision:", precision_score(y_test, y_pred),
            "\nRecall:", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(iris.data+iris.target, columns=iris.feature_names)
  df

source_raw:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(iris.data+iris.target, columns=iris.feature_names)
  df


source:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(iris.data+[iris.target], columns=iris.feature_names)
  df

source_raw:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(iris.data+[iris.target], columns=iris.feature_names)
  df


source:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(np.c_(iris.data, iris.target), columns=iris.feature_names)
  df

source_raw:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(np.c_(iris.data, iris.target), columns=iris.feature_names)
  df


source:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names)
  df

source_raw:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names)
  df


source:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names+["species"])
  df

source_raw:
  # Load iris dataset to create a binary classification problem
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names+["species"])
  df


source:
  x_train, x_test, y_train, y_test = train_test_split(
      x, y, test_size=0.2, random_state=42
  )

  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy:", accuracy_score(y_test, y_pred),
            "\nPrecision:", precision_score(y_test, y_pred),
            "\nRecall:", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))

source_raw:
  x_train, x_test, y_train, y_test = train_test_split(
      x, y, test_size=0.2, random_state=42
  )

  def evaluate(model):
      model.fit(x_train, y_train)
      y_pred = model.predict(x_test)
      print("\nAccuracy:", accuracy_score(y_test, y_pred),
            "\nPrecision:", precision_score(y_test, y_pred),
            "\nRecall:", recall_score(y_test, y_pred))
      print("\nClassification Report :-\n",
            classification_report(y_test, y_pred))


source:
  from sklearn.cluster import KMeans
  from sklearn import metrics
  from scipy.spatial.distance import cdist
  import numpy as np
  import matplotlib.pyplot as plt

source_raw:
  from sklearn.cluster import KMeans
  from sklearn import metrics
  from scipy.spatial.distance import cdist
  import numpy as np
  import matplotlib.pyplot as plt


source:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 2, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2)))

source_raw:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 2, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2)))


source:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 2, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2)))
  x

source_raw:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 2, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2)))
  x


source:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2)))
  x

source_raw:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2)))
  x


source:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = zip(x1, x2)
  x

source_raw:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = zip(x1, x2)
  x


source:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = list(zip(x1, x2))
  x

source_raw:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = list(zip(x1, x2))
  x


source:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2))).reshape(len(x1), 2))
  x

source_raw:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2))).reshape(len(x1), 2))
  x


source:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2))).reshape(len(x1), 2)
  x

source_raw:
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2))).reshape(len(x1), 2)
  x


source:
  # Visualize the data
  plt.plot()
  plt.xlim([0, 10])
  plt.ylim([0, 10])
  plt.title("Dataset")
  plt.scatter(x1, x2)

source_raw:
  # Visualize the data
  plt.plot()
  plt.xlim([0, 10])
  plt.ylim([0, 10])
  plt.title("Dataset")
  plt.scatter(x1, x2)


source:
  # Visualize the data
  plt.plot()
  plt.xlim([0, 10])
  plt.ylim([0, 10])
  plt.title("Dataset")
  plt.scatter(x1, x2)
  plt.show()

source_raw:
  # Visualize the data
  plt.plot()
  plt.xlim([0, 10])
  plt.ylim([0, 10])
  plt.title("Dataset")
  plt.scatter(x1, x2)
  plt.show()


source:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distortions[k] = sum(
          np.min(
              cdist(x, model.cluster_centers_),
              axis=1
          )
      ) / x.shape[0]
      
      inertias[k] = model.inertia_
      
  for item in distortions.items():
      print("%s: %s" %item)

source_raw:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distortions[k] = sum(
          np.min(
              cdist(x, model.cluster_centers_),
              axis=1
          )
      ) / x.shape[0]
      
      inertias[k] = model.inertia_
      
  for item in distortions.items():
      print("%s: %s" %item)


source:
  plt.plot(K, list(distortions.values()), 'bx-')
  plt.xlabel("Values of K")
  plt.ylabel("Distortion")
  plt.show()

source_raw:
  plt.plot(K, list(distortions.values()), 'bx-')
  plt.xlabel("Values of K")
  plt.ylabel("Distortion")
  plt.show()


source:
  # Creating the data
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2)))
  x

source_raw:
  # Creating the data
  x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8, 4, 4, 5, 4])
  x2 = np.array([5, 4, 5, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3, 9, 10, 9, 10])
  x = np.array(list(zip(x1, x2)))
  x


source:
  from sklearn.cluster import KMeans
  from scipy.spatial.distance import cdist
  import numpy as np
  import matplotlib.pyplot as plt

source_raw:
  from sklearn.cluster import KMeans
  from scipy.spatial.distance import cdist
  import numpy as np
  import matplotlib.pyplot as plt


source:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distortions[k] = sum(
          np.min(
              cdist(x, model.cluster_centers_),
              axis=1
          )
      ) / x.shape[0]
      
      inertias[k] = model.inertia_

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)

source_raw:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distortions[k] = sum(
          np.min(
              cdist(x, model.cluster_centers_),
              axis=1
          )
      ) / x.shape[0]
      
      inertias[k] = model.inertia_

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)


source:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_),
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)

source_raw:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_),
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)


source:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)

source_raw:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)


source:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      print(distances)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      print(clusters)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)
      

source_raw:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      print(distances)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      print(clusters)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)
      


source:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      print("###  For k =", k)
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      print("\nDistances :-\n", distances)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      print("\nclusters :-\n", clusters)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)
      

source_raw:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      print("###  For k =", k)
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      print("\nDistances :-\n", distances)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      print("\nclusters :-\n", clusters)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)
      


source:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      print("\n###  For k =", k)
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      print("\nDistances :-\n", distances)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      print("\nclusters :-\n", clusters)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

      print("distortion =", distortions[k])
      print("inertia =", inertias[k])

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)
      

source_raw:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      print("\n###  For k =", k)
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      print("\nDistances :-\n", distances)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      print("\nclusters :-\n", clusters)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

      print("distortion =", distortions[k])
      print("inertia =", inertias[k])

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)
      


source:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      print("\n###  For k =", k)
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      print("\nDistances :-\n", distances)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      print("\nclusters :-\n", clusters)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

      print("\nDistortion =", distortions[k])
      print("\nInertia =", inertias[k])

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)
      

source_raw:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      print("\n###  For k =", k)
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      print("\nDistances :-\n", distances)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      print("\nclusters :-\n", clusters)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      distortions[k] = sum(clusters) / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

      print("\nDistortion =", distortions[k])
      print("\nInertia =", inertias[k])

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)
      


source:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      print("\n###  For k =", k)
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      print("\nDistances :-\n", distances)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      print("\nclusters :-\n", clusters)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      total = sum(clusters) 
      print("\nTotal of distances =", total)
      distortions[k] = total / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

      print("\nDistortion =", distortions[k])
      print("\nInertia =", inertias[k])

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)
      

source_raw:
  distortions = {}
  inertias = {}
  K = range(1, 10)

  for k in K:
      print("\n###  For k =", k)
      # Building and fitting the model
      model = KMeans(n_clusters=k).fit(x)
      model.fit(x)
      
      distances = cdist(x, model.cluster_centers_)
      print("\nDistances :-\n", distances)
      # cdist(data_points, cluster_points)
      # cross distance, returns a table i.e. a list of lists (2D array) :-
      # [
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point1
      #   [distance_from_cluster1, distance_from_cluster2, ...],   <- for data_point2
      #   ...                                                      <- and so on...
      # ]

      # get the closest cluster for each data point and assign it to that data point
      clusters = np.min(distances, axis=1)
      print("\nclusters :-\n", clusters)
      # axis=1 gets the minimum on horizontal axis rather than vertical

      # sum up all the distances to get the net deviation in data
      total = sum(clusters) 
      print("\nTotal of distances =", total)
      distortions[k] = total / x.shape[0]
      # dividing by x.shape[0] i.e. no. of data points, to get the mean
      
      inertias[k] = model.inertia_

      print("\nDistortion =", distortions[k])
      print("\nInertia =", inertias[k])

  print("\nValues of distortions :-\n")
  for item in distortions.items():
      print("%s: %s" %item)
      


source:
  import pandas as pd
  import matplotlib.pyplot as plt
  from sklearn.preprocessing import MinMaxScaler
  from sklearn.cluster import KMeans

source_raw:
  import pandas as pd
  import matplotlib.pyplot as plt
  from sklearn.preprocessing import MinMaxScaler
  from sklearn.cluster import KMeans


source:
  categorical_features = ["Channel", "Region"]
  continous_features = ["Fresh", "milk", "Grocery", "Frozen", "Detergents_Paper", "Delicassen"]
  data[continous_features].describe()

source_raw:
  categorical_features = ["Channel", "Region"]
  continous_features = ["Fresh", "milk", "Grocery", "Frozen", "Detergents_Paper", "Delicassen"]
  data[continous_features].describe()


source:
  categorical_features = ["Channel", "Region"]
  continous_features = ["Fresh", "Milk", "Grocery", "Frozen", "Detergents_Paper", "Delicassen"]
  data[continous_features].describe()

source_raw:
  categorical_features = ["Channel", "Region"]
  continous_features = ["Fresh", "Milk", "Grocery", "Frozen", "Detergents_Paper", "Delicassen"]
  data[continous_features].describe()


source:
  for col in categorical_features:
      dummies = pd.get_dummies(data[col], prefix=col)
      data = pd.concat([data, dummies], axis=1)
      data.drop(col, axis=1, inplace=True)
      
  data.head()

source_raw:
  for col in categorical_features:
      dummies = pd.get_dummies(data[col], prefix=col)
      data = pd.concat([data, dummies], axis=1)
      data.drop(col, axis=1, inplace=True)
      
  data.head()


source:
  scaler = MinMaxScaler()
  scaler.fit(data)
  data = scaler.transform(data)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %d, inertia = %f" %(i, inertias[k]))

source_raw:
  scaler = MinMaxScaler()
  scaler.fit(data)
  data = scaler.transform(data)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %d, inertia = %f" %(i, inertias[k]))


source:
  scaler = MinMaxScaler()
  scaler.fit(data)
  data = scaler.transform(data)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %d, inertia = %f" %(i, inertias[k-1]))

source_raw:
  scaler = MinMaxScaler()
  scaler.fit(data)
  data = scaler.transform(data)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %d, inertia = %f" %(i, inertias[k-1]))


source:
  scaler = MinMaxScaler()
  scaler.fit(data)
  data = scaler.transform(data)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))

source_raw:
  scaler = MinMaxScaler()
  scaler.fit(data)
  data = scaler.transform(data)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))


source:
  categorical_features = ["Channel", "Region"]
  continous_features = ["Fresh", "Milk", "Grocery", "Frozen", "Detergents_Paper", "Delicassen"]
  df[continous_features].describe()

source_raw:
  categorical_features = ["Channel", "Region"]
  continous_features = ["Fresh", "Milk", "Grocery", "Frozen", "Detergents_Paper", "Delicassen"]
  df[continous_features].describe()


source:
  for col in categorical_features:
      dummies = pd.get_dummies(df[col], prefix=col)
      df = pd.concat([data, dummies], axis=1)
      df.drop(col, axis=1, inplace=True)
      
  df.head()

source_raw:
  for col in categorical_features:
      dummies = pd.get_dummies(df[col], prefix=col)
      df = pd.concat([data, dummies], axis=1)
      df.drop(col, axis=1, inplace=True)
      
  df.head()


source:
  for col in categorical_features:
      dummies = pd.get_dummies(df[col], prefix=col)
      df = pd.concat([df, dummies], axis=1)
      df.drop(col, axis=1, inplace=True)
      
  df.head()

source_raw:
  for col in categorical_features:
      dummies = pd.get_dummies(df[col], prefix=col)
      df = pd.concat([df, dummies], axis=1)
      df.drop(col, axis=1, inplace=True)
      
  df.head()


source:
  scaler = MinMaxScaler()
  scaler.fit(data)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))

source_raw:
  scaler = MinMaxScaler()
  scaler.fit(data)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))


source:
  scaler = MinMaxScaler()
  scaler.fit(df)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))

source_raw:
  scaler = MinMaxScaler()
  scaler.fit(df)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))


source:
  scaler = MinMaxScaler()
  scaler.fit(df)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      print(model.inertia_)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))

source_raw:
  scaler = MinMaxScaler()
  scaler.fit(df)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      print(model.inertia_)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))


source:
  scaler = MinMaxScaler()
  scaler.fit(df)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      print(inertias, model.inertia_)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))

source_raw:
  scaler = MinMaxScaler()
  scaler.fit(df)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      print(inertias, model.inertia_)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))


source:
  scaler = MinMaxScaler()
  scaler.fit(df)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      print(inertias, model.inertia_)
      inertias.append(model.inertia_)

  print(inertias)
  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))

source_raw:
  scaler = MinMaxScaler()
  scaler.fit(df)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      print(inertias, model.inertia_)
      inertias.append(model.inertia_)

  print(inertias)
  print("\nValues of Inertias :-\n")
  for i in K:
      print("At k = %2d, inertia = %f" %(i, inertias[k-1]))


source:
  scaler = MinMaxScaler()
  scaler.fit(df)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for k in K:
      print("At k = %2d, inertia = %f" %(k, inertias[k-1]))

source_raw:
  scaler = MinMaxScaler()
  scaler.fit(df)
  data = scaler.transform(df)

  inertias = []  # sum of squared distances
  K = range(1, 15)
  for k in K:
      model = KMeans(n_clusters=k)
      model = model.fit(data)
      inertias.append(model.inertia_)

  print("\nValues of Inertias :-\n")
  for k in K:
      print("At k = %2d, inertia = %f" %(k, inertias[k-1]))


source:
  # Visualize the data
  plt.plot(K, inertias, 'bx-')
  plt.xlabel("K")
  plt.ylabel("Inertias (Sum of Squared Distances)")
  plt.title("Elbow method for optimal K")
  plt.show()

source_raw:
  # Visualize the data
  plt.plot(K, inertias, 'bx-')
  plt.xlabel("K")
  plt.ylabel("Inertias (Sum of Squared Distances)")
  plt.title("Elbow method for optimal K")
  plt.show()


source:
  # Visualize the data
  plt.plot(K, inertias, 'bx-')
  plt.xlabel("Values of K")
  plt.ylabel("Inertias (Sum of Squared Distances)")
  plt.title("Elbow method for optimal K")
  plt.show()

source_raw:
  # Visualize the data
  plt.plot(K, inertias, 'bx-')
  plt.xlabel("Values of K")
  plt.ylabel("Inertias (Sum of Squared Distances)")
  plt.title("Elbow method for optimal K")
  plt.show()


source:
  for col in categorical_features:
      dummies = pd.get_dummies(df[col], prefix=col)
      df = pd.concat([df, dummies], axis=1)
      df.drop(col, axis=1, inplace=True)
      
  df.head( )

source_raw:
  for col in categorical_features:
      dummies = pd.get_dummies(df[col], prefix=col)
      df = pd.concat([df, dummies], axis=1)
      df.drop(col, axis=1, inplace=True)
      
  df.head( )


source:
  import pandas as pd
  import numpy as np
  import matplotlib.pyplot as plt
  from sklearn.datasets import load_iris
  from sklearn.preprocessing import StandardScaler
  from sklearn.decomposition import PCA

source_raw:
  import pandas as pd
  import numpy as np
  import matplotlib.pyplot as plt
  from sklearn.datasets import load_iris
  from sklearn.preprocessing import StandardScaler
  from sklearn.decomposition import PCA


source:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']
  x, y

source_raw:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']
  x, y


source:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']
  x

source_raw:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']
  x


source:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']
  y

source_raw:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']
  y


source:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']
  iris.target

source_raw:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']
  iris.target


source:
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']

source_raw:
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']


source:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']

  scaler = StandardScaler()
  x = scaler.fit_transform(x)
  pca = PCA()
  x = pca.fit_transform(x)
  x

source_raw:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']

  scaler = StandardScaler()
  x = scaler.fit_transform(x)
  pca = PCA()
  x = pca.fit_transform(x)
  x


source:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']

  scaler = StandardScaler()
  x = scaler.fit_transform(x)
  pca = PCA()
  x = pca.fit_transform(x)
  evr = pca.explained_variance_ratio_
  evr

source_raw:
  iris = load_iris()
  df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  x = df.drop("target", axis=1)
  y = df['target']

  scaler = StandardScaler()
  x = scaler.fit_transform(x)
  pca = PCA()
  x = pca.fit_transform(x)
  evr = pca.explained_variance_ratio_
  evr


source:
  plt.figure(figsize=(8,6))
  plt.plot(np.cumsum(evr), marker="o", linestyle="--")
  plt.title("Explained Variance Ratio")
  plt.xlabel("Number of Principal Components")
  plt.ylabel("Cumalative Explained Variance Ratio")
  plt.grid(True)
  plt.show

source_raw:
  plt.figure(figsize=(8,6))
  plt.plot(np.cumsum(evr), marker="o", linestyle="--")
  plt.title("Explained Variance Ratio")
  plt.xlabel("Number of Principal Components")
  plt.ylabel("Cumalative Explained Variance Ratio")
  plt.grid(True)
  plt.show


source:
  cvr = np.cumsum(evr)
  n_components = np.argmax(cvr >= 0.95) + 1
  print("Number of principal components to explain 95% of variance:", n_components)

source_raw:
  cvr = np.cumsum(evr)
  n_components = np.argmax(cvr >= 0.95) + 1
  print("Number of principal components to explain 95% of variance:", n_components)


source:
  pca = PCA(n_components=n_components)
  x = pca.fit_transform()

  plt.figure(figsize=(8,6))
  plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  plt.title("Data in Reduced-dimenstion Space")
  plt.xlabel("Principal Component 1")
  plt.ylabel("Principal Component 2")
  plt.colorbar(label="Target")
  plt.show()

source_raw:
  pca = PCA(n_components=n_components)
  x = pca.fit_transform()

  plt.figure(figsize=(8,6))
  plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  plt.title("Data in Reduced-dimenstion Space")
  plt.xlabel("Principal Component 1")
  plt.ylabel("Principal Component 2")
  plt.colorbar(label="Target")
  plt.show()


source:
  pca = PCA(n_components=n_components)
  x = pca.fit_transform(x)

  plt.figure(figsize=(8,6))
  plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  plt.title("Data in Reduced-dimenstion Space")
  plt.xlabel("Principal Component 1")
  plt.ylabel("Principal Component 2")
  plt.colorbar(label="Target")
  plt.show()

source_raw:
  pca = PCA(n_components=n_components)
  x = pca.fit_transform(x)

  plt.figure(figsize=(8,6))
  plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  plt.title("Data in Reduced-dimenstion Space")
  plt.xlabel("Principal Component 1")
  plt.ylabel("Principal Component 2")
  plt.colorbar(label="Target")
  plt.show()


source:
  iris = load_iris()
  # df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  # x = df.drop("target", axis=1)
  # y = df['target']

  scaler = StandardScaler()
  x = scaler.fit_transform(iris.data)
  pca = PCA()
  x = pca.fit_transform(x)
  evr = pca.explained_variance_ratio_
  evr

source_raw:
  iris = load_iris()
  # df = pd.DataFrame(np.c_[iris.data, iris.target], columns=iris.feature_names + ["target"])
  # x = df.drop("target", axis=1)
  # y = df['target']

  scaler = StandardScaler()
  x = scaler.fit_transform(iris.data)
  pca = PCA()
  x = pca.fit_transform(x)
  evr = pca.explained_variance_ratio_
  evr


source:
  plt.figure(figsize=(8,6))
  plt.plot(np.cumsum(evr), marker="o", linestyle="--")
  plt.title("Explained Variance Ratio")
  plt.xlabel("Number of Principal Components")
  plt.ylabel("Cumulative Explained Variance Ratio")
  plt.grid(True)
  plt.show

source_raw:
  plt.figure(figsize=(8,6))
  plt.plot(np.cumsum(evr), marker="o", linestyle="--")
  plt.title("Explained Variance Ratio")
  plt.xlabel("Number of Principal Components")
  plt.ylabel("Cumulative Explained Variance Ratio")
  plt.grid(True)
  plt.show


source:
  iris = load_iris()
  x, y = iris.data, iris.target

  scaler = StandardScaler()
  x = scaler.fit_transform(x)

  pca = PCA()
  x = pca.fit_transform(x)
  evr = pca.explained_variance_ratio_
  evr

source_raw:
  iris = load_iris()
  x, y = iris.data, iris.target

  scaler = StandardScaler()
  x = scaler.fit_transform(x)

  pca = PCA()
  x = pca.fit_transform(x)
  evr = pca.explained_variance_ratio_
  evr


source:
  iris = load_iris()
  x, y = iris.data, iris.target

  scaler = StandardScaler()
  x = scaler.fit_transform(x)

  pca = PCA()
  x = pca.fit_transform(x)
  #evr = pca.explained_variance_ratio_
  #evr

source_raw:
  iris = load_iris()
  x, y = iris.data, iris.target

  scaler = StandardScaler()
  x = scaler.fit_transform(x)

  pca = PCA()
  x = pca.fit_transform(x)
  #evr = pca.explained_variance_ratio_
  #evr


source:
  iris = load_iris()
  x, y = iris.data, iris.target

  scaler = StandardScaler()
  x = scaler.fit_transform(x)

  pca = PCA()
  x = pca.fit_transform(x)
  x
  #evr = pca.explained_variance_ratio_
  #evr

source_raw:
  iris = load_iris()
  x, y = iris.data, iris.target

  scaler = StandardScaler()
  x = scaler.fit_transform(x)

  pca = PCA()
  x = pca.fit_transform(x)
  x
  #evr = pca.explained_variance_ratio_
  #evr


source:
  plt.figure(figsize=(8,6))
  plt.plot(cvr, marker="o", linestyle="--")
  plt.title("Explained Variance Ratio")
  plt.xlabel("Number of Principal Components")
  plt.ylabel("Cumulative Explained Variance Ratio")
  plt.grid(True)
  plt.show

source_raw:
  plt.figure(figsize=(8,6))
  plt.plot(cvr, marker="o", linestyle="--")
  plt.title("Explained Variance Ratio")
  plt.xlabel("Number of Principal Components")
  plt.ylabel("Cumulative Explained Variance Ratio")
  plt.grid(True)
  plt.show


source:
  iris = load_iris()
  x, y = iris.data, iris.target

  scaler = StandardScaler()
  x = scaler.fit_transform(x)

  pca = PCA()
  pca.fit_transform(x)
  evr = pca.explained_variance_ratio_
  evr

source_raw:
  iris = load_iris()
  x, y = iris.data, iris.target

  scaler = StandardScaler()
  x = scaler.fit_transform(x)

  pca = PCA()
  pca.fit_transform(x)
  evr = pca.explained_variance_ratio_
  evr


source:
  pca = PCA(n_components=n_components)
  x = pca.fit_transform(x)
  print(x)

  plt.figure(figsize=(8,6))
  plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  plt.title("Data in Reduced-dimenstion Space")
  plt.xlabel("Principal Component 1")
  plt.ylabel("Principal Component 2")
  plt.colorbar(label="Target")
  plt.show()

source_raw:
  pca = PCA(n_components=n_components)
  x = pca.fit_transform(x)
  print(x)

  plt.figure(figsize=(8,6))
  plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  plt.title("Data in Reduced-dimenstion Space")
  plt.xlabel("Principal Component 1")
  plt.ylabel("Principal Component 2")
  plt.colorbar(label="Target")
  plt.show()


source:
  pca = PCA(n_components=3)
  x = pca.fit_transform(x)
  print(x)

  plt.figure(figsize=(8,6))
  plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  plt.title("Data in Reduced-dimenstion Space")
  plt.xlabel("Principal Component 1")
  plt.ylabel("Principal Component 2")
  plt.colorbar(label="Target")
  plt.show()

source_raw:
  pca = PCA(n_components=3)
  x = pca.fit_transform(x)
  print(x)

  plt.figure(figsize=(8,6))
  plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  plt.title("Data in Reduced-dimenstion Space")
  plt.xlabel("Principal Component 1")
  plt.ylabel("Principal Component 2")
  plt.colorbar(label="Target")
  plt.show()


source:
  pca = PCA(n_components=3)
  x = pca.fit_transform(x)
  print(x)

  # plt.figure(figsize=(8,6))
  # plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  # plt.title("Data in Reduced-dimenstion Space")
  # plt.xlabel("Principal Component 1")
  # plt.ylabel("Principal Component 2")
  # plt.colorbar(label="Target")
  # plt.show()

source_raw:
  pca = PCA(n_components=3)
  x = pca.fit_transform(x)
  print(x)

  # plt.figure(figsize=(8,6))
  # plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  # plt.title("Data in Reduced-dimenstion Space")
  # plt.xlabel("Principal Component 1")
  # plt.ylabel("Principal Component 2")
  # plt.colorbar(label="Target")
  # plt.show()


source:
  n_components = np.argmax(cvr >= 0.95) + 1
  print("Number of principal components to explain 95% of variance:", n_components)

source_raw:
  n_components = np.argmax(cvr >= 0.95) + 1
  print("Number of principal components to explain 95% of variance:", n_components)


source:
  pca = PCA(n_components=3)
  print(pca.fit_transform(x))

  # plt.figure(figsize=(8,6))
  # plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  # plt.title("Data in Reduced-dimenstion Space")
  # plt.xlabel("Principal Component 1")
  # plt.ylabel("Principal Component 2")
  # plt.colorbar(label="Target")
  # plt.show()

source_raw:
  pca = PCA(n_components=3)
  print(pca.fit_transform(x))

  # plt.figure(figsize=(8,6))
  # plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  # plt.title("Data in Reduced-dimenstion Space")
  # plt.xlabel("Principal Component 1")
  # plt.ylabel("Principal Component 2")
  # plt.colorbar(label="Target")
  # plt.show()


source:
  pca = PCA()
  print(pca.fit_transform(x))

  # plt.figure(figsize=(8,6))
  # plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  # plt.title("Data in Reduced-dimenstion Space")
  # plt.xlabel("Principal Component 1")
  # plt.ylabel("Principal Component 2")
  # plt.colorbar(label="Target")
  # plt.show()

source_raw:
  pca = PCA()
  print(pca.fit_transform(x))

  # plt.figure(figsize=(8,6))
  # plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  # plt.title("Data in Reduced-dimenstion Space")
  # plt.xlabel("Principal Component 1")
  # plt.ylabel("Principal Component 2")
  # plt.colorbar(label="Target")
  # plt.show()


source:
  pca = PCA(n_components=n_components)
  print(pca.fit_transform(x)

  # plt.figure(figsize=(8,6))
  # plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  # plt.title("Data in Reduced-dimenstion Space")
  # plt.xlabel("Principal Component 1")
  # plt.ylabel("Principal Component 2")
  # plt.colorbar(label="Target")
  # plt.show()

source_raw:
  pca = PCA(n_components=n_components)
  print(pca.fit_transform(x)

  # plt.figure(figsize=(8,6))
  # plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  # plt.title("Data in Reduced-dimenstion Space")
  # plt.xlabel("Principal Component 1")
  # plt.ylabel("Principal Component 2")
  # plt.colorbar(label="Target")
  # plt.show()


source:
  pca = PCA(n_components=n_components)
  print(pca.fit_transform(x))

  # plt.figure(figsize=(8,6))
  # plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  # plt.title("Data in Reduced-dimenstion Space")
  # plt.xlabel("Principal Component 1")
  # plt.ylabel("Principal Component 2")
  # plt.colorbar(label="Target")
  # plt.show()

source_raw:
  pca = PCA(n_components=n_components)
  print(pca.fit_transform(x))

  # plt.figure(figsize=(8,6))
  # plt.scatter(x[:, 0], x[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  # plt.title("Data in Reduced-dimenstion Space")
  # plt.xlabel("Principal Component 1")
  # plt.ylabel("Principal Component 2")
  # plt.colorbar(label="Target")
  # plt.show()


source:
  pca = PCA(n_components=n_components)
  pca_data = pca.fit_transform(x)

  plt.figure(figsize=(8,6))
  plt.scatter(pca_data[:, 0], pca_data[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  plt.title("Data in Reduced-dimenstion Space")
  plt.xlabel("Principal Component 1")
  plt.ylabel("Principal Component 2")
  plt.colorbar(label="Target")
  plt.show()

source_raw:
  pca = PCA(n_components=n_components)
  pca_data = pca.fit_transform(x)

  plt.figure(figsize=(8,6))
  plt.scatter(pca_data[:, 0], pca_data[:, 1], c=y, cmap="viridis", s=50, alpha=0.5)
  plt.title("Data in Reduced-dimenstion Space")
  plt.xlabel("Principal Component 1")
  plt.ylabel("Principal Component 2")
  plt.colorbar(label="Target")
  plt.show()


source:
  n_components = np.argmax(cvr >= 0.95) + 1
  print("\nNumber of principal components to explain 95% of variance:", n_components)

source_raw:
  n_components = np.argmax(cvr >= 0.95) + 1
  print("\nNumber of principal components to explain 95% of variance:", n_components)


source:
  categorical_features = ["Channel", "Region"]
  continous_features = ["Fresh", "Milk", "Grocery", "Frozen", "Detergents_Paper", "Delicassen"]

  df[continous_features].describe()

source_raw:
  categorical_features = ["Channel", "Region"]
  continous_features = ["Fresh", "Milk", "Grocery", "Frozen", "Detergents_Paper", "Delicassen"]

  df[continous_features].describe()


