Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
Homework 2 Question 2 KNN Classification in sklearn
# The following line will import KNeighborsClassifier "Class"
# KNeighborsClassifier is name of a "sklearn class" to perform "KNN Classification"
from sklearn.neighbors import KNeighborsClassifier
A) Read the iris dataset from the following URL:
https://raw.githubusercontent.com/mpourhoma/CS4661/master/iris.csv
# Importing the required packages and libraries
import pandas as pd
# creating and empty DataFrame:
df = pd.DataFrame()
# reading a CSV file directly from Web, and store it in a pandas DataFrame:
# "read_csv" is a pandas function to read csv files from web or local device:
df = pd.read_csv('https://raw.githubusercontent.com/mpourhoma/CS4661/master/iris.csv')
# displaying the DataFrame:
df
# Creating the Feature Matrix for iris dataset:
# create a python list of feature names that would like to pick from the dataset:
feature_cols = ['sepal_length','sepal_width','petal_length','petal_width']
# use the above list to select the features from the original DataFrame
X = df[feature_cols]
# print the first 5 rows
y=df['species']