本文整理汇总了Golang中github.com/sjwhitworth/golearn/base.ParseCSVToInstances函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseCSVToInstances函数的具体用法?Golang ParseCSVToInstances怎么用?Golang ParseCSVToInstances使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseCSVToInstances函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestLogisticRegression
func TestLogisticRegression(t *testing.T) {
Convey("Given labels, a classifier and data", t, func() {
// Load data
X, err := base.ParseCSVToInstances("train.csv", false)
So(err, ShouldEqual, nil)
Y, err := base.ParseCSVToInstances("test.csv", false)
So(err, ShouldEqual, nil)
// Setup the problem
lr := NewLogisticRegression("l2", 1.0, 1e-6)
lr.Fit(X)
Convey("When predicting the label of first vector", func() {
Z := lr.Predict(Y)
Convey("The result should be 1", func() {
So(Z.RowString(0), ShouldEqual, "1.00")
})
})
Convey("When predicting the label of second vector", func() {
Z := lr.Predict(Y)
Convey("The result should be -1", func() {
So(Z.RowString(1), ShouldEqual, "-1.00")
})
})
})
}
示例2: TestKnnClassifier
func TestKnnClassifier(t *testing.T) {
Convey("Given labels, a classifier and data", t, func() {
trainingData, err := base.ParseCSVToInstances("knn_train.csv", false)
So(err, ShouldBeNil)
testingData, err := base.ParseCSVToInstances("knn_test.csv", false)
So(err, ShouldBeNil)
cls := NewKnnClassifier("euclidean", 2)
cls.Fit(trainingData)
predictions := cls.Predict(testingData)
So(predictions, ShouldNotEqual, nil)
Convey("When predicting the label for our first vector", func() {
result := base.GetClass(predictions, 0)
Convey("The result should be 'blue", func() {
So(result, ShouldEqual, "blue")
})
})
Convey("When predicting the label for our second vector", func() {
result2 := base.GetClass(predictions, 1)
Convey("The result should be 'red", func() {
So(result2, ShouldEqual, "red")
})
})
})
}
示例3: TestKnnClassifier
func TestKnnClassifier(t *testing.T) {
Convey("Given labels, a classifier and data", t, func() {
trainingData, err1 := base.ParseCSVToInstances("knn_train.csv", false)
testingData, err2 := base.ParseCSVToInstances("knn_test.csv", false)
if err1 != nil {
t.Error(err1)
return
}
if err2 != nil {
t.Error(err2)
return
}
cls := NewKnnClassifier("euclidean", 2)
cls.Fit(trainingData)
predictions := cls.Predict(testingData)
Convey("When predicting the label for our first vector", func() {
result := predictions.GetClass(0)
Convey("The result should be 'blue", func() {
So(result, ShouldEqual, "blue")
})
})
Convey("When predicting the label for our first vector", func() {
result2 := predictions.GetClass(1)
Convey("The result should be 'red", func() {
So(result2, ShouldEqual, "red")
})
})
})
}
示例4: TestLinearRegression
func TestLinearRegression(t *testing.T) {
Convey("Doing a linear regression", t, func() {
lr := NewLinearRegression()
Convey("With no training data", func() {
Convey("Predicting", func() {
testData, err := base.ParseCSVToInstances("../examples/datasets/exams.csv", true)
So(err, ShouldBeNil)
_, err = lr.Predict(testData)
Convey("Should result in a NoTrainingDataError", func() {
So(err, ShouldEqual, NoTrainingDataError)
})
})
})
Convey("With not enough training data", func() {
trainingDatum, err := base.ParseCSVToInstances("../examples/datasets/exam.csv", true)
So(err, ShouldBeNil)
Convey("Fitting", func() {
err = lr.Fit(trainingDatum)
Convey("Should result in a NotEnoughDataError", func() {
So(err, ShouldEqual, NotEnoughDataError)
})
})
})
Convey("With sufficient training data", func() {
instances, err := base.ParseCSVToInstances("../examples/datasets/exams.csv", true)
So(err, ShouldBeNil)
trainData, testData := base.InstancesTrainTestSplit(instances, 0.1)
Convey("Fitting and Predicting", func() {
err := lr.Fit(trainData)
So(err, ShouldBeNil)
predictions, err := lr.Predict(testData)
So(err, ShouldBeNil)
Convey("It makes reasonable predictions", func() {
_, rows := predictions.Size()
for i := 0; i < rows; i++ {
actualValue, _ := strconv.ParseFloat(base.GetClass(testData, i), 64)
expectedValue, _ := strconv.ParseFloat(base.GetClass(predictions, i), 64)
So(actualValue, ShouldAlmostEqual, expectedValue, actualValue*0.05)
}
})
})
})
})
}
示例5: BenchmarkLinearRegressionOneRow
func BenchmarkLinearRegressionOneRow(b *testing.B) {
// Omits error handling in favor of brevity
trainData, _ := base.ParseCSVToInstances("../examples/datasets/exams.csv", true)
testData, _ := base.ParseCSVToInstances("../examples/datasets/exam.csv", true)
lr := NewLinearRegression()
lr.Fit(trainData)
b.ResetTimer()
for n := 0; n < b.N; n++ {
lr.Predict(testData)
}
}
示例6: TestBinning
func TestBinning(t *testing.T) {
Convey("Given some data and a reference", t, func() {
// Read the data
inst1, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
}
inst2, err := base.ParseCSVToInstances("../examples/datasets/iris_binned.csv", true)
if err != nil {
panic(err)
}
//
// Construct the binning filter
binAttr := inst1.AllAttributes()[0]
filt := NewBinningFilter(inst1, 10)
filt.AddAttribute(binAttr)
filt.Train()
inst1f := base.NewLazilyFilteredInstances(inst1, filt)
// Retrieve the categorical version of the original Attribute
var cAttr base.Attribute
for _, a := range inst1f.AllAttributes() {
if a.GetName() == binAttr.GetName() {
cAttr = a
}
}
cAttrSpec, err := inst1f.GetAttribute(cAttr)
So(err, ShouldEqual, nil)
binAttrSpec, err := inst2.GetAttribute(binAttr)
So(err, ShouldEqual, nil)
//
// Create the LazilyFilteredInstances
// and check the values
Convey("Discretized version should match reference", func() {
_, rows := inst1.Size()
for i := 0; i < rows; i++ {
val1 := inst1f.Get(cAttrSpec, i)
val2 := inst2.Get(binAttrSpec, i)
val1s := cAttr.GetStringFromSysVal(val1)
val2s := binAttr.GetStringFromSysVal(val2)
So(val1s, ShouldEqual, val2s)
}
})
})
}
示例7: TestChiMergeDiscretization
func TestChiMergeDiscretization(t *testing.T) {
Convey("Chi-Merge Discretization", t, func() {
chimDatasetPath := "../examples/datasets/chim.csv"
Convey(fmt.Sprintf("With the '%s' dataset", chimDatasetPath), func() {
instances, err := base.ParseCSVToInstances(chimDatasetPath, true)
So(err, ShouldBeNil)
_, rows := instances.Size()
frequencies := chiMerge(instances, instances.AllAttributes()[0], 0.9, 0, rows)
values := []float64{}
for _, entry := range frequencies {
values = append(values, entry.Value)
}
Convey("Computes frequencies correctly", func() {
So(values, ShouldResemble, []float64{1.3, 56.2, 87.1})
})
})
irisHeadersDatasetpath := "../examples/datasets/iris_headers.csv"
Convey(fmt.Sprintf("With the '%s' dataset", irisHeadersDatasetpath), func() {
instances, err := base.ParseCSVToInstances(irisHeadersDatasetpath, true)
So(err, ShouldBeNil)
Convey("Sorting the instances first", func() {
allAttributes := instances.AllAttributes()
sortedAttributesSpecs := base.ResolveAttributes(instances, allAttributes)[0:1]
sortedInstances, err := base.Sort(instances, base.Ascending, sortedAttributesSpecs)
So(err, ShouldBeNil)
_, rows := sortedInstances.Size()
frequencies := chiMerge(sortedInstances, sortedInstances.AllAttributes()[0], 0.9, 0, rows)
values := []float64{}
for _, entry := range frequencies {
values = append(values, entry.Value)
}
Convey("Computes frequencies correctly", func() {
So(values, ShouldResemble, []float64{4.3, 5.5, 5.8, 6.3, 7.1})
})
})
})
})
}
示例8: TestDBSCANDistanceQuery
func TestDBSCANDistanceQuery(t *testing.T) {
Convey("Should be able to determine which points are in range...", t, func() {
// Read in the synthetic test data
inst, err := base.ParseCSVToInstances("synthetic.csv", false)
So(err, ShouldBeNil)
// Create a neighbours vector
neighbours := big.NewInt(0)
// Compute pairwise distances
dist, err := computePairwiseDistances(inst, inst.AllAttributes(), pairwise.NewEuclidean())
So(dist.At(0, 0), ShouldAlmostEqual, 0)
So(dist.At(0, 1), ShouldAlmostEqual, 1)
So(dist.At(1, 0), ShouldAlmostEqual, 1)
So(dist.At(0, 2), ShouldAlmostEqual, math.Sqrt(5))
So(dist.At(2, 0), ShouldAlmostEqual, math.Sqrt(5))
So(err, ShouldBeNil)
// Do the region query
neighbours = regionQuery(0, neighbours, dist, 1)
So(neighbours.Bit(0), ShouldEqual, 1)
So(neighbours.Bit(1), ShouldEqual, 1)
So(neighbours.Bit(2), ShouldEqual, 0)
So(neighbours.Bit(3), ShouldEqual, 0)
So(neighbours.Bit(4), ShouldEqual, 0)
})
}
示例9: TestDBSCANSynthetic
func TestDBSCANSynthetic(t *testing.T) {
Convey("Synthetic DBSCAN test should work...", t, func() {
inst, err := base.ParseCSVToInstances("synthetic.csv", false)
So(err, ShouldBeNil)
p := DBSCANParameters{
ClusterParameters{
inst.AllAttributes(),
pairwise.NewEuclidean(),
},
1,
1,
}
m, err := DBSCAN(inst, p)
So(err, ShouldBeNil)
So(len(m), ShouldEqual, 2)
So(m[1], ShouldContain, 0)
So(m[1], ShouldContain, 1)
So(m[1], ShouldContain, 2)
So(m[1], ShouldContain, 3)
})
}
示例10: TestRandomForest1
func TestRandomForest1(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
}
rand.Seed(time.Now().UnixNano())
insts := base.InstancesTrainTestSplit(inst, 0.6)
filt := filters.NewChiMergeFilter(inst, 0.90)
filt.AddAllNumericAttributes()
filt.Build()
filt.Run(insts[1])
filt.Run(insts[0])
rf := new(BaggedModel)
for i := 0; i < 10; i++ {
rf.AddModel(trees.NewRandomTree(2))
}
rf.Fit(insts[0])
fmt.Println(rf)
predictions := rf.Predict(insts[1])
fmt.Println(predictions)
confusionMat := eval.GetConfusionMatrix(insts[1], predictions)
fmt.Println(confusionMat)
fmt.Println(eval.GetMacroPrecision(confusionMat))
fmt.Println(eval.GetMacroRecall(confusionMat))
fmt.Println(eval.GetSummary(confusionMat))
}
示例11: TestChiMergeFilter
func TestChiMergeFilter(t *testing.T) {
Convey("Chi-Merge Filter", t, func() {
// See http://sci2s.ugr.es/keel/pdf/algorithm/congreso/1992-Kerber-ChimErge-AAAI92.pdf
// Randy Kerber, ChiMerge: Discretisation of Numeric Attributes, 1992
instances, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
So(err, ShouldBeNil)
Convey("Create and train the filter", func() {
filter := NewChiMergeFilter(instances, 0.90)
filter.AddAttribute(instances.AllAttributes()[0])
filter.AddAttribute(instances.AllAttributes()[1])
filter.Train()
Convey("Filter the dataset", func() {
filteredInstances := base.NewLazilyFilteredInstances(instances, filter)
classAttributes := filteredInstances.AllClassAttributes()
Convey("There should only be one class attribute", func() {
So(len(classAttributes), ShouldEqual, 1)
})
expectedClassAttribute := "Species"
Convey(fmt.Sprintf("The class attribute should be %s", expectedClassAttribute), func() {
So(classAttributes[0].GetName(), ShouldEqual, expectedClassAttribute)
})
})
})
})
}
示例12: main
func main() {
// Load in a dataset, with headers. Header attributes will be stored.
// Think of instances as a Data Frame structure in R or Pandas.
// You can also create instances from scratch.
rawData, err := base.ParseCSVToInstances("datasets/iris.csv", false)
if err != nil {
panic(err)
}
// Print a pleasant summary of your data.
fmt.Println(rawData)
//Initialises a new KNN classifier
cls := knn.NewKnnClassifier("euclidean", 2)
//Do a training-test split
trainData, testData := base.InstancesTrainTestSplit(rawData, 0.50)
cls.Fit(trainData)
//Calculates the Euclidean distance and returns the most popular label
predictions := cls.Predict(testData)
fmt.Println(predictions)
// Prints precision/recall metrics
confusionMat, err := evaluation.GetConfusionMatrix(testData, predictions)
if err != nil {
panic(fmt.Sprintf("Unable to get confusion matrix: %s", err.Error()))
}
fmt.Println(evaluation.GetSummary(confusionMat))
}
示例13: main
func main() {
var tree base.Classifier
rand.Seed(time.Now().UTC().UnixNano())
// Load in the iris dataset
iris, err := base.ParseCSVToInstances("../datasets/iris_headers.csv", true)
if err != nil {
panic(err)
}
// Discretise the iris dataset with Chi-Merge
filt := filters.NewChiMergeFilter(iris, 0.99)
filt.AddAllNumericAttributes()
filt.Build()
filt.Run(iris)
// Create a 60-40 training-test split
insts := base.InstancesTrainTestSplit(iris, 0.60)
//
// First up, use ID3
//
tree = trees.NewID3DecisionTree(0.6)
// (Parameter controls train-prune split.)
// Train the ID3 tree
tree.Fit(insts[0])
// Generate predictions
predictions := tree.Predict(insts[1])
// Evaluate
fmt.Println("ID3 Performance")
cf := eval.GetConfusionMatrix(insts[1], predictions)
fmt.Println(eval.GetSummary(cf))
//
// Next up, Random Trees
//
// Consider two randomly-chosen attributes
tree = trees.NewRandomTree(2)
tree.Fit(insts[0])
predictions = tree.Predict(insts[1])
fmt.Println("RandomTree Performance")
cf = eval.GetConfusionMatrix(insts[1], predictions)
fmt.Println(eval.GetSummary(cf))
//
// Finally, Random Forests
//
tree = ensemble.NewRandomForest(100, 3)
tree.Fit(insts[0])
predictions = tree.Predict(insts[1])
fmt.Println("RandomForest Performance")
cf = eval.GetConfusionMatrix(insts[1], predictions)
fmt.Println(eval.GetSummary(cf))
}
示例14: BenchmarkBaggingRandomForestPredict
func BenchmarkBaggingRandomForestPredict(t *testing.B) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
t.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
rand.Seed(time.Now().UnixNano())
filt := filters.NewChiMergeFilter(inst, 0.90)
for _, a := range base.NonClassFloatAttributes(inst) {
filt.AddAttribute(a)
}
filt.Train()
instf := base.NewLazilyFilteredInstances(inst, filt)
rf := new(BaggedModel)
for i := 0; i < 10; i++ {
rf.AddModel(trees.NewRandomTree(2))
}
rf.Fit(instf)
t.ResetTimer()
for i := 0; i < 20; i++ {
rf.Predict(instf)
}
}
示例15: main
func main() {
var tree base.Classifier
rand.Seed(44111342)
// Load in the iris dataset
iris, err := base.ParseCSVToInstances("/home/kralli/go/src/github.com/sjwhitworth/golearn/examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
}
// Discretise the iris dataset with Chi-Merge
filt := filters.NewChiMergeFilter(iris, 0.999)
for _, a := range base.NonClassFloatAttributes(iris) {
filt.AddAttribute(a)
}
filt.Train()
irisf := base.NewLazilyFilteredInstances(iris, filt)
// Create a 60-40 training-test split
//testData
trainData, _ := base.InstancesTrainTestSplit(iris, 0.60)
findBestSplit(trainData)
//fmt.Println(trainData)
//fmt.Println(testData)
fmt.Println(tree)
fmt.Println(irisf)
}