Computer Science‎ > ‎

### Stats, ML, Data-Programs for Linear Regression with multiple variables

Programs to solve linear regression in one or more variables.
Core parts of the algorithmic/computational aspect of regression have been highlighted in blue.

## Python Program to solve a linear regression problem using the Numpy library

`import numpy as np`
`import sys`
`import math`

`F,N = map(int,sys.stdin.readline().split(" "))`
`X = []`
`y = []`

`for row in xrange(0,N):`
`values = map(float,sys.stdin.readline().split())`
`X += [[1]+values[0:F]]`
`y += [values[F]]`

`coefficients = np.linalg.lstsq(np.array(X),np.array(y))[0]`

`T = int(raw_input())`

`for row in xrange(0,T):`
`values = [1] + map(float,sys.stdin.readline().split())`
`result = 0`
`for index in xrange(0,F+1):`
`result += values[index] * coefficients[index]`
`print math.ceil(result*110)/100`

C++ Program to solve a linear regression problem, using a Gradient Descent method
`#include <cmath>`
`#include <cstdio>`
`#include <vector>`
`#include <iostream>`
`#include <algorithm>`
`using namespace std;`

`float priceof(const vector<float> &x, const vector<float> &y){`
`    float sum = 0.0;`
`    `
`    int n = x.size();`
`    for(int i=0; i < n; i++){`
`        sum += x[i] * y[i];`
`    }`
`    `
`    return sum;`
`}`

`float costfunc(const vector<vector<float>> &characteristics, const vector<float> &prices, const vector<float> &factors, int n, int c){`
`    vector<float> h(n);`
`    float sum = 0.0;`
`    `
`    for(int i = 0; i < n; i++){`
`        h[i] = priceof(characteristics[i], factors);`
`        sum += (h[i]-prices[i]) * (h[i]-prices[i]) / (2*(n));`
`    }`
`    `
`    return sum;`
`}`

`void printfactors(const vector<float> & factors){`
`    int n = factors.size();`
`    cout << "factors: ";`
`    for(int i = 0; i < n; i++)`
`        cout << factors[i] << " ";`
`        `
`    cout << endl;`
`}`

`void gradientdescent(const vector<vector<float>> &characteristics, const vector<float> &prices, vector<float> &factors, int n, int c, float alpha){`
`    vector<float> h(n);`
`    float sum = 0.0;`
`    `
`    for(int i = 0; i < n; i++)`
`        h[i] = priceof(characteristics[i], factors);`
`    `
`    `
`    for(int j = 0; j < c+1; j++){`
`        for(int i = 0; i < n; i++){`
`            sum += (h[i]-prices[i])*characteristics[i][j]*alpha/n;`
`        }`
`        `
`        factors[j] -= sum;`
`        sum = 0.0;`
`    }`
`    `
`}`

`int main() {`
`    int c, n;`
`    cin >> c >> n;`
`    vector<vector<float>> characteristics(n);`
`    vector<float> prices;`
`    vector<float> factors(c+1, 1.0);`
`    double pricemean;`
`    `
`    for(int i = 0; i < n; i++){`
`        float holder;`
`        for(int j = 0; j < c; j++){`
`            cin >> holder;`
`            characteristics[i].push_back(holder);`
`        }`
`        characteristics[i].push_back(1.0);`
`        cin >> holder;`
`        prices.push_back(holder);`
`        pricemean += holder;`
`    }`
`    `
`    //pricemean /= n;`
`    //factors[c] = pricemean;`
`    `
`    float cost = costfunc(characteristics, prices, factors, n, c);`
`    float costold = 0.0;`
`    float e = 0.001;`
`    `
`    `
`    while(abs(cost-costold) > e ){`
`        gradientdescent(characteristics, prices, factors, n, c, 0.1);`
`        costold = cost;`
`        cost = costfunc(characteristics, prices, factors, n, c);`
`    }`
`    `
`    int t;`
`    cin >> t;`
`    vector<float> target;`
`    `
`    for(int i = 0; i < t; i++){`
`        float holder;`
`        for(int j = 0; j < c; j++){`
`            cin >> holder;`
`            target.push_back(holder);`
`        }`
`        target.push_back(1.0);`
`        `
`        cout << priceof(target, factors) << endl;`
`        `
`        target.clear();`
`    }`
`    return 0;`
`}`

Ruby Program to solve a linear regression problem, using a matrix inversion based method

`#!/usr/bin/env ruby`

`require 'matrix'`

`f, n = gets.split.map {|i| i.to_i}`
`xrows = []`
`yrows = []`

`n.times do`
`  fs = gets.split.map {|f| f.to_f}`
`  xrows << [1, *fs.take(f)]`
`  yrows << [fs.last]`
`end`

`x = Matrix[*xrows]`
`y = Matrix[*yrows]`

`xt = x.transpose`
`b = (xt * x).inverse * xt * y`

`gets.to_i.times do`
`  ans = Matrix[[1, *gets.split.map {|f| f.to_f}]] * b`
`  puts(ans[0, 0])`
`end`

Java Program to solve a linear regression problem, using Weka

`import java.util.Scanner;`

`import weka.core.FastVector;`
`import weka.core.Attribute;`
`import weka.core.Instance;`
`import weka.core.Instances;`
`import weka.classifiers.Classifier;`
`import weka.classifiers.functions.LinearRegression;`

`public class Solution {`
` public static void main(String[] argv) {`
`  Scanner sc = new Scanner(System.in);`
`  int f = sc.nextInt(), n = sc.nextInt();`
`  FastVector fastVectorAtts = new FastVector(f + 1);`
`  for (int i = 0; i < f; i++) {`
`   fastVectorAtts.addElement(new Attribute("f" + Integer.toString(i)));`
`  }`
`  fastVectorAtts.addElement(new Attribute("c"));`
`  Instances dataset = new Instances("Test-dataset", fastVectorAtts, n);`
`  dataset.setClassIndex(f);`
`  Instance is;`
`  double value;`
`  for (int i = 0; i < n; i++) {`
`   is = new Instance(f + 1);`
`   for (int j = 0; j < f + 1; j++) {`
`    value = sc.nextDouble();`
`    is.setValue((Attribute) fastVectorAtts.elementAt(j), value);`
`   }`
`   dataset.add(is);`
`  }`
`  Classifier cModel = new LinearRegression();`
`  double pred;`
`  int t = sc.nextInt();`
`  try {`
`   cModel.buildClassifier(dataset);`
`   for (int i = 0; i < t; i++) {`
`    is = new Instance(f);`
`    for (int j = 0; j < f; j++) {`
`     value = sc.nextDouble();`
`     is.setValue((Attribute) fastVectorAtts.elementAt(j), value);`
`    }`
`    pred = cModel.classifyInstance(is);`
`    System.out.format("%.2f\n", pred);`
`   }`
`  } catch (Exception e) {`
`   System.out.println("Exception message:" + e.getMessage());`
`  }`

` }`
`}`