-1

I run a code taken from this website for calculating Silhouette Coefficient, but the code is running with error

Here is the code

from sklearn import datasets
from sklearn.metrics import *
iris = datasets.load_iris()
col = iris.feature_names
name = iris.target_names
X = pd.DataFrame(iris.data, columns = col)
y = iris.target
s = silhouette_score(X.values, y, metric='euclidean',sample_size=50)

The error is

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-2e1432d3b9ce> in <module>
      4 col = iris.feature_names
      5 name = iris.target_names
----> 6 X = pd.DataFrame(iris.data, columns = col)
      7 y = iris.target
      8 s = silhouette_score(X.values, y, metric='euclidean',sample_size=50)

NameError: name 'pd' is not defined

Any help?

AKX
  • 152,115
  • 15
  • 115
  • 172

2 Answers2

0

You need to import pandas first

import pandas as pd

If you don't have it installed you'll need to install pandas first. Run this in the command line

pip install pandas
Aayush Agrawal
  • 1,354
  • 1
  • 12
  • 24
-1

The pandas library is generally imported as pd.

Install it (if you don't have it already), then add

import pandas as pd

and you're off to the races.

AKX
  • 152,115
  • 15
  • 115
  • 172