Search code examples
machine-learningscikit-learndata-scienceknnsklearn-pandas

Knn classifier graph


I am trying to build a knn graph. but when i run this code mentioned below it throws me an error "AttributeError: 'DataFrame' object has no attribute 'data' "

%matplotlib inline
import matplotlib
matplotlib.use('GTKAgg')
import numpy as np
import pandas as pd 
from pandas import Series, DataFrame
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import neighbors, datasets

# importing data
dis = pd.read_csv("disease.csv")

# take the first two features
X = dis.data[:, :2]  
y = dis.target

print(X)

The CSV file looks like this : This is the screenshot of the csv file


Solution

  • In your code you are using dis.data[:, :2].

    You are trying to access an attribute named data of your dataframe dis. But Pandas dataframe have no attribute called data.

    In your code, you are trying to access first two columns of the dataframe. You can do that by slicing the dataframe after taking the values of the dataframe.

    In codes you can do the following.

    dis.values[:, :2]