Regression Analysis

Iris Classification

Sepehr Torabparhiz


In [ ]:
import numpy as np
from sklearn import neighbors, datasets
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

In [ ]:
iris_data, iris_labels = datasets.load_iris(return_X_y=True)


ax.scatter(iris_data[:, 0], iris_data[:, 1], iris_data[:, 2],
          c='blue')
ax.set_xlabel = 'Sepal Length'
ax.set_ylabel = 'Sepal Width'
ax.set_zlabel = 'Petal Length'
plt.show()

Generating Test & Training Data

Randomly choose 15 of the 150 measurments as tests


In [ ]:
test_indices = np.random.randint(150, size=15)
training_indices = list(set(list(range(0, 150))) - set(test_indices))

test_data = iris_data[test_indices, :]
test_labels = iris_labels[test_indices]


training_data = iris_data[training_indices, :]
training_labels = iris_labels[training_indices]

Classifying with norm 1


In [ ]:
knn_norm1_1nn = neighbors.KNeighborsClassifier(n_neighbors=1, radius=1,
                                algorithm='auto', p=1, n_jobs=1,
                                metric='minkowski')

knn_norm1_1nn.fit(training_data, training_labels)

score_norm1_1nn = knn_norm1_1nn.score(test_data, test_labels)
score_norm1_1nn

In [ ]:
knn_norm1_3nn = neighbors.KNeighborsClassifier(n_neighbors=3, radius=1,
                                algorithm='auto', p=1, n_jobs=1,
                                metric='minkowski')

knn_norm1_3nn.fit(training_data, training_labels)

score_norm1_3nn = knn_norm1_3nn.score(test_data, test_labels)
score_norm1_3nn

In [ ]:
knn_norm1_5nn = neighbors.KNeighborsClassifier(n_neighbors=5, radius=0.5,
                                algorithm='auto', p=1, n_jobs=1,
                                metric='minkowski')

knn_norm1_5nn.fit(training_data, training_labels)

score_norm1_5nn = knn_norm1_5nn.score(test_data, test_labels)
score_norm1_5nn

In [ ]:
knn_norm1_10nn = neighbors.KNeighborsClassifier(n_neighbors=10, radius=1,
                                algorithm='auto', p=1, n_jobs=1,
                                metric='minkowski')

knn_norm1_10nn.fit(training_data, training_labels)

score_norm1_10nn = knn_norm1_10nn.score(test_data, test_labels)
score_norm1_10nn

Classifying with norm 2


In [ ]:
knn_norm2_1nn = neighbors.KNeighborsClassifier(n_neighbors=1, radius=1,
                                algorithm='auto', p=2, n_jobs=1,
                                metric='minkowski')

knn_norm2_1nn.fit(training_data, training_labels)

score_norm2_1nn = knn_norm2_1nn.score(test_data, test_labels)
score_norm2_1nn

In [ ]:
knn_norm2_1nn = neighbors.KNeighborsClassifier(n_neighbors=1, radius=1,
                                algorithm='auto', p=2, n_jobs=1,
                                metric='minkowski')

knn_norm2_1nn.fit(training_data, training_labels)

score_norm2_1nn = knn_norm2_1nn.score(test_data, test_labels)
score_norm2_1nn

In [ ]:
knn_norm2_3nn = neighbors.KNeighborsClassifier(n_neighbors=3, radius=1,
                                algorithm='auto', p=2, n_jobs=1,
                                metric='minkowski')

knn_norm2_3nn.fit(training_data, training_labels)

score_norm2_3nn = knn_norm2_3nn.score(test_data, test_labels)
score_norm2_3nn

In [ ]:
knn_norm2_5nn = neighbors.KNeighborsClassifier(n_neighbors=5, radius=1,
                                algorithm='auto', p=2, n_jobs=1,
                                metric='minkowski')

knn_norm2_5nn.fit(training_data, training_labels)

score_norm2_5nn = knn_norm2_5nn.score(test_data, test_labels)
score_norm2_5nn

In [ ]:
knn_norm2_10nn = neighbors.KNeighborsClassifier(n_neighbors=10, radius=1,
                                algorithm='auto', p=2, n_jobs=1,
                                metric='minkowski')

knn_norm2_10nn.fit(training_data, training_labels)

score_norm2_10nn = knn_norm2_10nn.score(test_data, test_labels)
score_norm2_10nn

Classifying with norm 3


In [ ]:
knn_norm3_1nn = neighbors.KNeighborsClassifier(n_neighbors=1, radius=1,
                                algorithm='auto', p=3, n_jobs=1,
                                metric='minkowski')

knn_norm3_1nn.fit(training_data, training_labels)

score_norm3_1nn = knn_norm3_1nn.score(test_data, test_labels)
score_norm3_1nn

In [ ]:
knn_norm3_3nn = neighbors.KNeighborsClassifier(n_neighbors=3, radius=1,
                                algorithm='auto', p=3, n_jobs=1,
                                metric='minkowski')

knn_norm3_3nn.fit(training_data, training_labels)

score_norm3_3nn = knn_norm3_3nn.score(test_data, test_labels)
score_norm3_3nn

In [ ]:
knn_norm3_5nn = neighbors.KNeighborsClassifier(n_neighbors=5, radius=1,
                                algorithm='auto', p=3, n_jobs=1,
                                metric='minkowski')

knn_norm3_5nn.fit(training_data, training_labels)

score_norm3_5nn = knn_norm3_5nn.score(test_data, test_labels)
score_norm3_5nn

In [ ]:
knn_norm3_10nn = neighbors.KNeighborsClassifier(n_neighbors=10, radius=1,
                                algorithm='auto', p=3, n_jobs=1,
                                metric='minkowski')

knn_norm3_10nn.fit(training_data, training_labels)

score_norm3_10nn = knn_norm3_10nn.score(test_data, test_labels)
score_norm3_10nn

In [ ]:


In [ ]: