Supervised Learning
Supervised Learning
Program
import numpy as np
import pandas as pd
url="https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names=['sepal-length','sepal-width','petal-length','petal-width','Class']
dataset=pd.read_csv(url,names=names)
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 4].values
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)
# Use only the first two features for a 2D decision boundary plot
knn_2D = KNeighborsClassifier(n_neighbors=5)
knn_2D.fit(X_train_2D, y_train_2D)
h = .02
x_min, x_max = X_two_features[:, 0].min() - 1, X_two_features[:, 0].max() + 1
Z = knn_2D.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(figsize=(8, 6))
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.show()
output: