linearModel

package
v0.0.0-...-0705f78 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2018 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Float = gg.Float64

Float is gorgonia's Float64

View Source
var LossFunctions = map[string]Loss{"square": SquareLoss, "log": LogLoss, "cross-entropy": CrossEntropyLoss}

LossFunctions is the map of implemented loss functions

Functions

func CrossEntropyLoss

func CrossEntropyLoss(Ytrue, X, Theta mat.Matrix, Ypred, Ydiff, grad *mat.Dense, Alpha, L1Ratio float64, nSamples int, activation Activation) (J float64)

CrossEntropyLoss is the loss for LogisticRegression and Classifiers J: -y*math.Log(h)-(1.-y)*log(1.-h) grad: hprime*(-y/h + (1-y)/(1-h))

func LogLoss

func LogLoss(Ytrue, X, Theta mat.Matrix, Ypred, Ydiff, grad *mat.Dense, Alpha, L1Ratio float64, nSamples int, activation Activation) (J float64)

LogLoss for one versus rest classifiers

func SquareLoss

func SquareLoss(Ytrue, X, Theta mat.Matrix, Ypred, Ydiff, grad *mat.Dense, Alpha, L1Ratio float64, nSamples int, activation Activation) (J float64)

SquareLoss Quadratic Loss, for regressions Ytrue, X, Theta must be passed in Ypred,Ydiff,Ytmp are temporary matrices passed in here to avoid reallocations. nothing to initialize for them except storage Alpha, L1Ratio are regularization parameters J: mat.Pow(h-y,2)/2 grad: hprime*(h-y)

func ToDenseTensor

func ToDenseTensor(X interface{}) *tensor.Dense

ToDenseTensor converts to a *tensor.Dense accepts []float64 [][]float64 *mat.Dense mat.Matrix

Types

type Activation

type Activation = base.Activation

Activation is borrowed from base package

type BayesianRidge

type BayesianRidge struct {
	LinearModel
	NIter                                 int
	Tol, Alpha1, Alpha2, Lambda1, Lambda2 float
	ComputeScore, Verbose                 bool
	Alpha, Lambda                         float
	Sigma                                 *mat.Dense
	Scores                                []float
}

BayesianRidge regression struct

Example
nSamples, nFeatures, nOutputs := 10000, 5, 5
X := mat.NewDense(nSamples, nFeatures, nil)
X.Apply(func(i int, j int, v float64) float64 {
	return rand.NormFloat64() * 20
}, X)
f := func(X mat.Matrix, i, o int) float {
	if o == 0 {
		return 1. + 2.*X.At(i, 0) + 3.*X.At(i, 1) + 4.*X.At(i, 2)
	}
	return 1. - 2.*X.At(i, 0) + 3.*X.At(i, 1) + float64(o)*X.At(i, 2)

}
Y := mat.NewDense(nSamples, nOutputs, nil)
Y.Apply(func(i int, o int, v float64) float64 {
	return f(X, i, o)
}, Y)
m := NewBayesianRidge()
//start := time.Now()
m.Fit(X, Y)
//elapsed := time.Since(start)
Ypred := mat.NewDense(nSamples, nOutputs, nil)
m.Predict(X, Ypred)
r2score := metrics.R2Score(Y, Ypred, nil, "variance_weighted").At(0, 0)
if r2score > .999 {
	fmt.Println("BayesianRidge ok")
}
Output:

BayesianRidge ok

func NewBayesianRidge

func NewBayesianRidge() *BayesianRidge

NewBayesianRidge creates a *BayesianRidge with defaults

func (*BayesianRidge) Fit

func (regr *BayesianRidge) Fit(X0, Y *mat.Dense) base.Transformer

Fit the model

Parameters
----------
X : numpy array of shape [nSamples,nFeatures]
    Training data
y : numpy array of shape [nSamples]
    Target values. Will be cast to X's dtype if necessary

func (*BayesianRidge) FitTransform

func (regr *BayesianRidge) FitTransform(X, Y *mat.Dense) (Xout, Yout *mat.Dense)

FitTransform is for Pipeline

func (*BayesianRidge) Predict

func (regr *BayesianRidge) Predict(X, Y *mat.Dense) base.Regressor

Predict using the linear model. In addition to the mean of the predictive distribution, also its standard deviation can be returned. Parameters ---------- X : {array-like, sparse matrix}, shape = (nSamples, nFeatures)

Samples.

Returns ------- yMean : array, shape = (nSamples,)

Mean of predictive distribution of query points.

"""

func (*BayesianRidge) Predict2

func (regr *BayesianRidge) Predict2(X, Y, yStd *mat.Dense) base.Regressor

Predict2 returns y and stddev

func (*BayesianRidge) Transform

func (regr *BayesianRidge) Transform(X, Y *mat.Dense) (Xout, Yout *mat.Dense)

Transform is for Pipeline

type LinFitOptions

type LinFitOptions struct {
	Epochs, MiniBatchSize int
	Tol                   float64
	Solver                base.Optimizer
	// Alpha is regularization factor for Ridge,Lasso
	Alpha float64
	// L1Ratio is the part of L1 regularization 0 for ridge,1 for Lasso
	L1Ratio          float64
	Loss             Loss
	Activation       Activation
	GOMethodCreator  func() optimize.Method
	ThetaInitializer func(Theta *mat.Dense)
	Recorder         optimize.Recorder
	PerOutputFit     bool
}

LinFitOptions are options for LinFit

type LinFitResult

type LinFitResult struct {
	Converged bool
	RMSE, J   float64
	Epoch     int
	Theta     *mat.Dense
}

LinFitResult is the result or LinFit

func LinFit

func LinFit(X, Ytrue *mat.Dense, opts *LinFitOptions) *LinFitResult

LinFit is an internal helper to fit linear regressions

func LinFitGOM

func LinFitGOM(X, Ytrue *mat.Dense, opts *LinFitOptions) *LinFitResult

LinFitGOM fits a regression with a gonum/optimizer Method

type LinearModel

type LinearModel struct {
	FitIntercept, Normalize          bool
	XOffset, XScale, Coef, Intercept *mat.Dense
}

LinearModel is a base struct for multioutput regressions

func (*LinearModel) DecisionFunction

func (regr *LinearModel) DecisionFunction(X, Y *mat.Dense)

DecisionFunction fills Y with X dot Coef+Intercept

func (*LinearModel) Score

func (regr *LinearModel) Score(X, Y *mat.Dense) float64

Score returns R2Score between Y and X dot Coef+Intercept

type LinearRegression

type LinearRegression struct {
	LinearModel
	Optimizer           base.Optimizer
	Tol, Alpha, L1Ratio float64
	LossFunction        Loss
	ActivationFunction  Activation
	Options             LinFitOptions
}

LinearRegression ia Ordinary least squares Linear Regression. Parameters ---------- fitIntercept : boolean, optional, default True

whether to calculate the intercept for this model. If set
to False, no intercept will be used in calculations
(e.g. data is expected to be already centered).

normalize : boolean, optional, default False

This parameter is ignored when ``fitIntercept`` is set to False.
If True, the regressors X will be normalized before regression by
subtracting the mean and dividing by the l2-norm.
If you wish to standardize, please use
:class:`sklearn.preprocessing.StandardScaler` before calling ``fit`` on
an estimator with ``normalize=False``.

---------- coef : array, shape (nFeatures, ) or (nTargets, nFeatures)

Estimated coefficients for the linear regression problem.
If multiple targets are passed during the fit (y 2D), this
is a 2D array of shape (nTargets, nFeatures), while if only
one target is passed, this is a 1D array of length nFeatures.

intercept : array

Independent term in the linear model.

func NewLasso

func NewLasso() *LinearRegression

NewLasso creates a *Lasso with defaults

func NewLinearRegression

func NewLinearRegression() *LinearRegression

NewLinearRegression create a *LinearRegression with defaults implemented as a per-output optimization of (possibly regularized) square-loss a base.Optimizer (defaults to Adam)

func NewRidge

func NewRidge() *LinearRegression

NewRidge creates a *Ridge with defaults

func (*LinearRegression) Fit

func (regr *LinearRegression) Fit(X0, Y0 *mat.Dense) base.Transformer

Fit fits Coef for a LinearRegression

func (*LinearRegression) FitTransform

func (regr *LinearRegression) FitTransform(X, Y *mat.Dense) (Xout, Yout *mat.Dense)

FitTransform is for Pipeline

func (*LinearRegression) Predict

func (regr *LinearRegression) Predict(X, Y *mat.Dense) base.Regressor

Predict predicts y for X using Coef

func (*LinearRegression) Transform

func (regr *LinearRegression) Transform(X, Y *mat.Dense) (Xout, Yout *mat.Dense)

Transform is for Pipeline

type LinearRegressionGorgonia

type LinearRegressionGorgonia struct {
	LinearModel
	Epochs                            int
	LearningRate, Tol, Alpha, L1Ratio float
	// contains filtered or unexported fields
}

LinearRegressionGorgonia is a multioutput libear regression using gorgonia

func NewLinearRegressionGorgonia

func NewLinearRegressionGorgonia() *LinearRegressionGorgonia

NewLinearRegressionGorgonia create a *LinearRegressionGorgonia with good defaults

func (*LinearRegressionGorgonia) Fit

func (regr *LinearRegressionGorgonia) Fit(X0, y0 *mat.Dense) base.Transformer

Fit lears coef and intercept for a *LinearRegressionGorgonia

func (*LinearRegressionGorgonia) FitTransform

func (regr *LinearRegressionGorgonia) FitTransform(X, Y *mat.Dense) (Xout, Yout *mat.Dense)

FitTransform is for Pipeline

func (*LinearRegressionGorgonia) Predict

func (regr *LinearRegressionGorgonia) Predict(X, Y *mat.Dense) base.Regressor

Predict return predicted Ys for a list or Xs

func (*LinearRegressionGorgonia) Transform

func (regr *LinearRegressionGorgonia) Transform(X, Y *mat.Dense) (Xout, Yout *mat.Dense)

Transform is for Pipeline

type LogisticRegression

type LogisticRegression struct {
	LinearRegression
}

LogisticRegression WIP

func NewLogisticRegression

func NewLogisticRegression() *LogisticRegression

NewLogisticRegression create and init a *LogisticRegression

func (*LogisticRegression) Predict

func (regr *LogisticRegression) Predict(X, Y *mat.Dense)

Predict predicts y for X using Coef

func (*LogisticRegression) PredictProba

func (regr *LogisticRegression) PredictProba(X, Y *mat.Dense)

PredictProba predicts probabolity of y=1 for X using Coef

type Loss

type Loss func(Ytrue, X, Theta mat.Matrix, Ypred, Ydiff, grad *mat.Dense, Alpha, L1Ratio float64, nSamples int, activation Activation) (J float64)

Loss puts cost in J and cost gradient in grad. Ytrue, X, Theta must be passed in Ypred,Ydiff,Ytmp are temporary matrices passed in here to avoid reallocations. nothing to initialize for them except storage Alpha and L1Ratio are for regularization Loss derivative is dJWrtTheta=dJWrth*dhWrtz*X

type SGDRegressor

type SGDRegressor struct {
	LinearModel
	Tol, Alpha, L1Ratio float
	NJobs               int
	Method              optimize.Method
}

SGDRegressor base struct should be named GonumOptimizeRegressor implemented as a per-output optimization of (possibly regularized) square-loss with gonum/optimize methods

func NewSGDRegressor

func NewSGDRegressor() *SGDRegressor

NewSGDRegressor creates a *SGDRegressor with defaults

func (*SGDRegressor) Fit

func (regr *SGDRegressor) Fit(X0, y0 *mat.Dense) base.Transformer

Fit learns Coef

func (*SGDRegressor) FitTransform

func (regr *SGDRegressor) FitTransform(X, Y *mat.Dense) (Xout, Yout *mat.Dense)

FitTransform is for Pipeline

func (*SGDRegressor) Predict

func (regr *SGDRegressor) Predict(X, Y *mat.Dense) base.Regressor

Predict predicts y from X using Coef

func (*SGDRegressor) Transform

func (regr *SGDRegressor) Transform(X, Y *mat.Dense) (Xout, Yout *mat.Dense)

Transform is for Pipeline

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL