stat

package
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Nil2Bool              = false      // 空指针转bool
	BoolNaN               = false      // bool 无效值
	True2Bool             = true       // true转bool
	False2Bool            = false      // false 转bool
	True2Float32  float32 = float32(1) // true转float32
	False2Float32 float32 = float32(0) // false转float32

	StringBad2Bool   = false // 字符串解析bool异常
	StringTrue2Bool  = true  // 字符串true转bool
	StringFalse2Bool = false // 字符串false转bool
)
View Source
const (
	MaxFloat32                  = float32(math.MaxFloat32)             // float32最大值
	MinFloat32                  = float32(math.SmallestNonzeroFloat32) // float32最小值
	StringTrue2Float32  float32 = float32(1)                           // 字符串true转float32
	StringFalse2Float32 float32 = float32(0)                           // 字符串false转float32
)
View Source
const (
	MaxFloat64          float64 = float64(math.MaxFloat64)             // float64最大值
	MinFloat64          float64 = float64(math.SmallestNonzeroFloat64) // float64最小值
	True2Float64        float64 = float64(1)                           // true转float64
	False2Float64       float64 = float64(0)                           // false转float64
	StringNil2Float     float64 = float64(0)                           // deprecated: 字符串空指针转float64
	StringBad2Float     float64 = float64(0)                           // deprecated: 字符串解析float64异常
	StringTrue2Float64  float64 = float64(1)                           // 字符串true转float64
	StringFalse2Float64 float64 = float64(0)                           // 字符串false转float64
)
View Source
const (
	MaxInt64          = int64(math.MaxInt64)
	MinInt64          = int64(math.MinInt64)
	Nil2Int64         = int64(0) // 空指针转int64
	Int64NaN          = int64(0) // int64 无效值
	True2Int64        = int64(1) // true转int64
	False2Int64       = int64(0) // false 转int64
	StringBad2Int64   = int64(0) // 字符串解析int64异常
	StringTrue2Int64  = int64(1) // 字符串true转int64
	StringFalse2Int64 = int64(0) // 字符串false转int64
)
View Source
const (
	StringNaN    = "NaN"   // 字符串NaN
	Nil2String   = "NaN"   // nil指针转string
	True2String  = "true"  // true转string
	False2String = "false" // false转string
)

Variables

View Source
var (
	// Nil2Float64 nil指针转换float64
	Nil2Float64 = float64(0)
	// Nil2Float32 nil指针转换float32
	Nil2Float32 = float32(0)
	DTypeNaN    = DType(0)
)
View Source
var (
	// 不支持的类型
	ErrUnsupportedType = exception.New(errorTypeBase+0, "Unsupported type")
)
View Source
var (
	// IgnoreParseExceptions 忽略解析异常
	IgnoreParseExceptions bool = true
)
View Source
var (
	// PossibleNaOfString 有可能出现的NaN字符串的全部选项
	PossibleNaOfString = []string{"NA", "NaN", "nan", "<nil>"}
)

Functions

func Abs

func Abs[T StatType](x []T) []T

Abs 泛型绝对值

func Align added in v0.6.3

func Align[T MoveType](x []T, a T, dLen int) []T

Align Data alignment

func AnyToFloat32 added in v0.6.4

func AnyToFloat32(v any) float32

func AnyToFloat64 added in v0.6.4

func AnyToFloat64(v any) float64

func AnyToInt64 added in v0.6.4

func AnyToInt64(v any) int64

AnyToInt64 any转换int64

func AnyToSlice added in v0.6.4

func AnyToSlice[T StatType](A any, n int) []T

AnyToSlice any转切片

func AnyToString added in v0.6.4

func AnyToString(v any) string

AnyToString any转string

func ArgMax added in v0.6.4

func ArgMax[T Number](v []T) int

ArgMax Returns the indices of the maximum values along an axis. 返回轴上最大值的索引

func ArgMin added in v0.6.4

func ArgMin[T Number](v []T) int

ArgMin Returns the indices of the minimum values along an axis. 返回轴上最小值的索引

func CumSum added in v0.6.4

func CumSum[T StatType](f []T) []T

CumSum 计算累和

func DTypeIsNaN added in v0.6.4

func DTypeIsNaN(d DType) bool

DTypeIsNaN 判断DType是否NaN

func Fill added in v0.6.4

func Fill[T StatType | ~string](v []T, d T, args ...any) (rows []T)

Fill 填充

Fill NA/NaN values using the specified method.
Parameters
----------
value : scalar, dict, Series, or DataFrame
   Value to use to fill holes (e.g. 0), alternately a
   dict/Series/DataFrame of values specifying which value to use for
   each index (for a Series) or column (for a DataFrame).  Values not
   in the dict/Series/DataFrame will not be filled. This value cannot
   be a list.
method : {{'backfill', 'bfill', 'pad', 'ffill', None}}, default None
   Method to use for filling holes in reindexed Series
   pad / ffill: propagate last valid observation forward to next valid
   backfill / bfill: use next valid observation to fill gap.
axis : {axes_single_arg}
   Axis along which to fill missing values. For `Series`
   this parameter is unused and defaults to 0.
inplace : bool, default False [√]
   If True, fill in-place. Note: this will modify any
   other views on this object (e.g., a no-copy slice for a column in a
   DataFrame).
limit : int, default None
   If method is specified, this is the maximum number of consecutive
   NaN values to forward/backward fill. In other words, if there is
   a gap with more than this number of consecutive NaNs, it will only
   be partially filled. If method is not specified, this is the
   maximum number of entries along the entire axis where NaNs will be
   filled. Must be greater than 0 if not None.
downcast : dict, default is None
   A dict of item->dtype of what to downcast if possible,
   or the string 'infer' which will try to downcast to an appropriate
   equal type (e.g. float64 to int64 if possible).

Returns
-------
[]T or None

func FillNa added in v0.6.4

func FillNa[T StatType | ~string](v []T, args ...any) []T

FillNa NaN填充默认值

func Float32IsNaN added in v0.6.4

func Float32IsNaN(f float32) bool

Float32IsNaN 判断float32是否NaN

func Float64IsNaN added in v0.6.4

func Float64IsNaN(f float64) bool

Float64IsNaN 判断float64是否NaN

func IsEmpty added in v0.6.4

func IsEmpty(s string) bool

IsEmpty Code to test if string is empty

func LinearRegression

func LinearRegression(x, y, weights []float64, origin bool) (alpha, beta float64)

LinearRegression 线性回归

func Max added in v0.6.3

func Max[T Float](f []T) T

Max 计算最大值

func Maximum added in v0.6.4

func Maximum[T Float](f1, f2 []T) []T

Maximum AVX2版本, 两个序列横向比较最大值 TODO:print(np.maximum(1.4, np.nan)) 输出nan

func Maximum_GO added in v0.6.4

func Maximum_GO[T Float](f1, f2 []T) []T

Maximum_GO go版本, 两个序列横向比较最大值

func Mean added in v0.6.4

func Mean[T StatType](x []T) T

Mean 求均值

func Min added in v0.6.4

func Min[T Float](f []T) T

Min 计算最小值

func Minimum added in v0.6.4

func Minimum[T Float](f1, f2 []T) []T

Minimum AVX2版本, 两个序列横向比较最大值

func Minimum_GO added in v0.6.4

func Minimum_GO[T Float](f1, f2 []T) []T

Minimum_GO go版本 两个序列横向比较最大值

func ParseFloat32 added in v0.6.4

func ParseFloat32(s string, v any) float32

ParseFloat32 字符串转float32

func ParseFloat64 added in v0.6.4

func ParseFloat64(s string, v any) float64

ParseFloat64 字符串转float64 任意组合的nan字符串都会被解析成NaN

func ParseInt64 added in v0.6.4

func ParseInt64(s string, v any) int64

ParseInt64 解析int字符串, 尝试解析10进制和16进制

func Repeat added in v0.6.3

func Repeat[T StatType](f T, n int) []T

Repeat repeat

func Reverse added in v0.6.4

func Reverse[S ~[]E, E any](s S) S

Reverse 反转切片

func Sequence added in v0.6.4

func Sequence[T StatType](n int) []T

Sequence 产生从0到n-1的数组

func SliceToFloat32 added in v0.6.4

func SliceToFloat32(v any) []float32

SliceToFloat32 any输入只能是一维slice或者数组

func SliceToFloat64 added in v0.6.4

func SliceToFloat64(v any) []float64

SliceToFloat64 any输入只能是一维slice或者数组

func Sqrt added in v0.6.4

func Sqrt[T StatType](v []T) []T

Sqrt 求平方根

func Std added in v0.6.4

func Std[T Float](f []T) T

Std 计算标准差

func Std_TODO added in v0.6.4

func Std_TODO[T Float](f []T) T

Std_TODO StdDev 这个版本有bug, gonum计算的std不对 TODO: 于总来解决

func StringIsNaN added in v0.6.4

func StringIsNaN(s string) bool

StringIsNaN 判断字符串是否NaN

func Sub added in v0.6.4

func Sub[T StatType](x []T, y any) []T

Sub 减法

func Sum added in v0.6.4

func Sum[T StatType](f []T) T

Sum 求和

func Where

func Where[T StatType](condition []T, x, y []T) []T

Where 返回根据“条件”从“x”或“y”中选择的元素 这里先实现一个简单的, 留给于总重构 params只支持两个默认值x和y, 如果condition为true返回x, 否则返回y condition和param都可能是基础数据类型,也可能是一个slice, 并且长度可能不一致 直接写成序列版本, 可能更简单

func Zeros added in v0.6.4

func Zeros()

Zeros Return a new array of given shape and type, filled with zeros.

Types

type Array added in v0.6.4

type Array[T Number] []T

func (Array[T]) Len added in v0.6.4

func (a Array[T]) Len() int

type BigFloat added in v0.6.4

type BigFloat = big.Float // 预留将来可能扩展float

type DType added in v0.6.4

type DType = float64

func Any2DType added in v0.6.4

func Any2DType(v any) DType

Any2DType any转DType

func Median added in v0.6.4

func Median[T StatType](values []T) DType

Median returns median value of series. Linear interpolation is used for odd length. TODO:未加验证, 未加速

func PolyFit added in v0.6.4

func PolyFit(x, y []DType, deg int, args ...any) []DType

PolyFit Least squares polynomial fit.

.. note::
	This forms part of the old polynomial API. Since version 1.4, the
	new polynomial API defined in `numpy.polynomial` is preferred.
	A summary of the differences can be found in the
	:doc:`transition guide </reference/routines.polynomials>`.

Fit a polynomial ``p(x) = p[0] * x**deg + ... + p[deg]`` of degree `deg`
to points `(x, y)`. Returns a vector of coefficients `p` that minimises
the squared error in the order `deg`, `deg-1`, ... `0`.

The `Polynomial.fit <numpy.polynomial.polynomial.Polynomial.fit>` class
method is recommended for new code as it is more stable numerically. See
the documentation of the method for more information.

Parameters
----------
x : array_like, shape (M,)
	x-coordinates of the M sample points ``(x[i], y[i])``.
y : array_like, shape (M,) or (M, K)
	y-coordinates of the sample points. Several data sets of sample
	points sharing the same x-coordinates can be fitted at once by
	passing in a 2D-array that contains one dataset per column.
deg : int
	Degree of the fitting polynomial

Returns
-------
p : ndarray, shape (deg + 1,) or (deg + 1, K)
	Polynomial coefficients, highest power first.  If `y` was 2-D, the
	coefficients for `k`-th data set are in ``p[:,k]``.

residuals, rank, singular_values, rcond
	These values are only returned if ``full == True``

	- residuals -- sum of squared residuals of the least squares fit
	- rank -- the effective rank of the scaled Vandermonde
		coefficient matrix
	- singular_values -- singular values of the scaled Vandermonde
		coefficient matrix
	- rcond -- value of `rcond`.

	For more details, see `numpy.linalg.lstsq`.

Warns
-----
RankWarning
	The rank of the coefficient matrix in the least-squares fit is
	deficient. The warning is only raised if ``full == False``.

	The warnings can be turned off by

	>>> import warnings
	>>> warnings.simplefilter('ignore', np.RankWarning)

See Also
--------
polyval : Compute polynomial values.
linalg.lstsq : Computes a least-squares fit.
scipy.interpolate.UnivariateSpline : Computes spline fits.

Notes
-----
The solution minimizes the squared error

.. math::
	E = \\sum_{j=0}^k |p(x_j) - y_j|^2

in the equations::

	x[0]**n * p[0] + ... + x[0] * p[n-1] + p[n] = y[0]
	x[1]**n * p[0] + ... + x[1] * p[n-1] + p[n] = y[1]
	...
	x[k]**n * p[0] + ... + x[k] * p[n-1] + p[n] = y[k]

The coefficient matrix of the coefficients `p` is a Vandermonde matrix.

`polyfit` issues a `RankWarning` when the least-squares fit is badly
conditioned. This implies that the best fit is not well-defined due
to numerical error. The results may be improved by lowering the polynomial
degree or by replacing `x` by `x` - `x`.mean(). The `rcond` parameter
can also be set to a value smaller than its default, but the resulting
fit may be spurious: including contributions from the small singular
values can add numerical noise to the result.

Note that fitting polynomial coefficients is inherently badly conditioned
when the degree of the polynomial is large or the interval of sample points
is badly centered. The quality of the fit should always be checked in these
cases. When polynomial fits are not satisfactory, splines may be a good
alternative.

References
----------
.. [1] Wikipedia, "Curve fitting",
		https://en.wikipedia.org/wiki/Curve_fitting
.. [2] Wikipedia, "Polynomial interpolation",
		https://en.wikipedia.org/wiki/Polynomial_interpolation
.. [3] numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)
		https://numpy.org/doc/stable/reference/generated/numpy.polyfit.html

Examples
--------
>>> import warnings
>>> x = np.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
>>> y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
>>> z = np.polyfit(x, y, 3)
>>> z
array([ 0.08703704, -0.81349206,  1.69312169, -0.03968254]) # may vary

func PolyVal added in v0.6.4

func PolyVal(p, x []DType) []DType

PolyVal

Evaluate a polynomial at specific values.

.. note::
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in `numpy.polynomial` is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.

If `p` is of length N, this function returns the value:

``p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]``

If `x` is a sequence, then ``p(x)`` is returned for each element of ``x``.
If `x` is another polynomial then the composite polynomial ``p(x(t))``
is returned.

Parameters

----------

p : array_like or poly1d object
	1D array of polynomial coefficients (including coefficients equal
	to zero) from highest degree to the constant term, or an
	instance of poly1d.
x : array_like or poly1d object
	A number, an array of numbers, or an instance of poly1d, at
	which to evaluate `p`.

Returns

-------

values : ndarray or poly1d
	If `x` is a poly1d instance, the result is the composition of the two
	polynomials, i.e., `x` is "substituted" in `p` and the simplified
	result is returned. In addition, the type of `x` - array_like or
	poly1d - governs the type of the output: `x` array_like => `values`
	array_like, `x` a poly1d object => `values` is also.

See Also
--------
poly1d: A polynomial class.

Notes
-----
Horner's scheme [1]_ is used to evaluate the polynomial. Even so,
for polynomials of high degree the values may be inaccurate due to
rounding errors. Use carefully.

If `x` is a subtype of `ndarray` the return value will be of the same type.

References
----------
.. [1] I. N. Bronshtein, K. A. Semendyayev, and K. A. Hirsch (Eng.
trans. Ed.), *Handbook of Mathematics*, New York, Van Nostrand
Reinhold Co., 1985, pg. 720.

Examples
--------
>>> np.polyval([3,0,1], 5)  # 3 * 5**2 + 0 * 5**1 + 1
76
>>> np.polyval([3,0,1], np.poly1d(5))
poly1d([76])
>>> np.polyval(np.poly1d([3,0,1]), 5)
76
>>> np.polyval(np.poly1d([3,0,1]), np.poly1d(5))
poly1d([76])

func Slice2DType added in v0.6.4

func Slice2DType(v any) []DType

Slice2DType 切片转DType

type Float added in v0.6.3

type Float interface {
	~float32 | ~float64
}

type Frame added in v0.6.4

type Frame interface {
	Len() int
}

type GenericType added in v0.6.4

type GenericType interface {
	~bool | ~int32 | ~int64 | ~int | ~float32 | ~float64 | ~string
}

GenericType Series支持的所有类型

type Int added in v0.6.4

type Int = int32

func DType2Int added in v0.6.4

func DType2Int(d []DType) []Int

DType切片转int32切片

type Integer added in v0.6.4

type Integer interface {
	Number8 | Number16 | Number32 | Number64
}

type MoveType added in v0.6.3

type MoveType interface {
	StatType | ~bool | ~string
}

type NDArray added in v0.6.4

type NDArray []DType

type Number added in v0.6.4

type Number interface {
	Integer | Float
}

Number int和uint的长度取决于CPU是多少位

type Number16 added in v0.6.4

type Number16 interface {
	~int16 | ~uint16
}

type Number32 added in v0.6.4

type Number32 interface {
	~int32 | ~uint32 | float32
}

type Number64 added in v0.6.4

type Number64 interface {
	~int64 | ~uint64 | float64 | int | uint
}

type Number8 added in v0.6.4

type Number8 interface {
	~int8 | ~uint8
}

type StatType

type StatType interface {
	~int32 | ~int64 | ~float32 | ~float64
}

Jump to

Keyboard shortcuts

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