plot

package module
v0.0.0-...-2631b70 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2022 License: MIT Imports: 13 Imported by: 0

README

plot

Go simple-to-use library for plotting and interpreting data, including the Gaussian distribution and Taylor series approximations as well as statistical computations of datasets, vector and matrix visualizations and more.

GoDoc

The plot package is easy to use and it offers a vast ammount of data methods. You can upload your own dataset to plot or use a dataset with the correspondant output. Looking forward to get some feedback!

Let's take a look at some functionality!

Installation

Pretty straightforward.

go get github.com/timothy102/plot

Import the library in your Go file.

import "github.com/timothy102/plot"

Usage

Let's look at a dataset using the cosine function. Second and third argument to the function invoke the starting and end point of iteration. You can adjust the iterations parameter to get a more dense distribution.

points := plot.DefineDataset(math.Cos, -10.0, 10.0, 500)
plot.PlotPoints(points, "Cosine.png", false)

alt text

That is how simple it is. If you would like to import your dataset externally, use the ReadFromDatafile function. The rest is the same.

points,err:=plot.ReadFromDatafile(filepath)
//error handling
if err:=PlotPoints(points,"Graph.png",false);err!=nil{
  log.Fatalf("could not plot data :%v",err)
}

Taylor series approximation for the Tan function and its error with the real deal. The last argument indicates the polynomial degree to which the function approximates.

points := plot.DefineDatasetWithPolynomial(TanEstimate, -2.0, 2.0, 250, 15)
e := EstimationError(math.Tan, points)
fmt.Printf("%.9f\n", e)

And the error is:

0.000000059

Taylor series approximation of the 15th degree polynomial of Sine and the difference between Sine.

points := plot.DefineDatasetWithPolynomial(plot.SinusEstimate, -5.0, 5.0, 250, 14)
plot.PlotPoints(points, "SinusEstimate.png", false)

p2 := plot.DefineDataset(math.Sin, -5.0, 5.0, 250)
plot.PlotPoints(p2, "Sinus.png", false)
	
e := plot.ErrorBetweenPoints(points, p2)
fmt.Printf("%.7f", e)

alt text alt text

With an average error of:

0.0000085

Let's take a look at creating a random dataset and then normalizing it so the data is centered and it has the standard deviation of 1 The DefineRandomPoints creates a 1000 points between 0 and 10.

pts := plot.DefineRandomPoints(1000, 0.0, 10.0)
pts = plot.Normalize(pts)
plot.PlotPoints(pts, "RandomPoints.png", true)

alt text alt text

And finally, the Gaussian distribution. First parameter is the mean value, the second is the standard deviation and the third is the number of iterations. Adjust these parameters for experimentation.

PlotGaussian(4.2,1.2,1000)

alt text

Contact

Please, feel free to reach out on LinkedIn, gmail. For more, check my medium article.

https://towardsdatascience.com/golang-as-the-new-machine-learning-powerforce-e1b74b10b83b

https://www.linkedin.com/in/tim-cvetko-32842a1a6/

License

Licensed under the MIT LICENSE

Documentation

Overview

Package plot implements plotting functionality for all kinds of data, including the Gaussian distribution, Taylor series approximations and more. It provides an insightful way to data representation and interpretation along with vector and matrix visualisations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Absolute

func Absolute(x float64) float64

Absolute returns absolute value of x

func ApplyProductRule

func ApplyProductRule(f func(x float64) float64, base, power float64) func(float64) float64

ApplyProductRule returns the function via the productRule. Note that the input function should be of shape a*b**x

func AreDistant

func AreDistant(a, b int) bool

AreDistant returns true if the largest common divider between a and b is 1, meaning they have no common dividers.

func AverageEuclidean

func AverageEuclidean(pts Points) float64

AverageEuclidean returns the average euclidean distance by iteration through all Points.

func AverageX

func AverageX(pts Points) float64

AverageX computes the average of x coordinates of the dataset

func AverageY

func AverageY(pts Points) float64

AverageY computes the average of y coordinates of the dataset

func BiggestIndividualError

func BiggestIndividualError(est, real Points) float64

BiggestIndividualError returns the highest error of individual Points.

func Bisection

func Bisection(f func(float64) float64, down, upper float64, iter int) float64

Bisection method for finding null values of the polynom by disecting the interval given down and upper parameters in this case.

func ClosestToZero

func ClosestToZero(pts Points) float64

ClosestToZero returns value closest to zero. You can use this function to see which approximation best estimates the actual function.

func Correlation

func Correlation(pts Points)

Correlation prints the memo for how the X and Y of the dataset are correlated.

func CosineEstimate

func CosineEstimate(x float64, polynomial int) float64

CosineEstimate returns the Taylor series cosine approximate of X.

func Cost

func Cost(pts Points, k, n float64) float64

Cost returns the cost function.

func Covariance

func Covariance(pts Points) float64

Covariance returns the covariance of a given dataset

func CovarianceMatrix

func CovarianceMatrix(pts Points) matrix.Matrix

CovarianceMatrix returns the covariance matrix of the dataset.

func CrossEntropy

func CrossEntropy(f func(x float64) float64, pts Points) float64

CrossEntropy returns the cross entropy loss

func DegreesToRadians

func DegreesToRadians(x float64) float64

DegreesToRadians converts degrees to radians

func DisplayAllStats

func DisplayAllStats(pts Points)

DisplayAllStats prints all the statistics considering the dataset.

func DistanceFromOrigin

func DistanceFromOrigin(p Point) float64

DistanceFromOrigin returns the euclidean distance from origin.

func DoApproximation

func DoApproximation(pts Points, learningRate float64, iterations int, file string) error

DoApproximation uses the ApproximateLine outputs to plot the approximation line.

func DrawApproximation

func DrawApproximation(pts Points, k, n float64, file string) error

DrawApproximation draws a line of approximation based on k,n which should be the outputs of ApproximiateLine

func DrawLine

func DrawLine(p1, p2 Point) (*plotter.Line, error)

DrawLine draws a line between p1 and p2.

func DrawStddev

func DrawStddev(pl *plot.Plot, pts Points) error

DrawStddev plots the standard deviation

func ErrorBetweenPoints

func ErrorBetweenPoints(est, real Points) float64

ErrorBetweenPoints returns the average error between est and real.

func EstimationError

func EstimationError(f func(x float64) float64, pts Points) float64

EstimationError returns the average error between Points and the function f at same parameters.

func Euclidean

func Euclidean(p1, p2 Point) float64

Euclidean returns the Euclidean distance between two Points.

func ExponentialEstimate

func ExponentialEstimate(x float64, polynomial int) float64

ExponentialEstimate returns the Taylor series exponential approximate of X. E to the x.

func F1Score

func F1Score(p1, p2 Points) int

F1Score based on the bool of Points, p1 should be the predicted value and p2 the actual values.

func Factorial

func Factorial(n int) int

Factorial returns the factorial of n

func FalseNegatives

func FalseNegatives(p1, p2 Points) int

FalseNegatives returns the number of false negative predicted values.

func FalsePositives

func FalsePositives(p1, p2 Points) int

FalsePositives returns the number of false positive predicted values.

func FindMaxX

func FindMaxX(pts Points) float64

FindMaxX returns the Point with the highest X value

func FindMaxY

func FindMaxY(pts Points) float64

FindMaxY returns the Point with the highest Y value

func FindMinX

func FindMinX(pts Points) float64

FindMinX returns the Point with the lowest X value

func FindMinY

func FindMinY(pts Points) float64

FindMinY returns the Point with the lowest Y value

func FixDifferentPointSizes

func FixDifferentPointSizes(p1, p2 Points) (Points, Points)

FixDifferentPointSizes equalizes Point sizes by shortening the longer one.

func Gaussian

func Gaussian(x float64, mean float64, stddev float64) float64

Gaussian returns the Gaussian distribution function. For plotting, check PlotGaussian().

func GetEigenValues

func GetEigenValues(m matrix.Matrix) ([]float64, error)

GetEigenValues returns the eigenvalues. Inputs should be eigenvectors.

func GetIndex

func GetIndex(pts Points, p Point) (x int)

GetIndex returns the index of p in Points

func GetValueX

func GetValueX(pts Points, index int) float64

GetValueX returns the X coordinate of Point at index.

func GetValueY

func GetValueY(pts Points, index int) float64

GetValueY returns the Y coordinate of Point at index.

func GradientAt

func GradientAt(f func(x float64) float64, x float64) float64

GradientAt returns the gradient of f at x

func GradientLines

func GradientLines(f func(float64) float64, stPoint, endPoint float64, step float64, file string) error

GradientLines plots the tangents based on the gradients of the function f, given the starting and end point and the step

func Gradients

func Gradients(f func(x float64) float64, startPoint, endPoint float64) []float64

Gradients returns the gradients from startPoint to endPoint See GradientAt for gradient at a specific Point.

func IndexAtClosestToValue

func IndexAtClosestToValue(pts Points, value float64) int

IndexAtClosestToValue returns the index of the Point that is closest to the value on the Y axis.

func IndexAtClosestToZero

func IndexAtClosestToZero(pts Points) int

IndexAtClosestToZero returns the index of the value within Points that is closest to zero.

func IndexAtMedianX

func IndexAtMedianX(pts Points) int

IndexAtMedianX returns the index of the median x value of the dataset.

func IndexAtMedianY

func IndexAtMedianY(pts Points) int

IndexAtMedianY returns the index of the median y value of the dataset.

func Integral

func Integral(f func(x float64) float64, a, b float64, n int) float64

Integral defines a definite integral between the Points a and b for function f. N represents the number to which you want to divide the space from a and b into. You should be getting better approximations for the area under the curve by increasing n.

func IntegralByTrapezoid

func IntegralByTrapezoid(f func(x float64) float64, a, b float64, n int) float64

IntegralByTrapezoid defines a definite integral between the Points a and b for function f. N represents the number of trapezoids you want to divide the space between a and b into. You should be getting better approximations for the area under the curve by increasing n.

func Intercept

func Intercept(p1, p2 Point) float64

Intercept returns the intercept or the value of the linear function at Point 0.

func JaccardIndex

func JaccardIndex(p1, p2 Points) int

JaccardIndex returns the Jaccard index.

func LargestCommonDivider

func LargestCommonDivider(a, b int) int

LargestCommonDivider returns the largest common divider between a and b

func LassoRegression

func LassoRegression(actual, pred []float64, lambda float64) float64

LassoRegression returns the LassoRegression or the l1 regularization to the loss function.

func LinearRegression

func LinearRegression(pts Points, lr float64, iterations int) (k, c float64)

LinearRegression returns the optimal line via gradient descent and returns the coefficient and the intercept.

func MaclaurinFor1Over1MinusX

func MaclaurinFor1Over1MinusX(x float64, polynomial int) float64

MaclaurinFor1Over1MinusX is designed to estimate 1/(1-x) function via Maclaurin series

func MedianX

func MedianX(pts Points) float64

MedianX returns the median x value of the dataset.

func MedianY

func MedianY(pts Points) float64

MedianY returns the median y value of the dataset.

func Mse

func Mse(f func(x float64) float64, pts Points) float64

Mse returns the mean squared error between the function f and Points

func OutOfRange

func OutOfRange(pts Points, index int) bool

OutOfRange returns true if index is out of range.

func PartialsForLinReg

func PartialsForLinReg(pts Points, k, c float64) (dk, dc float64)

PartialsForLinReg returns the

func PlotEigen

func PlotEigen(m matrix.Matrix, file string) error

PlotEigen plots m's eigenvectors

func PlotGaussian

func PlotGaussian(mean, stddev float64, iterations int) error

PlotGaussian plots the Gaussian distribution with the default filename of "Gaussian"

func PlotMatrix

func PlotMatrix(m matrix.Matrix, file string) error

PlotMatrix returns the input matrix plotted to a file.

func PlotPoints

func PlotPoints(pts Points, file string, showStddev bool) error

PlotPoints plots the Points onto the file. By default it shows the standard deviation on the graph.

func PlotSinusApproximation

func PlotSinusApproximation(iterations, polynomial int, file string) error

PlotSinusApproximation plots the sinus approximation onto file

func PlotTangents

func PlotTangents(f func(x float64) float64, startingPoint, endPoint float64, iterations int) error

PlotTangents plots the tangent via gradient to the plot

func PlotVector

func PlotVector(v matrix.Vector, file string) error

PlotVector plots the vector onto file.

func PlotVectorAfterMatrixTransformation

func PlotVectorAfterMatrixTransformation(v matrix.Vector, mat matrix.Matrix, file string) error

PlotVectorAfterMatrixTransformation plots vector before and after matrix transorfmation

func PlotVectors

func PlotVectors(vectors []matrix.Vector, file string) error

PlotVectors plots vectors

func PlotsineAndCosine

func PlotsineAndCosine(file string) error

PlotsineAndCosine plots all four derivates.

func PointInDataset

func PointInDataset(pts Points, p Point) bool

PointInDataset returns a bool if a Point belongs to the dataset

func PointToVector

func PointToVector(pts Points) []matrix.Vector

PointToVector is a helper function

func PointToXYs

func PointToXYs(pts Points) plotter.XYs

PointToXYs is a helper function

func Precision

func Precision(p1, p2 Points) int

Precision returns the precision.

func PrintMetrics

func PrintMetrics(p1, p2 Points)

PrintMetrics prints the evaluation metrics.

func PrintPoints

func PrintPoints(pts Points)

PrintPoints prints Points by line.

func RadiansToDegrees

func RadiansToDegrees(x float64) float64

RadiansToDegrees converts radians to degrees

func Recall

func Recall(p1, p2 Points) int

Recall returns the recall.

func RidgeRegression

func RidgeRegression(actual, pred []float64, lambda float64) float64

RidgeRegression returns the RidgeRegression or the l2 regularization to the loss function.

func Rmse

func Rmse(f func(x float64) float64, pts Points) float64

Rmse returns the root mean squared error between the function f and Points

func SamePoints

func SamePoints(p1, p2 Points) bool

SamePoints returns true if p1 dataset is the same as the p2 dataset.

func SameSign

func SameSign(f func(float64) float64, a, b float64) bool

SameSign returns true if for f(a) and f(b) the values are on the side of the x axis. Either both positive or both negative.

func Seasonality

func Seasonality(time, amplitude float64, period, phase int) float64

Seasonality returns the amplitude* seasonalPattern

func Sensitivity

func Sensitivity(p1, p2 Points) int

Sensitivity returns the sensitivity

func Series

func Series(i Season, x float64) float64

Series returns a seasonal plot defined by 5 parameters. X is the actual input, time and slope define the gradient, amplitude is the frequency and noiseLevel is a constant You can use the SeasonalPattern,Seasonality and trend to define this function, but this has been taken care of for you.

func Sigmoid

func Sigmoid(x float64) float64

Sigmoid returns the sigmoid of x

func SigmoidPrime

func SigmoidPrime(x float64) float64

SigmoidPrime returns the derivative of sigmoid.

func SinusEstimate

func SinusEstimate(x float64, polynomial int) float64

SinusEstimate returns the Taylor series sinus approximate of X.

func Slope

func Slope(p1, p2 Point) float64

Slope returns the coeffcient for linear regression

func SmallestIndividualError

func SmallestIndividualError(est, real Points) float64

SmallestIndividualError returns the smallest error of individual Points.

func Specificity

func Specificity(p1, p2 Points) int

Specificity returns the specificity

func StddevX

func StddevX(pts Points) float64

StddevX returns the standard devation of X coordinates

func StddevY

func StddevY(pts Points) float64

StddevY returns the standard devation of Y coordinates

func TanEstimate

func TanEstimate(x float64, polynomial int) float64

TanEstimate returns the Taylor series approximation with polynomial accuracy.

func TriangleArea

func TriangleArea(p1, p2, p3 Point) float64

TriangleArea returns the area of a triangle via the determinant.

func TrueNegatives

func TrueNegatives(p1, p2 Points) int

TrueNegatives returns the number of true negative predicted values.

func TruePositivies

func TruePositivies(p1, p2 Points) int

TruePositivies returns the number of true positive predicted values.

func VarianceX

func VarianceX(pts Points) float64

VarianceX returns the variance of X coordinates

func VarianceY

func VarianceY(pts Points) float64

VarianceY returns the variance of Y coordinates

func VectorToXYs

func VectorToXYs(vectors []matrix.Vector) plotter.XYs

VectorToXYs is a helper function

func Xs

func Xs(pts Points) []float64

Xs returns all x coordinates of the dataset.

func Ys

func Ys(pts Points) []float64

Ys returns all y coordinates of the dataset.

Types

type Category

type Category struct {
	// contains filtered or unexported fields
}

Category used with KNN algorithm

func Knn

func Knn(k int, p Point, pts []Point) Category

Knn is the K-Nearest-Neighbour classifiCation algorithm. You can vary the parameter k for k nearest neighbours to be selected as an estimation. Knn is implemented with the help of the struct Category.

type Point

type Point struct {
	X, Y float64
	Is   bool
	Cat  Category
}

Point struct Use the boolean is and categories only if needed.

func GetPoint

func GetPoint(pts Points, index int) Point

GetPoint returns Point at index.

func HighestAccuracyPolynomial

func HighestAccuracyPolynomial(f func(x float64, polynomial int) float64, fa func(x float64) float64, stPoint, endPoint float64, iterations, uptopolynomial int) ([]Point, int)

HighestAccuracyPolynomial plots the best polynomial approximation of f regarding fa->which should be the actual function. Function returns Points and the polynomial accuracy index.

func NewPoint

func NewPoint(x, y float64) Point

NewPoint returns a new Point object.

func PointNearestValue

func PointNearestValue(pts Points, value float64) Point

PointNearestValue returns the Point which is closest to value on the Y axis

func RandomPoint

func RandomPoint() Point

RandomPoint returns a random Point

func TopKEuclideans

func TopKEuclideans(p Point, pts []Point, k int) []Point

TopKEuclideans returns the closest k points to the point p. Which Points have the shortest Euclidean distance to the Point p. It is used with KNN Algorithm.

type Points

type Points []Point

Points is a slice of Points.

func DefineDataset

func DefineDataset(f func(x float64) float64, stPoint, endPoint float64, iterations int) Points

DefineDataset returns Points given the inputs. The function will iterate from stPoint to endPoint producing iterations number of Points.

func DefineDatasetPolynomial

func DefineDatasetPolynomial(f func(x float64, polynomial int) float64, stPoint, endPoint float64, iterations, polynomial int) Points

DefineDatasetPolynomial returns an array of Points given the inputs. The function will iterate from stPoint to endPoint with iterations.

func DefineDatasetQuadratic

func DefineDatasetQuadratic(f func(x, a, b, c float64) float64, a, b, c float64, stPoint, endPoint float64, iterations int) Points

DefineDatasetQuadratic defines a quadratic dataset

func DefineDatasetToPower

func DefineDatasetToPower(n float64, stPoint, endPoint float64, iterations int) Points

DefineDatasetToPower is designed to plot Points to the power datasets.

func DefineLinearDataset

func DefineLinearDataset(k, n, min, max float64, iterations int) Points

DefineLinearDataset returns points given k=slope and n=the intercept from min to max with iterations.

func DefineNormalDistribution

func DefineNormalDistribution(n int, min, max, mean, stddev float64) Points

DefineNormalDistribution defines the normal distribution via n which is the number of points, min and max, and the mean and the standard deviaton.

func DefineRandomPoints

func DefineRandomPoints(number int, min, max float64) Points

DefineRandomPoints returns 'number' of Points between min and max

func DefineWithRandomNoise

func DefineWithRandomNoise(f func(x float64) float64, stPoint, endPoint float64, iterations int) Points

DefineWithRandomNoise generates a dataset based on a function and random noise

func DiscludeNan

func DiscludeNan(pts Points) Points

DiscludeNan gets rid of every NaN dataPoint

func FlipOverXAxis

func FlipOverXAxis(pts Points) Points

FlipOverXAxis flips Points over the X axis.

func FlipOverYAxis

func FlipOverYAxis(pts Points) Points

FlipOverYAxis flips Points over the Y axis.

func LimitTo

func LimitTo(pts Points, xUpper, xDown, yUpper, yDown float64) Points

LimitTo limits Points to x and y limits

func LimitToXDown

func LimitToXDown(pts Points, xDown float64) Points

LimitToXDown limits Points to an upper X limit.

func LimitToXUpper

func LimitToXUpper(pts Points, xUpper float64) Points

LimitToXUpper limits Points to an upper X limit.

func LimitToYDown

func LimitToYDown(pts Points, yDown float64) Points

LimitToYDown limits Points to an upper X limit.

func LimitToYUpper

func LimitToYUpper(pts Points, yUpper float64) Points

LimitToYUpper limits Points to an upper X limit.

func Normalize

func Normalize(pts Points) Points

Normalize normalizes data so that it is between the interval of 0 and 1

func PointsFromArrays

func PointsFromArrays(xs, ys []float64) Points

PointsFromArrays returns Points from arrays of x and y coordiantes.

func Randomize

func Randomize(pts Points) Points

Randomize randomizes Points to random values.

func ReadFromDatafile

func ReadFromDatafile(filepath string) (Points, error)

ReadFromDatafile reads from the filepath and returns an array of Points.

func RemoveFromIndexDownwards

func RemoveFromIndexDownwards(pts Points, index int) Points

RemoveFromIndexDownwards removes all Points from index Points downwards from the array

func RemoveFromIndexUpwards

func RemoveFromIndexUpwards(pts Points, index int) Points

RemoveFromIndexUpwards removes all Points from index Points upwards from the array

func RemoveFromPoints

func RemoveFromPoints(pts Points, p Point) Points

RemoveFromPoints removes p from Points

func RemovePointAt

func RemovePointAt(pts Points, index int) Points

RemovePointAt removes Point at index from Points.

func ReverseIndexes

func ReverseIndexes(pts Points) Points

ReverseIndexes flips the array indices.

func RoundPointstoDecimals

func RoundPointstoDecimals(pts Points, decimals int) Points

RoundPointstoDecimals rounds all Points to decimals accuracy.

func SeriesDataset

func SeriesDataset(s Season, iterations int) Points

SeriesDataset returns Points from inputs and iterations.

func ShiftDatasetOnX

func ShiftDatasetOnX(pts Points, scalar float64) Points

ShiftDatasetOnX shifts the x coordinates by the scalar

func ShiftDatasetOnY

func ShiftDatasetOnY(pts Points, scalar float64) Points

ShiftDatasetOnY shifts the y coordinates by the scalar

func SortPointsByX

func SortPointsByX(pts Points) Points

SortPointsByX bubble sorts Points, so the Point with the highest X value is the last element of the array.

func SortPointsByY

func SortPointsByY(pts Points) Points

SortPointsByY bubble sorts Points, so the Point with the highest Y value is the last element of the array.

func Standardize

func Standardize(pts Points) Points

Standardize standardizes Points so that they are centered and the standard deviation is 1.

func StretchByFactorX

func StretchByFactorX(pts Points, factor float64) Points

StretchByFactorX streches the x coordinates by the factor. Check how mean and variance change.

func StretchByFactorY

func StretchByFactorY(pts Points, factor float64) Points

StretchByFactorY streches the x coordinates by the factor. Check how mean and variance change.

func VectorToPoints

func VectorToPoints(vectors []matrix.Vector) Points

VectorToPoints is a helper function

type Season

type Season struct {
	// contains filtered or unexported fields
}

Season struct is used with the MessyDataset function to generate a seasonal plot. Start and end are defining an interval where your seasonality would like to occur.

Jump to

Keyboard shortcuts

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