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
Ostrovityanka [42]
3 years ago
11

You will write a program estimate that uses a training data set to learn weights for a set of house attributes, and then applies

those weights to a set of input data to calculate prices for those houses. estimate takes two arguments, which are the paths to files containing the training data and input data.
Computers and Technology
1 answer:
Bumek [7]3 years ago
8 0

Answer:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

double ** transpose(double ** m,int rows,int columns);

double ** multiply(double ** m1,double ** m2,int rows1,int rows2,int columns);

double * vmultiply(double ** m,double * v,int rows,int columns);

double ** inverse(double ** m,int rows);

int main(int argc,char ** argv) {

   FILE * f1 = NULL; //temp file variables

   FILE * f2 = NULL;

   FILE * t = NULL;

   FILE * d = NULL;

   char * filename1 = NULL;

   char * filename2 = NULL;

   char label[20] = {};  

   int columns,rows;

   double ** temp;

   double ** train;

   double ** data;

   double * prices;

   double * weights;

   double ** tr;

   double ** in;

   double ** r1;  

   double ** r2;

   double * r3;

   if (argc != 3) {

       printf("error\n");

       exit(0);

   }

   filename1 = argv[1];

   filename2 = argv[2];

   f1 = fopen(filename1,"r");

   f2 = fopen(filename2,"r");

   if (f1 == NULL || f2 == NULL) {

       printf("error\n");

       exit(0);

   }

   fscanf(f1,"%s\n",label);

   if ((strncmp(label,"train",5) == 0)) {

       t = f1;

       d = f2;

   } else {

       t = f2;

       d = f1;

   }

   fscanf(t,"%d\n",&columns);

   fscanf(t,"%d\n",&rows);

   columns++; //there are k+1 attributes

   train = (double **) malloc(rows * sizeof(double *));

   temp = (double **) malloc(rows * sizeof(double *));

 

   prices = (double *) malloc(rows * sizeof(double *));

   int i;

   for (i = 0; i < rows; i++) {

       train[i] = (double *) malloc(columns * sizeof(double));

       temp[i] = (double *) malloc(columns * sizeof(double));

       train[i][0] = 1; //make the first column all 1s

   }

   int j;

   for (i = 0; i < rows; i++) {

       for (j = 0; j < columns; j++) {

           fscanf(t,"%lf ",&temp[i][j]);

       }

       fscanf(t,"\n");

   }

   for (i = 0; i < rows; i++) {

       for (j = 1; j < columns; j++) {

           train[i][j] = temp[i][j-1];

       }

   }

   for (i = 0; i < rows; i++) {

       prices[i] = temp[i][columns-1];

   }

   tr = transpose(train,rows,columns);

   r1 = multiply(tr,train,columns,rows,columns);

   in = inverse(r1,columns);

   r2 = multiply(in,tr,columns,columns,columns);

   weights = vmultiply(r2,prices,columns,columns);

 

   fscanf(d,"%s\n",label);

   //make sure there aren't two training data file

   if (strncmp(label,"data",4) != 0) {

       printf("error\n");

       exit(0);

   }

   fscanf(d,"%d\n",&columns);

   fscanf(d,"%d\n",&rows);

   columns++; //there will me k+1 columns

 

   data = (double **) malloc(rows * sizeof(double *));

   for (i = 0; i < rows; i++) {

       data[i] = (double *) malloc(columns * sizeof(double));

       data[i][0] = 1; //make the first column all 1s

   }

   for (i = 0; i < rows; i++) {

       for (j = 1; j < columns; j++) {

           fscanf(d,"%lf ",&data[i][j]);

       }

       fscanf(d,"\n");

   }

   

   r3 = vmultiply(data,weights,rows,columns);

   for (i = 0; i < rows; i++) {

       printf("%.0f\n",r3[i]);

   }

   return 0;

}

double ** transpose(double ** m,int rows,int columns) {

 

   double ** t = (double **) malloc(columns * sizeof(double *));

   int i,j;

   for (i = 0; i < columns; i++) {

       t[i] = (double *) malloc(rows * sizeof(double));

   }

   for (i = 0; i < columns; i++) {

       for (j = 0; j < rows; j++) {

           t[i][j] = m[j][i];

       }

   }

   return t;

}

double ** multiply(double ** m1,double ** m2,int rows1,int rows2,int columns) {

   double ** t = (double **) malloc(rows1 * sizeof(double *));

   int i,j,k;

   for (i = 0; i < rows1; i++) {

       t[i] = (double *) malloc(columns * sizeof(double));

       //make every element 0 to prevent error

       for (j = 0; j < columns; j++) {

           t[i][j] = 0;

       }

   }

   for (i = 0; i < rows1; i++) {

       for (j = 0; j < columns; j++) {

           for (k = 0; k < rows2; k++) {

               t[i][j] += m1[i][k] * m2[k][j];

           }

       }

   }

   return t;

}

double * vmultiply(double ** m,double * v,int rows,int columns) {

   double * t = (double *) malloc(rows * sizeof(double));

   //make sure every element is preset to 0

   int i,j;

   for (i = 0; i < rows; i++) {

       t[i] = 0;

   }

   for (i = 0; i < rows; i++) {

       for (j = 0; j < columns; j++) {

           t[i] += m[i][j] * v[j];

       }

   }

   return t;

}

double ** inverse(double ** m,int size) {

 

   double ** id = (double **) malloc(size * sizeof(double *));

   int i,j,k;

   for (i = 0; i < size; i++) {

       id[i] = (double *) malloc(size * sizeof(double));

       for (j = 0; j < size; j++) {

           if (i == j) {

               id[i][j] = 1;

           } else {

               id[i][j] = 0;

           }

       }

   }

   for (int i = 0; i < size; i++) {

       double rec; //reciprocal value of the pivot

     

       if (m[i][i] != 1) {

           rec = 1/m[i][i];

           

           for (j = 0; j < size; j++) {

               m[i][j] *= rec;

               id[i][j] *= rec; //make the adjustment to the identity matrix

             

               if (m[i][j] == -0) m[i][j] = 0;

               if (id[i][j] == -0) id[i][j] = 0;

           }

       }

     

       for (k = i+1; k < size; k++) {

           double f; //factor of pivot value

           if (m[k][i] != 0) {

               f = m[k][i] * -1;

         

               int l;

               for (l = 0; l < size; l++) {

                   m[k][l] += (f*m[i][l]);

                   id[k][l] += (f*id[i][l]);

                 

                   if (m[k][l] == -0) m[k][l] = 0;

                   if (id[k][l] == -0) id[k][l] = 0;

               }

           }

       }

   }

  for (i = size-1; i >= 0; i--) {

      double rec;

      if (m[i][i] != 1) {

          rec = 1/m[i][i];

          for (j = 0; j < size; j++) {

              m[i][i] *= rec;

              id[i][i] *= rec;

              if (m[i][j] == -0) m[i][j] = 0;

              if (id[i][j] == -0) id[i][j] = 0;

          }

      }

      for (k = i-1; k >= 0; k--) {

           double f;

           if (m[k][i] != 0) {

               f = m[k][i] * -1;

           

               int l;

               for (l = 0; l < size; l++) {

                   m[k][l] += (f*m[i][l]);

                   id[k][l] += (f*id[i][l]);

                 

                   if (m[k][l] == -0) m[k][l] = 0;

                   if (id[k][l] == -0) id[k][l] = 0;

               }

           }

      }

  }

 

  return id;

}

You might be interested in
__________ may be defined as the application of methods and means to ensure that test results are reliable and valid, and errors
kow [346]

Answer:

Quality assurance

Explanation:

8 0
3 years ago
Remember not to use tools that are ________ in any way.
kupik [55]
Remember not to use tools that are rusty and damaged in any way.
3 0
3 years ago
Typically , how do people earn income
natima [27]
People earn income by getting jobs and working. When they work, they get paid. That's an income
3 0
4 years ago
Read 2 more answers
Which of the following is the fastest growing input technique​
Natali [406]
We need more information for this one, please.
8 0
3 years ago
Of the measures of feasibility in the accompanying figure, ____ considers points such as "Does the proposed platform have suffic
Olin [163]

Answer:

technical feasibility

Explanation:

This is the process of validating the technology assumptions, architecture and design of a product or project. A technical feasibility study shows the details of how you intend to deliver a product or service to customers. Puts into consideration the following :

materials, labor, transportation, where your business will be located, and the technology that will be necessary to bring all this together.

3 0
3 years ago
Other questions:
  • Windows 7 is used to run ____ software
    5·2 answers
  • What are 5 actions that you can do to take care of your camera
    14·2 answers
  • The length of time the valve is open, expressed in degrees of crankshaft rotation, is called camshaft
    10·1 answer
  • Why should you log out when you finish an online session?
    9·1 answer
  • The most important protocol at the internet layer is ____.
    13·1 answer
  • Take the hypothetical situation that your business is considering a move to the cloud for core systems that contain confidential
    8·1 answer
  • You have a computer at home. The computer is connected to the Internet through a dial-up connection. Every time you connect to t
    7·1 answer
  • When an object is created, the attributes associated with the object are called: A. instance fields B. instance methods C. fixed
    11·1 answer
  • They have requested a 1- to 2-page memo that describes the hospital's protections against a ransomware attack.
    10·1 answer
  • In a relational database, the three basic operations used to develop useful sets of data are:_________.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!