You are on page 1of 3

Question

Finally, you will implement the full Pegasos algorithm. You will be given the
same feature matrix and labels array as you were given in Full Perceptron
Algorithm. You will also be given T , the maximum number of times that you
should iterate through the feature matrix before terminating the algorithm.
Initialize θ and θ0 to zero. For each update, set η=1t√ where t is a counter for
the number of updates performed so far (between 1 and nT inclusive). This
function should return a tuple in which the first element is the final value of θ
and the second element is the value of θ0 . Note: Please call
get_order(feature_matrix.shape[0]), and use the ordering to iterate the feature
matrix in each iteration. The ordering is specified due to grading purpose. In
practice, people typically just randomly shuffle indices to do stochastic
optimization. Available Functions: You have access to the NumPy python
library as np and pegasos_single_step_update which you have already
implemented.

Answer

In[7] def pegasos(feature_matrix, labels, T, L):


   """
   .
   let learning rate = 1/sqrt(t),
   where t is a counter for the number of updates performed so far      
(between 1   and nT inclusive).
Args:
       feature_matrix - A numpy matrix describing the given data. Each row
           represents a single data point.
       labels - A numpy array where the kth element of the array is the
           correct classification of the kth row of the feature matrix.
       T -  the maximum number of times that you should iterate through the
feature matrix before terminating the algorithm.
       L - The lamba valueto update the pegasos
   Returns: Is defined as a  tuple in which the first element is the final value of
θ and the second element is the value of θ0
   """
   (nsamples, nfeatures) = feature_matrix.shape
   theta = np.zeros(nfeatures)
   theta_0 = 0
   count = 0
   for t in range(T):
       for i in get_order(nsamples):
           count += 1
           eta = 1.0 / np.sqrt(count)
           (theta, theta_0) = pegasos_single_step_update(
               feature_matrix[i], labels[i], L, eta, theta, theta_0)
   return (theta, theta_0)
In[7] (np.array([1-1/np.sqrt(2), 1-1/np.sqrt(2)]), 1)
Out[7] (array([0.29289322, 0.29289322]), 1)
In[8] feature_matrix = np.array([[1, 1], [1, 1]])
   labels = np.array([1, 1])
   T = 1
   L = 1
   exp_res = (np.array([1-1/np.sqrt(2), 1-1/np.sqrt(2)]), 1)
  
   pegasos(feature_matrix, labels, T, L)
Out[8] (array([0.29289322, 0.29289322]), 1.0)
Moses Obasola is a dedicated and experienced mathematics teacher at Lincoln High School.
With over 10 years of teaching experience, I have a passion for helping students understand
complex mathematical concepts and develop critical thinking skills. I hold a degree in Chemical
Engineering and am committed to staying up to date with the latest teaching methods and
technology to enhance my students' learning experience. I can’t wait to solve this problem for
you.

The pendulum on a grandfather clock moves in a SINUSOIDAL fashion. The length of the pendulum
is 31 inches at its lowest point. The pendulum is 1.5 feet from the floor. The pendulum begins to
move at t1 so that the interior angle between t2 and t3 is 90 degrees. It takes .875 seconds for the
pendulum to swing from t2 to t3. 1. Write the sinusoidal equation that models the movement of the
pendulum as a COSINE FUNCTION 2. how high will the pendulum be in one hour 3. what is the
minimum and maximum height the pendulum will reach?
1. To model the movement of the pendulum as a cosine function, we'll use the following equation:

h(t) = A * cos(B(t - C)) + D

where h(t) is the height of the pendulum at time t, A is the amplitude, B is the angular frequency, C is
the phase shift, and D is the vertical shift.

The amplitude A is half the difference between the maximum and minimum heights, which is 31
inches / 2 = 15.5 inches. The period T is the time it takes for one complete oscillation, which is 2 *
0.875 seconds = 1.75 seconds. The angular frequency B can be found using the formula:

B = 2 * pi / T

The phase shift C is the amount of time it takes for the pendulum to reach its maximum height,
which we can set to 0 for simplicity. The vertical shift D is the minimum height of the pendulum,
which is 31 inches.

Putting it all together:

h(t) = 15.5 * cos(2 * pi / 1.75 * (t - 0)) + 31

2. To find the height of the pendulum in one hour, we'll plug in t = 1 hour = 60 minutes = 3600
seconds:

h(3600) = 15.5 * cos(2 * pi / 1.75 * (3600 - 0)) + 31

3. To find the minimum and maximum heights, we'll use the maximum and minimum values of the
cosine function:
 Maximum height: cos(x) = 1, so h(t) = 15.5 * 1 + 31 = 46.5 inches
 Minimum height: cos(x) = -1, so h(t) = 15.5 * -1 + 31 = 15.5 inches

So the minimum height the pendulum will reach is 15.5 inches, and the maximum height the
pendulum will reach is 46.5 inches.

You might also like