Esempio n. 1
0
func createNeigboursByIdx(indices []int) (neighbors knn.Neighbors) {
	for x, idx := range indices {
		row := tabula.Row{}

		for _, v := range dataFloat64[idx] {
			rec := tabula.NewRecordReal(v)
			row.PushBack(rec)
		}

		neighbors.Add(&row, float64(distances[x]))
	}
	return
}
Esempio n. 2
0
func createNeigbours() (neighbors knn.Neighbors) {
	for x, d := range dataFloat64 {
		row := tabula.Row{}

		for _, v := range d {
			rec := tabula.NewRecordReal(v)
			row.PushBack(rec)
		}

		neighbors.Add(&row, float64(distances[x]))
	}
	return
}
Esempio n. 3
0
//
// createSynthetic will create synthetics row from original row `p` and their
// `neighbors`.
//
func (in *Runtime) createSynthetic(p *tabula.Row, neighbors knn.Neighbors) (
	synthetic *tabula.Row,
) {
	// choose one of the K nearest neighbors
	randIdx := rand.Intn(neighbors.Len())
	n := neighbors.Row(randIdx)

	// Check if synthetic sample can be created from p and n.
	canit, slp, sln := in.canCreate(p, n)
	if !canit {
		if DEBUG >= 2 {
			fmt.Println("[lnsmote] can not create synthetic")
		}

		if slp.Len() <= 0 {
			in.outliers.PushBack(p)
		}

		// we can not create from p and synthetic.
		return nil
	}

	synthetic = p.Clone()

	for x, srec := range *synthetic {
		// Skip class attribute.
		if x == in.ClassIndex {
			continue
		}

		delta := in.randomGap(p, n, slp.Len(), sln.Len())
		pv := (*p)[x].Float()
		diff := (*n)[x].Float() - pv
		srec.SetFloat(pv + delta*diff)
	}

	return
}
Esempio n. 4
0
/*
WriteRow dump content of Row to file using format in metadata.
*/
func (writer *Writer) WriteRow(row *tabula.Row, recordMd []MetadataInterface) (
	e error,
) {
	nRecord := row.Len()
	v := []byte{}
	esc := []byte(DefEscape)

	for i := range writer.OutputMetadata {
		md := writer.OutputMetadata[i]

		// find the input index based on name on record metadata.
		rIdx, mdMatch := FindMetadata(&md, recordMd)

		// No input metadata matched? skip it too.
		if rIdx >= nRecord {
			continue
		}

		// If input column is ignored, continue to next record.
		if mdMatch != nil && mdMatch.GetSkip() {
			continue
		}

		recV := (*row)[rIdx].Bytes()
		lq := md.GetLeftQuote()

		if "" != lq {
			v = append(v, []byte(lq)...)
		}

		rq := md.GetRightQuote()
		sep := md.GetSeparator()

		// Escape the escape character itself.
		if md.T == tabula.TString {
			recV, _ = tekstus.BytesEncapsulate(esc, recV, esc, nil)
		}

		// Escape the right quote in field content before writing it.
		if "" != rq && md.T == tabula.TString {
			recV, _ = tekstus.BytesEncapsulate([]byte(rq), recV,
				esc, nil)
		} else {
			// Escape the separator
			if "" != sep && md.T == tabula.TString {
				recV, _ = tekstus.BytesEncapsulate([]byte(sep),
					recV, esc, nil)
			}
		}

		v = append(v, recV...)

		if "" != rq {
			v = append(v, []byte(rq)...)
		}

		if "" != sep {
			v = append(v, []byte(sep)...)
		}
	}

	v = append(v, DefEOL)

	_, e = writer.BufWriter.Write(v)

	return e
}