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


Golang DenseMatrix.AppendRow方法代码示例

本文整理汇总了Golang中github.com/bobhancock/gomatrix/matrix.DenseMatrix.AppendRow方法的典型用法代码示例。如果您正苦于以下问题:Golang DenseMatrix.AppendRow方法的具体用法?Golang DenseMatrix.AppendRow怎么用?Golang DenseMatrix.AppendRow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/bobhancock/gomatrix/matrix.DenseMatrix的用法示例。


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

示例1: Xmeans

// Xmeans runs k-means for k lower bound to k upper bound on a data set.
// Once the k centroids have converged each cluster is bisected and the BIC
// of the orginal cluster (parent = a model with one centroid) to the
// the bisected model which consists of two centroids and whichever is greater
// is committed to the set of clusters for this larger model k.
//
func Xmeans(datapoints, centroids *matrix.DenseMatrix, kmax int, cc, bisectcc CentroidChooser, measurer VectorMeasurer) ([]Model, map[string]error) {
	logname := "/var/tmp/xmeans.log"
	fp, err := os.OpenFile(logname, os.O_RDWR|os.O_APPEND, 0666)
	if err != nil {
		if os.IsNotExist(err) {
			fp, err = os.Create(logname)
			if err != nil {
				fmt.Printf("Xmeans: cannot open %s for logging.\n", logname)
			}
		}
	}

	log.SetOutput(io.Writer(fp))

	k, _ := centroids.GetSize()
	log.Printf("Start k=%d kmax=%d\n", k, kmax)

	R, M := datapoints.GetSize()
	errs := make(map[string]error)
	runtime.GOMAXPROCS(numworkers)
	models := make([]Model, 0)

	for k <= kmax {
		log.Printf("kmeans started k=%d\n", k)
		model, err := kmeans(datapoints, centroids, measurer)
		if err != nil {
			errs[strconv.Itoa(k)] = err
		}

		// Bisect the returned clusters
		log.Println("bisect started")
		bimodel := bisect(model.Clusters, R, M, bisectcc, measurer)
		numCentroids := len(bimodel.Clusters)
		log.Printf("bisect returned %d clusters\n", numCentroids)
		models = append(models, model)

		var cent *matrix.DenseMatrix

		if numCentroids <= kmax {
			for rowexists := true; rowexists == true; {
				cent = cc.ChooseCentroids(datapoints, 1)
				rowexists = centroids.RowExists(cent)
			}

			centroids, err = centroids.AppendRow(cent)
			if err != nil {
				log.Printf("AppendRow: %v\n", err)
				errs["ApppendRow"] = err
				break
			}
			k++
		} else {
			k = numCentroids
		}
	}

	log.Println("Finished")
	return models, errs
}
开发者ID:neevor,项目名称:goxmeans,代码行数:65,代码来源:km.go

示例2: Xmeans

// Xmeans runs k-means for k lower bound to k upper bound on a data set.
// Once the k centroids have converged each cluster is bisected and the BIC
// of the orginal cluster (parent = a model with one centroid) to the
// the bisected model which consists of two centroids and whichever is greater
// is committed to the set of clusters for this larger model k.
//
func Xmeans(datapoints, centroids *matrix.DenseMatrix, k, kmax int, cc, bisectcc CentroidChooser, measurer VectorMeasurer) ([]Model, map[string]error) {
	var err error

	// Uncomment logging code as well as the import statement above if you want simple logging to the elapsed
	// time between major events.
	/*	logname := "/var/tmp/xmeans.log"
			fp, err :=  os.OpenFile(logname, os.O_RDWR|os.O_APPEND, 0666)
			if err != nil {
				if os.IsNotExist(err) {
					fp, err = os.Create(logname)
		    		if err != nil {
			    		fmt.Printf("Xmeans: cannot open %s for logging.\n", logname)
					}
				}
			}

			log.SetOutput(io.Writer(fp))
	*/
	if k > kmax {
		m := make([]Model, 0)
		e := map[string]error{
			"k": errors.New(fmt.Sprintf("k must be <= kmax.  Received k=%d and kmax=%d.", k, kmax)),
		}
		return m, e
	}

	//	log.Printf("Start k=%d kmax=%d\n", k, kmax)

	R, M := datapoints.GetSize()
	errs := make(map[string]error)
	runtime.GOMAXPROCS(numworkers)
	models := make([]Model, 0)

	for k <= kmax {
		//		log.Printf("kmeans started k=%d\n", k)
		model := kmeans(datapoints, centroids, measurer)

		// Bisect the returned clusters
		//		log.Println("bisect started")
		bimodel := bisect(model.Clusters, R, M, bisectcc, measurer)
		numCentroids := len(bimodel.Clusters)
		//		log.Printf("bisect returned %d clusters\n", numCentroids)
		models = append(models, model)

		var cent *matrix.DenseMatrix

		if numCentroids <= kmax {
			for rowexists := true; rowexists == true; {
				cent = cc.ChooseCentroids(datapoints, 1)
				rowexists = centroids.RowExists(cent)
			}

			centroids, err = centroids.AppendRow(cent)
			if err != nil {
				errs["ApppendRow"] = err
				break
			}
			k++
		} else {
			k = numCentroids
		}
	}

	//	log.Println("Finished")
	return models, errs
}
开发者ID:postfix,项目名称:goxmeans,代码行数:72,代码来源:km.go


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