当前位置: 首页>>代码示例>>Golang>>正文


Golang sort.Float64s函数代码示例

本文整理汇总了Golang中sort.Float64s函数的典型用法代码示例。如果您正苦于以下问题:Golang Float64s函数的具体用法?Golang Float64s怎么用?Golang Float64s使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Float64s函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: makeSelection

// cheesy tournament selection - we consider everyone to be in the tournament.
// Alternatively we could select a random number of genomes from the population
// and select the fittest among those.
func (pop *Population) makeSelection() {
	scores := make([]float64, 0)
	for i := 0; i < len(pop.genomes); i++ {
		scores = append(scores, pop.genomes[i].score)
	}
	sort.Float64s(scores)

	// choose the top 50% of scores
	topScores := make([]float64, 0)
	topN := int(float64(len(scores)) * 0.5)
	for i := len(scores) - 1; i >= len(scores)-topN; i-- {
		topScores = append(topScores, scores[i])
	}
	sort.Float64s(topScores)
	min := topScores[0]
	max := topScores[len(topScores)-1]

	// build a new list of genomes that are in topScores
	selectedGenomes := make([]*Genome, 0)
	fmt.Printf("min, max scores: %f, %f\n", min, max)
	for i := 0; i < len(pop.genomes); i++ {
		genome := pop.genomes[i]
		if genome.score >= min && genome.score <= max {
			selectedGenomes = append(selectedGenomes, genome)
		}
	}

	pop.genomes = selectedGenomes
}
开发者ID:snyderep,项目名称:genreco,代码行数:32,代码来源:gene.go

示例2: GridScale

// GridScale determines what scale should be used on the graph, for
// both the X and Y axes
func (n *New) GridScale(column string, gridCount float64) float64 {
	xsort := make([]float64, len(n.Xvalues))
	ysort := make([]float64, len(n.Yvalues))
	copy(xsort, n.Xvalues)
	copy(ysort, n.Yvalues)
	sort.Float64s(xsort)
	sort.Float64s(ysort)
	// Courtesy: Stack Overflow
	microAdjust := func(span, grids float64) float64 {
		width := (span / (grids - 1))
		x := math.Ceil(math.Log10(width) - 1)
		p := math.Pow(10, x)
		rspace := math.Ceil(width/p) * p
		return rspace
	}

	switch column {
	case "X":
		rangeX := n.DataRange("X")
		return microAdjust(rangeX, gridCount)
	case "Y":
		rangeY := n.DataRange("Y")
		return microAdjust(rangeY, gridCount)
	default:
		return 0
	}
}
开发者ID:enodev0,项目名称:rat,代码行数:29,代码来源:reg.go

示例3: TestRunIncomes

func TestRunIncomes(t *testing.T) {
	JsonObj := []byte(`{"Age":22, "Retirement_age":65, "Terminal_age":90, "Effective_tax_rate":0.3, "Returns_tax_rate":0.3, "N": 20000, 
						"Non_Taxable_contribution":17500, "Taxable_contribution": 0, "Non_Taxable_balance":0, "Taxable_balance": 0, 
						"Yearly_social_security_income":0, "Asset_volatility": 0.15, "Expected_rate_of_return": 0.07, "Inflation_rate":0.035}`)
	rc, err := NewRetCalcFromJSON(JsonObj)
	if err != nil {
		t.Error(err)
	}
	runIncomes := rc.RunIncomes()
	sort.Float64s(runIncomes)
	incomePerRun := make([]float64, rc.N, rc.N)
	for i := range incomePerRun {
		incomePerRun[i] = rc.IncomeOnPath(i)
	}
	sort.Float64s(incomePerRun)
	incomesOk := true
	for i := range incomePerRun {
		if incomePerRun[i] != runIncomes[i] {
			incomesOk = false
			fmt.Printf("RunIncomes: %f, IncomeOnPath: %f\n", runIncomes[i], incomePerRun[i])
		}
	}
	if !incomesOk {
		t.Errorf("Incomes do not calculate correctly for RunIncomes()")
	}
	if !sort.Float64sAreSorted(runIncomes) {
		t.Errorf("Incomes from RetCalc.RunIncomes() should be sorted on return")
	}
}
开发者ID:jd1123,项目名称:retirement_calculator-go,代码行数:29,代码来源:rc_test.go

示例4: MedianDist

// MedianDist computes the median (med) and the median absolute deviation (mad)
// of the spherical distances between point q and points p₀,p₁,...
// on the unit sphere S². This function panics if len(p) = 0.
func MedianDist(q Geo, p []Geo) (med, mad float64) {
	n := len(p)
	if n == 0 {
		panic("MedianDist: len(p) = 0")
	}

	m := n / 2

	d := make([]float64, n)
	for i, t := range p {
		d[i] = q.Dist(t)
	}
	sort.Float64s(d)
	if !odd(n) {
		a, b := d[m-1], d[m]
		med = a + 0.5*(b-a)
	} else {
		med = d[m]
	}

	for i := range d {
		d[i] = math.Abs(med - d[i])
	}
	sort.Float64s(d)
	if !odd(n) {
		a, b := d[m-1], d[m]
		mad = a + 0.5*(b-a)
	} else {
		mad = d[m]
	}

	return
}
开发者ID:reconditematter,项目名称:stats2,代码行数:36,代码来源:mediandist.go

示例5: DataRange

// DataRange determines the range of both the
// predictor and response variables
func (n *New) DataRange(column string) float64 {
	// TODO: some cleanup
	xval := make([]float64, len(n.Xvalues))
	copy(xval, n.Xvalues)
	yval := make([]float64, len(n.Yvalues))
	copy(yval, n.Yvalues)
	switch column {
	case "X":
		sort.Float64s(xval)
		upperX := (xval[len(xval)-1])
		lowerX := (xval[0])
		if upperX == lowerX { // integrate with error handler
			panic("Range calculates to zero on X")
		}
		return (upperX - lowerX)
	case "Y":
		sort.Float64s(yval)
		upperY := (yval[len(yval)-1])
		lowerY := (yval[0])
		if upperY == lowerY {
			panic("Range calculates to zero on Y")
		}
		return (upperY - lowerY)
	default:
		return -1
	}
}
开发者ID:enodev0,项目名称:rat,代码行数:29,代码来源:reg.go

示例6: isResourceUsageSimilarEnough

// A simple comparison checking if minimum and maximums in both datasets are within allowedVariance
// If this function changes, PrintToStdout should be updated accordingly.
func isResourceUsageSimilarEnough(left, right percentileUsageData, allowedVariance float64) bool {
	if len(left.cpuData) == 0 || len(left.memData) == 0 || len(right.cpuData) == 0 || len(right.memData) == 0 {
		glog.V(4).Infof("Length of at least one data vector is zero. Returning false for the lack of data.")
		return false
	}

	sort.Float64s(left.cpuData)
	sort.Float64s(right.cpuData)
	sort.Sort(int64arr(left.memData))
	sort.Sort(int64arr(right.memData))

	leftCPUMin := math.Max(left.cpuData[0], minCPU)
	leftCPUMax := math.Max(left.cpuData[len(left.cpuData)-1], minCPU)
	leftMemMin := max(left.memData[0], minMem)
	leftMemMax := max(left.memData[len(left.memData)-1], minMem)
	rightCPUMin := math.Max(right.cpuData[0], minCPU)
	rightCPUMax := math.Max(right.cpuData[len(right.cpuData)-1], minCPU)
	rightMemMin := max(right.memData[0], minMem)
	rightMemMax := max(right.memData[len(right.memData)-1], minMem)

	return leq(leftCPUMin, allowedVariance*rightCPUMin) &&
		leq(rightCPUMin, allowedVariance*leftCPUMin) &&
		leq(leftCPUMax, allowedVariance*rightCPUMax) &&
		leq(rightCPUMax, allowedVariance*leftCPUMax) &&
		leq(float64(leftMemMin), allowedVariance*float64(rightMemMin)) &&
		leq(float64(rightMemMin), allowedVariance*float64(leftMemMin)) &&
		leq(float64(leftMemMax), allowedVariance*float64(rightMemMax)) &&
		leq(float64(rightMemMax), allowedVariance*float64(leftMemMax))
}
开发者ID:jojimt,项目名称:contrib,代码行数:31,代码来源:compare-resources.go

示例7: histoAnalyze

//HistoAnalyze calls a Scanner to get items and convert items to numbers and do analysis to get Top N and Histogram of those numbers
// it returns an error if a item read in cannot be converted to a float64 number or the scanner encountered an error
// When there is no error detected, it returns
// 		1)number count read in
// 		2) top N numbers as a slice
// 		3) histogram data as a map
//		4) nil (no error)
func histoAnalyze(s Scanner, N int, binWidth float64) (int64, []float64, map[float64]int64, error) {

	//init return parameters
	count := int64(0)
	topN := []float64{}
	histo := map[float64]int64{}
	err := error(nil)

	//check pass in parameter
	if N < 1 || binWidth < 0.0 {
		return -1, nil, nil, fmt.Errorf("HistoAnalyze: negitive N or bin width - %d,%f", N, binWidth)
	}

	for s.Scan() {
		count++
		// fmt.Printf("\n%s", s.Text())

		// func ParseFloat(s string, bitSize int) (f float64, err error)
		num, err := strconv.ParseFloat(s.Text(), 64)
		if err != nil {
			return -1, nil, nil, err
		}

		//calculate the lower boundary
		lowerB := float64(math.Floor(num/binWidth)) * binWidth

		// fmt.Printf("\nlower Boundery %f, number %f, d.binWidth %f, int64(num/d.binWidth) %d ", lowerB, num, d.binWidth, int64(num/d.binWidth))

		// increase frquency of the bin
		histo[lowerB]++

		//Now handling Top N
		switch {
		//append the first TOP_N items when the TopN slice is not long enough
		case len(topN) < N:
			topN = append(topN, num)
			sort.Float64s(topN)
		//the new number is greater than one of the existing TopN
		case num > topN[0]:
			// if new number is bigger than the first one, which is the min value in the TopN as TopN slice is sorted
			// replace the first value
			topN[0] = num
			sort.Float64s(topN)
		}

	}
	//check if the scanner has encountered any error
	if err = s.Err(); err != nil {
		return -1, nil, nil, err
	}

	// fmt.Printf("\n\nNOT SORTED%#v", histo)
	return count, topN, histo, err
}
开发者ID:jusongchen,项目名称:stat,代码行数:61,代码来源:histogram.go

示例8: Split

func (node *Node) Split(depth int) {
	if len(node.shapes) < 8 {
		return
	}
	xs := make([]float64, 0, len(node.shapes)*2)
	ys := make([]float64, 0, len(node.shapes)*2)
	zs := make([]float64, 0, len(node.shapes)*2)
	for _, shape := range node.shapes {
		box := shape.Box()
		xs = append(xs, box.Min.X)
		xs = append(xs, box.Max.X)
		ys = append(ys, box.Min.Y)
		ys = append(ys, box.Max.Y)
		zs = append(zs, box.Min.Z)
		zs = append(zs, box.Max.Z)
	}
	sort.Float64s(xs)
	sort.Float64s(ys)
	sort.Float64s(zs)
	mx, my, mz := Median(xs), Median(ys), Median(zs)
	best := int(float64(len(node.shapes)) * 0.85)
	bestAxis := AxisNone
	bestPoint := 0.0
	sx := node.PartitionScore(AxisX, mx)
	if sx < best {
		best = sx
		bestAxis = AxisX
		bestPoint = mx
	}
	sy := node.PartitionScore(AxisY, my)
	if sy < best {
		best = sy
		bestAxis = AxisY
		bestPoint = my
	}
	sz := node.PartitionScore(AxisZ, mz)
	if sz < best {
		best = sz
		bestAxis = AxisZ
		bestPoint = mz
	}
	if bestAxis == AxisNone {
		return
	}
	l, r := node.Partition(best, bestAxis, bestPoint)
	node.axis = bestAxis
	node.point = bestPoint
	node.left = NewNode(l)
	node.right = NewNode(r)
	node.left.Split(depth + 1)
	node.right.Split(depth + 1)
	node.shapes = nil // only needed at leaf nodes
}
开发者ID:kirillrdy,项目名称:pt,代码行数:53,代码来源:tree.go

示例9: KS

// KS performs a Kolmogorov-Smirnov test for the two datasets, and returns the
// p-value for the null hypothesis that the two sets come from the same distribution.
func KS(data1, data2 []float64) float64 {

	sort.Float64s(data1)
	sort.Float64s(data2)

	for math.IsNaN(data1[0]) {
		data1 = data1[1:]
	}

	for math.IsNaN(data2[0]) {
		data2 = data2[1:]
	}

	n1, n2 := len(data1), len(data2)
	en1, en2 := float64(n1), float64(n2)

	var d float64
	var fn1, fn2 float64

	j1, j2 := 0, 0
	for j1 < n1 && j2 < n2 {
		d1 := data1[j1]
		d2 := data2[j2]

		if d1 <= d2 {
			for j1 < n1 && d1 == data1[j1] {
				j1++
				fn1 = float64(j1) / en1
			}
		}

		if d2 <= d1 {
			for j2 < n2 && d2 == data2[j2] {
				j2++
				fn2 = float64(j2) / en2
			}
		}

		if dt := math.Abs(fn2 - fn1); dt > d {
			d = dt
		}

	}
	en := math.Sqrt((en1 * en2) / (en1 + en2))
	// R and Octave don't use this approximation that NR does
	//return qks((en + 0.12 + 0.11/en) * d)
	return qks(en * d)
}
开发者ID:nnuss,项目名称:go-onlinestats,代码行数:50,代码来源:kstest.go

示例10: GraphInit

// GraphInit initializes and returns a Graphdata
// TODO: massive cleanups required
func (n *New) GraphInit(gridcountX, gridcountY int) Graphdata {
	a := make([]float64, len(n.Xvalues))
	b := make([]float64, len(n.Yvalues))
	copy(a, n.Xvalues)
	copy(b, n.Yvalues)
	sort.Float64s(a)
	sort.Float64s(b) // naive
	leastX := a[0]
	leastY := b[0]
	var xs []float64
	var ys []float64     // scaled grid marks
	xs = append(xs, 0.0) // origin
	ys = append(ys, 0.0)
	m, p, _ := Translate(0, 0)
	// m, p := 0.0, 0.0
	x := n.GridScale("X", float64(gridcountX))
	y := n.GridScale("Y", float64(gridcountY))
	for i := 1; i <= gridcountX; i++ {
		if i == 0 {
			xs = append(xs, (leastX - x))
		} else if i == 1 {
			xs = append(xs, (leastX))
		} else {
			xs = append(xs, (leastX + (float64(i) * x)))
		}
	}
	for i := 1; i <= gridcountY; i++ {
		if i == 0 {
			ys = append(ys, (leastY - y))
		} else if i == 1 {
			ys = append(ys, (leastY))
		} else {
			ys = append(ys, (leastY + (float64(i) * y)))
		}
	}
	var xg []float64
	for i := 1; i <= gridcountX+1; i++ { // +1 to account for origin
		xg = append(xg, m)
		m = m + 20 //x coordinate
	}
	var yg []float64 // actual translated coordinates
	for i := 1; i <= gridcountY+1; i++ {
		yg = append(yg, p)
		p = p - 20 //y coordinate
	}
	g := Graphdata{xs, ys, xg, yg}
	return g
}
开发者ID:enodev0,项目名称:rat,代码行数:50,代码来源:reg.go

示例11: StepFunction

func StepFunction(data map[float64][]float64) func(float64) []float64 {
	sorted := make([]float64, len(data))
	i := 0
	for key, _ := range data {
		sorted[i] = key
		i++
	}

	sort.Float64s(sorted)

	return func(x float64) []float64 {
		curPoint := 0
		for ; curPoint < len(sorted) && sorted[curPoint] <= x; curPoint++ {
		}
		if curPoint >= len(sorted) {
			curPoint = len(sorted) - 1
		}

		if curPoint == 0 && sorted[curPoint] > x {
			fmt.Printf("values at x=%f are undefined, earliest point in step function defined at x=%f\n", x, sorted[curPoint])
			panic("undefined value")
		}

		return data[sorted[curPoint]]
	}
}
开发者ID:skiesel,项目名称:plot,代码行数:26,代码来源:errorpoints_spaced.go

示例12: StumpPool

func StumpPool(samples SampleList) boosting.Pool {
	dims := len(samples[0])
	res := make([]boosting.Classifier, 0, len(samples)*dims)
	for d := 0; d < dims; d++ {
		values := make([]float64, 0, len(samples))
		seenValues := map[float64]bool{}
		for _, s := range samples {
			val := s[d]
			if !seenValues[val] {
				seenValues[val] = true
				values = append(values, val)
			}
		}
		sort.Float64s(values)
		for i, val := range values {
			var t TreeStump
			if i == 0 {
				t = TreeStump{FieldIndex: d, Threshold: val - 1}
			} else {
				lastVal := values[i-1]
				average := (lastVal + val) / 2
				t = TreeStump{FieldIndex: d, Threshold: average}
			}
			res = append(res, t)
		}
	}
	return boosting.NewStaticPool(res, samples)
}
开发者ID:unixpickle,项目名称:weakai,代码行数:28,代码来源:stumps.go

示例13: createPrintableResult

func createPrintableResult(slice []*Result) *PrintableResult {
	if slice == nil || len(slice) == 0 {
		return nil
	}
	pr := PrintableResult{}
	pr.count = uint64(0)
	pr.totalT = float64(0)
	pr.times = []float64{}
	pr.totalSize = uint64(0)
	for _, result := range slice {
		pr.count++
		pr.totalT += float64(result.t)
		pr.times = append(pr.times, float64(result.t))
		pr.totalSize += uint64(result.size)
	}
	pr.avgSize = float64(pr.totalSize) / float64(pr.count)
	sort.Float64s(pr.times)
	pr.min = pr.times[0]
	pr.p25 = pr.times[int(float64(25.0/100.0)*float64(pr.count))]
	pr.p50 = pr.times[int(float64(50.0/100.0)*float64(pr.count))]
	pr.avg = pr.totalT / float64(pr.count)
	pr.p75 = pr.times[int(float64(75.0/100.0)*float64(pr.count))]
	pr.p80 = pr.times[int(float64(80.0/100.0)*float64(pr.count))]
	pr.p85 = pr.times[int(float64(85.0/100.0)*float64(pr.count))]
	pr.p90 = pr.times[int(float64(90.0/100.0)*float64(pr.count))]
	pr.p95 = pr.times[int(float64(95.0/100.0)*float64(pr.count))]
	pr.p99 = pr.times[int(float64(99.0/100.0)*float64(pr.count))]
	pr.max = pr.times[pr.count-1]
	return &pr
}
开发者ID:jigish,项目名称:smack,代码行数:30,代码来源:smack.go

示例14: Histogram

// Histogram numpy.histogram
func Histogram(series []float64, bins int) ([]int, []float64) {
	var binEdges []float64
	var hist []int
	l := len(series)
	if l == 0 {
		return hist, binEdges
	}
	sort.Float64s(series)
	w := (series[l-1] - series[0]) / float64(bins)
	for i := 0; i < bins; i++ {
		binEdges = append(binEdges, w*float64(i)+series[0])
		if binEdges[len(binEdges)-1] >= series[l-1] {
			break
		}
	}
	binEdges = append(binEdges, w*float64(bins)+series[0])
	bl := len(binEdges)
	hist = make([]int, bl-1)
	for i := 0; i < bl-1; i++ {
		for _, val := range series {
			if val >= binEdges[i] && val < binEdges[i+1] {
				hist[i] += 1
				continue
			}
			if i == (bl-2) && val >= binEdges[i] && val <= binEdges[i+1] {
				hist[i] += 1
			}
		}
	}
	return hist, binEdges
}
开发者ID:postfix,项目名称:skyline,代码行数:32,代码来源:statistic.go

示例15: TestQuantRandQuery

func TestQuantRandQuery(t *testing.T) {
	s := NewTargeted(0.5, 0.90, 0.99)
	a := make([]float64, 0, 1e5)
	rand.Seed(42)
	for i := 0; i < cap(a); i++ {
		v := float64(rand.Int63())
		s.Insert(v)
		a = append(a, v)
	}
	t.Logf("len: %d", s.Count())
	sort.Float64s(a)
	w := getPerc(a, 0.50)
	if g := s.Query(0.50); math.Abs(w-g)/w > 0.03 {
		t.Errorf("perc50: want %v, got %v", w, g)
		t.Logf("e: %f", math.Abs(w-g)/w)
	}
	w = getPerc(a, 0.90)
	if g := s.Query(0.90); math.Abs(w-g)/w > 0.03 {
		t.Errorf("perc90: want %v, got %v", w, g)
		t.Logf("e: %f", math.Abs(w-g)/w)
	}
	w = getPerc(a, 0.99)
	if g := s.Query(0.99); math.Abs(w-g)/w > 0.03 {
		t.Errorf("perc99: want %v, got %v", w, g)
		t.Logf("e: %f", math.Abs(w-g)/w)
	}
}
开发者ID:Greentor,项目名称:roshi,代码行数:27,代码来源:stream_test.go


注:本文中的sort.Float64s函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。