1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
RUDIKE [14]
2 years ago
12

Finally you will implement the full Pegasos algorithm. You will be given the same feature matrix and labels array as you were gi

ven 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.
Engineering
1 answer:
Diano4ka-milaya [45]2 years ago
6 0

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)

Explanation:

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)

You might be interested in
A homeowner consumes 260 kWh of energy in July when the family is on vacation most of the time. Determine the average cost per k
Llana [10]

Answer:

16.2 cents

Explanation:

Given that a homeowner consumes 260 kWh of energy in July when the family is on vacation most of the time.

Where Base monthly charge of $10.00. First 100 kWh per month at 16 cents/kWh. Next 200 kWh per month at 10 cents/kWh. Over 300 kWh per month at 6 cents/kWh.

For the first 100 kWh:

16 cent × 100 = 1600 cents = 16 dollars

Since 1 dollar = 100 cents

For the remaining energy:

260 - 100 = 160 kwh

10 cents × 160 = 1600 cents = 16 dollars

The total cost = 10 + 16 + 16 = 42 dollars

Note that the base monthly of 10 dollars is added.

The cost of 260 kWh of energy consumption in July is 42 dollars

To determine the average cost per kWh for the month of July, divide the total cost by the total energy consumed.

That is, 42 / 260 = 0.1615 dollars

Convert it to cents by multiplying the result by 100.

0.1615 × 100 = 16.15 cents

Approximately 16.2 cents

7 0
3 years ago
You want to see both Michael and Meet Wally Sparks. If you purchase tickets for both
Aleonysh [2.5K]
$2.

Both tickets cost $1.50
$1.50 x 2 = $3
$5 - $3 = $2
6 0
2 years ago
What type of engineer makes sure equipment is safe and operational
zvonat [6]

Answer:

mechanical engineer is the best answer

8 0
3 years ago
4. Which 2D shape on the left would be used to make the 3D shape on the right? (1 pt.)
dexar [7]

it would be a bc its a sqare?well 3d is like you can say a cube 2d is like flat

Explanation:

7 0
2 years ago
A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design the class Point that can store and process
Black_prince [1.1K]

Answer:

#include <iostream>

#include <iomanip>

using namespace std;

class pointType

{

public:

pointType()

{

x=0;

y=0;

}

pointType::pointType(double x,double y)

{

this->x = x;

this->y = y;

}          

void pointType::setPoint(double x,double y)

{

this->x=x;

this->y=y;

}

void pointType::print()

{

cout<<"("<<x<<","<<y<<")\n";

}

double pointType::getX()

{return x;

}

double pointType::getY()

{return y;

}

private:

   double x,y;

};

int main()

{

pointType p2;

double x,y;

cout<<"Enter an x Coordinate for point ";

cin>>x;

cout<<"Enter an y Coordinate for point ";

cin>>y;

p2.setPoint(x,y);

p2.print();

system("pause");    

return 0;

}

5 0
3 years ago
Other questions:
  • An aluminum cylinder bar ( 70 GPa E m = ) is instrumented with strain gauges and is subject to a tensile force of 5 kN. The diam
    9·1 answer
  • I am trying to create a line of code to calculate distance between two points. (distance=[tex]\sqrt{ (x2-x1)^2+(y2-y1)^2}) My li
    5·1 answer
  • Using any of the bilinear transform, matched pole-zero, or impulse invariance techniques in converting a continuous-time system
    14·2 answers
  • What is pneumatic troubleshooting
    6·1 answer
  • If the outside diameter of a pipe is 2 m, the length of a piece of insulation wrapped around it would be a)- 628 cm b)- 12.56 m.
    15·1 answer
  • An operating gear box (transmission) has 350 hp at its input shaft while 250. hp are delivered to the output shaft. The gear box
    10·1 answer
  • Which statement concerning symbols used on plans is true?
    10·1 answer
  • Calculate total hole mobility if the hole mobility due to lattice scattering is 50 cm2 /Vsec and the hole mobility due to ionize
    5·2 answers
  • The hydraulic tool holder is a type of what? A.port B. clamp C. Press D. Actuator​
    11·1 answer
  • When using fall arrest, free fall must be kept at or below how many feet
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!