Skip to content Skip to sidebar Skip to footer

Using Pandas And Sklearn.neighbors

I'm trying to fit a KNN model on a dataframe, using Python 3.5/Pandas/Sklearn.neighbors. I've imported the data, split it into training and testing data and labels, but when I try

Solution 1:

You should be using the KNeighborsClassifier for this KNN. You are trying to predict the label Species for classification. The regressor in your code above is trying to train and predict continuously valued numerical variables, which is where your problem is being introduced.

from sklearn.neighbors import KNeighborsClassifier
seeds = pd.read_csv('seeds.tsv',sep='\t',names=['Area','Perimeter','Compactness','Kern_len','Kern_width','Assymetry','Kern_groovlen','Species'])
data = seeds.iloc[:,[0,1,2,3,4,5,6]]
labels = seeds.iloc[:,[7]]
x_train, x_test, y_train, y_test = cross_validation.train_test_split(data,labels, test_size=0.4, random_state=1 )
knn = KNeighborsClassifier(n_neighbors=30)

http://scikit-learn.org/stable/auto_examples/neighbors/plot_classification.html

Here is what the regressor would plot compared to the classifier (which you want to use).

Regressor

Classifier

Post a Comment for "Using Pandas And Sklearn.neighbors"