Question
(1). Please report the accuracy of the prediction report (2) choose the class with the highest confidence score (3) Please report the errors (scatter plot)
(1). Please report the accuracy of the prediction report
(2) choose the class with the highest confidence score
(3) Please report the errors (scatter plot)
(4) do not use scikit-learn library
Given the following code in python:
import numpy as np import pandas as pd import matplotlib.pyplot as plt
class SGDClassifier: def __init__(self, lr=0.01, n_iters=1000): self.lr = lr self.n_iters = n_iters self.weights = None self.bias = None
def fit(self, X, y): n_samples, n_features = X.shape
self.weights = np.zeros(n_features) self.bias = 0
for _ in range(self.n_iters): for idx, x_i in enumerate(X): y_pred = self._sigmoid(np.dot(x_i, self.weights) + self.bias) gradient = (y[idx] - y_pred) * x_i self.weights += self.lr * gradient self.bias += self.lr * (y[idx] - y_pred)
def predict(self, X): y_pred = np.dot(X, self.weights) + self.bias y_pred = np.round(self._sigmoid(y_pred)) return y_pred.astype(int)
def _sigmoid(self, x): return 1 / (1 + np.exp(-x))
class OneVsRestClassifier: def __init__(self, classifier=SGDClassifier()): self.classifier = classifier
def fit(self, X, y): self.binary_classifiers = [] for label in np.unique(y): binary_y = np.where(y == label, 1, 0) binary_classifier = self.classifier binary_classifier.fit(X, binary_y) self.binary_classifiers.append(binary_classifier)
def predict(self, X): y_pred = [] for x_i in X: predictions = [] for binary_classifier in self.binary_classifiers: predictions.append(binary_classifier.predict([x_i])[0]) y_pred.append(np.argmax(predictions)) return np.array(y_pred) # Each plant in the dataset has 4 attributes: # sepal length, sepal width, petal length, and petal width. # We will use our logistic regression model to predict flowers species using # just these attributes.
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" df = pd.read_csv(url, header=None, names=[ "Sepal length (cm)", "Sepal width (cm)", "Petal length (cm)", "Petal width (cm)", "Species"]) df.head()
# Now well encode the species to an integer value, shuffle the data, # and split it into training and test data: df['Species'] = df['Species'].astype('category').cat.codes data = np.array(df) np.random.shuffle(data) num_train = int(.8 * len(data)) # 80/20 train/test split x_train, y_train = data[:num_train, :-1], data[:num_train, -1] x_test, y_test = data[num_train:, :-1], data[num_train:, -1]
# Lets look at the distribution of the data: plt.scatter(x_train[:,2], x_train[:, 3], c=y_train, alpha=0.5) plt.xlabel("Petal Length (cm)"); plt.ylabel("Petal Width (cm)");
# ***************************************************************
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started