Skip to content

LEAST_SQUARES

Download Flojoy Studio to try this app
Perform a least squares regression on the input DataContainer (input can be a Matrix or OrderedPair). Params: a : OrderedPair|Matrix A list of points or a coefficient matrix. b : Optional[OrderedPair|Matrix] Ordinate or "dependent variable" values. Returns: out : OrderedPair or Matrix OrderedPair x: input matrix (data points) y: fitted line computed with returned regression weights Matrix m: fitted matrix computed with returned regression weights
Python Code
import numpy as np
from typing import Optional
from flojoy import flojoy, OrderedPair, Matrix


@flojoy
def LEAST_SQUARES(
    a: OrderedPair | Matrix, b: Optional[OrderedPair | Matrix] = None
) -> Matrix | OrderedPair:
    """Perform a least squares regression on the input DataContainer (input can be a Matrix or OrderedPair).

    Parameters
    ----------
    a : OrderedPair|Matrix
        A list of points or a coefficient matrix.
    b : Optional[OrderedPair|Matrix]
        Ordinate or "dependent variable" values.

    Returns
    -------
    OrderedPair or Matrix
        OrderedPair
        x: input matrix (data points)
        y: fitted line computed with returned regression weights

        Matrix
        m: fitted matrix computed with returned regression weights
    """

    if b is None:
        if isinstance(a, OrderedPair):
            x = a.x
            y = a.y
            try:
                a = np.vstack([x, np.ones(len(x))]).T
                p = np.linalg.lstsq(a, y, rcond=None)[0]
            except np.linalg.LinAlgError:
                raise ValueError("Least Square Computation failed.")

            slope, intercept = p[0:-1], p[-1]
            res = slope * x + intercept

            return OrderedPair(x=x, y=res)
        else:
            raise ValueError("For matrix type b must be specified!")
    else:
        if isinstance(a, OrderedPair) and isinstance(b, OrderedPair):
            x = a.y
            y = b.y

            try:
                a = np.vstack([x, np.ones(len(x))]).T
                p = np.linalg.lstsq(a, y, rcond=None)[0]
            except np.linalg.LinAlgError:
                raise ValueError("Least Square Computation failed.")

            slope, intercept = p[0:-1], p[-1]
            print("=============== This is slope: ", slope)
            print("=============== This is intercept: ", intercept)
            res = slope * x + intercept

            return OrderedPair(x=x, y=res)

        elif isinstance(a, Matrix) and isinstance(b, Matrix):
            x = a.m
            y = b.m

            try:
                a = np.vstack([x, np.ones(len(x))]).T
                p = np.linalg.lstsq(a, y, rcond=None)[0]
            except np.linalg.LinAlgError:
                raise ValueError("Least Square Computation failed.")

            slope, intercept = p[0:-1], p[-1]
            res = slope * x + intercept

            return Matrix(m=res)
        else:
            raise ValueError("a and b must be of same type!")

Find this Flojoy Block on GitHub

Example App

Having problems with this example app? Join our Discord community and we will help you out!
React Flow mini map

In this example, two LINSPACE each generates an array of 300 samples.

One of the array is passed down to ‘RAND’ node which randomizes the data points.

Two OrderedPairs are passed down to ‘LEAST SQUARES’ which calculates the coefficients that minimize the errors from all the data points. Then for the simplicity of plotting, the initial X matrix from first linspace is multiplied to the coefficients so that the line is drawn and it can be passed as OrderedPair with X.