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]
3 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]3 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
Simplify the following expressions, then implement them using digital logic gates. (a) f = A + AB + AC (b) f = AB + AC + BC (c)
Tema [17]

<u>Explanation</u>:

(a)

f=A+A B+A C\\=A[1+B+C]=A \quad[1+x=1]\\F=A

No gate is required to implement this function

(b)

\begin{aligned}&\ f=A B+\bar{A} C+B C\\&\therefore f=A B+A C\end{aligned}                                  \begin{array}{l}(A B+\bar{A} C+B E=A B+\bar{A} C \\B C \text { is redendant })\end{array}

Note: Refer the first image.

(c)

\begin{aligned}f &=\overline{A+B}+A \bar{B}+B \bar{C} \\&=(\bar{A} \bar{B})+A \bar{B}+B \bar{C} \\&=\bar{B}[A+\bar{A}]+B \bar{C} \\& F=\bar{B}+B \bar{C} =\bar{B}+\bar{C}\end{aligned}    

Note: Refer the second image      

(d)

\begin{aligned}f=& A B \bar{c}+\overline{A+\bar{c}} \\=& A B \bar{c}+\bar{A} \bar{c}=\bar{A} B \bar{c}+\bar{A} c \\f=& \bar{A}[c+B \bar{c}] . \\& f=\bar{A} B+\bar{A} c=\bar{A}(B+c)\end{aligned}

Note: Refer the third image

(e)

\begin{aligned}f=& A \bar{B}+\bar{B} C+A \bar{B} \\&=\bar{B}[A+\bar{A}+c] \\&=\bar{B}[1+C]\end{aligned}

       f=\frac{}{B}

(f)

\begin{aligned}f &=A B C+A B D+A B C \\&=A B[C+C]+A B D \\&=A B+A B D \\&=B[A+A D] \\&=B[A+D] \\\therefore & A=B[A+D]\end{aligned}

Note: Refer the fourth image

                         

6 0
3 years ago
1. The system must be able to manage multiple students (max of 15) and their grades for assignments from three different assignm
mr_godi [17]

Answer:

#include<iostream>

#include<string.h>

using namespace std;

class gradebook;

class student

{

friend class gradebook;

char name[20]; // std name

int homework_grades[6], quiz_grades[6], exam_grades[4]; //grades array

float homework_avg, quiz_avg, exam_avg, final_avg;

};

class gradebook

{

student list[16];

public:

char gb_name[20]; // gradebook name

int max, total;

// Constructor

gradebook()

{

strcpy(gb_name,"noname");

max = 5;

total = 0;

for(int i=1;i<=15;i++)

{

strcpy(list[i].name,"noname");

for(int j=1;j<=5;j++)

{

list[i].homework_grades[j] = -1;

list[i].quiz_grades[j] = -1;

}

list[i].exam_grades[1] = list[i].exam_grades[2] = list[i].exam_grades[3] = -1;

}

}

// Member functions

void display_std_name();

void display_grades(int, int);

int add_std(char *new_name);

int remove(int);

//int ch_grade(int, int);

};

// Define all functions

// Remove a student from list and adjust remaining

int gradebook::remove(int std_id)

{

if(total==0 || std_id>total || std_id<=0)

return 0;

if(std_id==total)

{

cout<<"\n“"<<list[std_id].name<<"” has been successfully removed! (New class size: 2)";

strcpy(list[total--].name,"noname");

return 1;

}

cout<<"\n“"<<list[std_id].name<<"” has been successfully removed! (New class size: 2)";

for(int i=std_id; i<total; i++)

{

list[i]=list[i+1];

}

total--;

return 1;

}

// Add student to gradebook

int gradebook::add_std(char *new_name)

{

if(total<15)

{

strcpy(list[++total].name, new_name);

return 1;

}

return 0;

}

// Display student name list

void gradebook::display_std_name()

{

for(int i=1;i<=total;i++)

{

cout<<"\n"<<i<<" "<<list[i].name;

}

}

/* Display grades of students

// (grade_id, student_number_in_list)

homework(1), quiz(2), exam(3)

*/

void gradebook::display_grades(int g_id, int std_id)

{

int i;

if(std_id<=total)

{

if(g_id==1)

{

for(i=1;i<6;i++)

{

cout<<"\n"<<i<<". ";

if(list[std_id].homework_grades[i]==-1) cout<<"<ungraded>";

}

}

else if(g_id==2)

{

for(i=1;i<6;i++)

{

cout<<"\n"<<i<<". ";

if(list[std_id].quiz_grades[i]==-1) cout<<"<ungraded>";

}

}

else if(g_id==3)

{

for(i=1;i<4;i++)

{

cout<<"\n"<<i<<". ";

if(list[std_id].exam_grades[i]==-1) cout<<"<ungraded>";

}

}

}

else

cout<<"\nNo such std exists";

}

/****************************** MAIN ****************************/

int main()

{

int main_choice, grade_choice, std_id, new_grade;

char g_name[20], s_name[20];

float a,b,c;

gradebook g;

do

{

cout<<"\n\n-=| MAIN MENU |=-";

cout<<"\n1. Add a student\n2. Remove a student\n3. Change a student’s grade\n4. Change group weights\n5. Change the gradebook name\n6. Display class averages\n7. Display full report\n0. QUIT\n";

cout<<"\nEnter an action: ";

cin>>main_choice;

switch(main_choice)

{

case 0:

break;

case 1:

cout<<"\n-=| ADDING STUDENT |=-";

cout<<"\nPlease enter the student’s name: ";

cin>>s_name;

if(g.add_std(s_name))

cout<<"\n"<<s_name<<" was successfully added to the gradebook!";

else

cout<<"\nStudents cannot be added because the gradebook is full!";

break;

case 2:

cout<<"\n-=| REMOVING STUDENT |=-";

if(g.total!=0)

{

g.display_std_name();

cout<<"\n Enter student to remove: ";

cin>>std_id;

if(!g.remove(std_id))

cout<<"\nCannot remove students";

}

else

cout<<"\nStudents cannot be added because the gradebook is full!";

break;

case 3:

cout<<"\n-=| CHANGING GRADE |=-\n1. Change a homework grade\n2. Change a quiz grade\n3. Change an exam grade";

cout<<"\nWhat type of grade would you like to change: ";

cin>>grade_choice;

g.display_std_name();

cout<<"\nWhich student’s grade would you like to change? ";

cin>>std_id;

//ch_grade(grade_choice, std_id);

g.display_grades(grade_choice, std_id);

break;

/*

case 4:

cout<<"\n-=| CHANGING WEIGHTS |=-";

while(1)

{

cout<<"\nEnter the weights, separated by spaces, in the order of homework, quizzes, and exams (total must add up to 1.0):";

cin>>a>>b>>c;

if(a+b+c==1.00)

break;

else

cout<<"\n Weights do not add up to 1.0, try again...";

}

break;

case 5:

cout<<"\n-=| CHANGING NAME |=-\nPlease enter the new name for the gradebook: ";

cin>>g_name;

cout<<"\n Gradebook name changed from “ ” to "<<g_name;

break;

case 6:

cout<<"\n-=| CLASS AVERAGES |=-";

cout<<"\nHomework average of class: ";

cout<<"\nQuiz average of class: ";

cout<<"\nExam average of class: ";

cout<<"\nFinal average of class: ";

break;

case 7:

break;

*/

default:

cout<<"\n\nInvalid input\n\n";

}

}while(main_choice!=0);

return 0;

}

Explanation:

6 0
3 years ago
What is the resolution limit for a projection type of photolithographic system if the incident wavelength is 365 nm (the i-line
Vadim26 [7]

Answer:

Depth of field  = 347.619 nm

Explanation:

wavelenght =  365nm

N.A =0.63

k1= 0.6

so we have that the resolution limit is:

R=k1*A

R=0.6*365

R=219 nm

and the Depth of field needed for the best resolution is:

DoF = Resolution / N.A.

DoF= R/N.A

DoF= 219/0.63

DoF= 347.619 nm

7 0
3 years ago
The in situ moisture content of a soil is 18% and the moist (total) unit weight is 105 pcf. The soil is to be excavated and tran
densk [106]

Answer: Volume= 1.16 yd³

Explanation:

We are given the in-situ moisture content= 18% and moist unit weight= 105 pcf

First we find out the dry unit weight

Dry unit weight= Moist unit weight / (1+ moisture content)

Dry unit weight= \frac{105}{1.18}

Dry unit weight= 88.983 pcf

Now, to find the volume ( in terms of each cubic yard i.e 1 yd³ we have

Volume = Dry unit weight compacted / Dry unit weight (in-situ) * 1 yd³

Volume= \frac{103.5}{88.983} * 1

Volume= 1.16 yd³

6 0
4 years ago
A capillary tube is immersed vertically in a water container. Knowing that water starts to evaporate when the pressure drops bel
REY [17]

Answer: 10.12m, 20micro meter

Explanation:

8 0
3 years ago
Other questions:
  • Women make up about 20% of the workforce in the aviation industry. With such a small percentage of female employees, JetBlue mus
    8·2 answers
  • A stem and leaf display
    12·1 answer
  • 5. Identify the pros and cons of<br> manufactured siding.
    12·1 answer
  • Can anyone pls help.Will mark as branliest
    14·1 answer
  • A 50kg block of nickel at 90°C is dropped into an insulated tank that contains 0.5 m3 of liquid water at 25°C. Determine the fol
    9·2 answers
  • An oil pump is drawing 44 kW of electric power while pumping oil with rho = 860 kg/m^3 at a rate of 0.07 m^3/s. The inlet and ou
    8·1 answer
  • A seasonal color change helps animals in the Taiga to
    11·1 answer
  • The steady-state data listed below are claimed for a power cycle operating between hot and cold reservoirs at 1200 K and 400 K,
    5·1 answer
  • How does Accenture generate value for clients through Agile and DevOps?
    15·2 answers
  • Pls help me Extra points
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!