Question: Please solve the following in python. Preparation Steps In 1]: N# Import all necessary python packages import numpy as np import os import pandas as
Please solve the following in python.
![Please solve the following in python. Preparation Steps In 1]: N# Import](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f2f53da267d_52566f2f53d1a065.jpg)

Preparation Steps In 1]: N# Import all necessary python packages import numpy as np import os import pandas as pd import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from sklearn.linear_model import LogisticRegression In [2]: "# ### Reading in the Iris data $ = os.path.join('https://archive.ics.uci.edu', 'ml', 'machine learning-databases', 'iris', 'iris.data) S = s.replace("\\","/"); print('URL:', s) df = pd. read_csv(s, header=None, encoding='utf-8') URL: https://archive.ics.uci.edu/ml/machine learning-databases/iris/iris.data In [3]: N# select setosa and versicolor y = df.iloc[0:100, 4].values y = np.where(y == 'Iris-setosa', -1, 1) # extract sepal Length and petal Length X = df.iloc[:100, [0, 2]].values # plot data plt.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa') plt.scatter (X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor) plt.xlabel('sepal length [cm]') plt.ylabel('petal length [cm]') plt.legend (loc='upper left') # plt. savefig('images/02_06.png', dpi=300) plt.show() X setosa versicolor petal length [cm] 45 50 55 60 65 sepal length [cm] Question 1. Exact Adaline Weights In the class we discussed gradient descent for computing the weights of Adaline. It is possible however to use an 'exact' solver: Suppose X is the data matrix, with shape nxd, where d is the number of attributes. Now let's augment X by adding one extra attribute/column on the left side of X with all entries being equal to 1. Let X be this new matrix (this step reflects that Xo = 1). Also, let w be the vector of weights (with shape (d + 1) x1, and y be the vector of labels (with shape (n + 1) x 1). It turns out that the optimal weight vector w is given by w= (XTX-?xy (i) Write a Python function exactAdaline (using numpy) that takes as input X and y and returns the optimal. weights w, following the above formula (ii) Apply the function on the iris dataset that you imported in an earlier cell. Report the weights calculated by the function. (iii) The total error for a vector w is given by this formula err = ||Xw - y112 The subscript of the norm means the 2-norm. Use numpy to do your calculation. (Please insert cells below for your answers. Clearly id the part of the question you answer) In [ ]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
