1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-30 13:48:57 +08:00

added String() function to sentimentIron's inline-training-data function

This commit is contained in:
Thatcher Peskens 2016-07-06 18:00:30 -07:00
parent 6f7326b6ff
commit de9a6246fd

View File

@ -3,6 +3,7 @@ package base
import ( import (
"fmt" "fmt"
"github.com/gonum/matrix/mat64" "github.com/gonum/matrix/mat64"
"bytes"
) )
type Mat64Instances struct { type Mat64Instances struct {
@ -17,13 +18,15 @@ func InstancesFromMat64(rows, cols int, data *mat64.Dense) *Mat64Instances {
var ret Mat64Instances var ret Mat64Instances
for i := 0; i < cols; i++ { for i := 0; i < cols; i++ {
ret.attributes = append(ret.attributes, NewFloatAttribute(fmt.Sprintf("%d", ret.attributes = append(ret.attributes, NewFloatAttribute(fmt.Sprintf("%d", i)))
i)))
} }
ret.classAttrs = make(map[int]bool) ret.classAttrs = make(map[int]bool)
ret.Data = data ret.Data = data
ret.rows = rows ret.rows = rows
ret.AddClassAttribute(ret.attributes[len(ret.attributes)-1])
return &ret return &ret
} }
@ -103,7 +106,6 @@ func (m *Mat64Instances) MapOverRows(as []AttributeSpec, f func([][]byte, int) (
} }
} }
return nil return nil
} }
// RowString: should print the values of a row // RowString: should print the values of a row
@ -116,3 +118,53 @@ func (m *Mat64Instances) RowString(row int) string {
func (m *Mat64Instances) Size() (int, int) { func (m *Mat64Instances) Size() (int, int) {
return len(m.attributes), m.rows return len(m.attributes), m.rows
} }
// String returns a human-readable summary of this dataset.
func (m *Mat64Instances) String() string {
var buffer bytes.Buffer
// Get all Attribute information
as := ResolveAllAttributes(m)
// Print header
cols, rows := m.Size()
buffer.WriteString("Instances with ")
buffer.WriteString(fmt.Sprintf("%d row(s) ", rows))
buffer.WriteString(fmt.Sprintf("%d attribute(s)\n", cols))
buffer.WriteString(fmt.Sprintf("Attributes: \n"))
cnt := 0
for _, a := range as {
prefix := "\t"
if m.classAttrs[cnt] {
prefix = "*\t"
}
cnt++
buffer.WriteString(fmt.Sprintf("%s%s\n", prefix, a.attr))
}
buffer.WriteString("\nData:\n")
maxRows := 30
if rows < maxRows {
maxRows = rows
}
for i := 0; i < maxRows; i++ {
buffer.WriteString("\t")
for _, a := range as {
val := m.Get(a, i)
buffer.WriteString(fmt.Sprintf("%s ", a.attr.GetStringFromSysVal(val)))
}
buffer.WriteString("\n")
}
missingRows := rows - maxRows
if missingRows != 0 {
buffer.WriteString(fmt.Sprintf("\t...\n%d row(s) undisplayed", missingRows))
} else {
buffer.WriteString("All rows displayed")
}
return buffer.String()
}