Fold the dataset k - 1 times to form k equal size datasets.

@param int $k
@throws \InvalidArgumentException
@return array
968295
public function fold(int $k = 10) : array
    {
        if ($k < 2) {
            throw new InvalidArgumentException('Cannot create less than'
                . " 2 folds, $k given.");
        }

        $n = (int) floor($this->numRows() / $k);

        $folds = [];

        for ($i = 0; $i < $k; $i++) {
            $offset = $i * $n;

            $samples = array_slice($this->samples, $offset, $n);
            $labels = array_slice($this->labels, $offset, $n);

            $folds[] = self::quick($samples, $labels);
        }

        return $folds;
    }
1272161
public function &__get($key)
    {
        if (!isset($this->stack[$key])) {
            throw new InvalidArgumentException(sprintf('Value "%s" is not defined.', $key));
        }

        $set = isset($this->stack[$key]);

        //return $this->stack[$key];
        $return = $set &&
        is_callable($this->stack[$key]) ?
            $this->stack[$key]($this) :
            $this->stack[$key];

        return $return;
    }
880218
public function flattenArray($array)
    {
        $result = [];
        foreach (array_dot($array) as $key => $value) {
            $splitKey = explode('.', $key);
            $newKey = $this->createArrayKey($splitKey);
            $result = array_merge($result, [$newKey => $value]);
        }

        return $result;
    }
595170
public function merge( $elements ) {
		if ( $elements instanceof static ) {
			$elements = $elements->to_array();
		}

		if ( ! is_array( $elements ) ) {
			throw new InvalidArgumentException( 'Merge must be given array or Collection' );
		}

		$this->type->validate_elements( $elements );

		return $this->new_from_trusted(
			array_merge( $this->elements, $elements )
		);
	}
207622
public static function collapse($array)
    {
        $results = [];

        foreach ($array as $values) {
            if ($values instanceof Collection) {
                $values = $values->all();
            } elseif (! is_array($values)) {
                continue;
            }

            $results = array_merge($results, $values);
        }

        return $results;
    }
1341659
public function split(float $ratio = 0.5) : array
    {
        if ($ratio <= 0. or $ratio >= 1.) {
            throw new InvalidArgumentException('Split ratio must be strictly'
                . " between 0 and 1, $ratio given.");
        }

        $p = (int) ($ratio * $this->numRows());

        $left = self::quick(array_slice($this->samples, 0, $p));
        $right = self::quick(array_slice($this->samples, $p));

        return [$left, $right];
    }
599965
public function append($k, $v = null)
    {
        $array = $this->val();

        if (!$this->isNull($v)) {
            $array[$k] = $v;
        } else {
            $array[] = $k;
        }

        $this->val($array);

        return $this;
    }
1301732
public function flattenRecursive()
    {
        $flat = array();

        foreach (new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this)) as $value) {
            $flat[] = $value;
        }

        return $flat;
    }
816239
public function fit(Dataset $dataset) : void
    {
        if (!$dataset->homogeneous() or $dataset->columnType(0) !== DataType::CONTINUOUS) {
            throw new InvalidArgumentException('This transformer only works'
                . ' with continuous features.');
        }

        $this->r = Matrix::gaussian($dataset->numColumns(), $this->dimensions);
    }
650060
public function divide($b)
    {
        switch (true) {
            case $b instanceof Matrix:
                $c = $this->divideMatrix($b);
                break;

            case $b instanceof ColumnVector:
                $c = $this->divideColumnVector($b);
                break;
            
            case $b instanceof Vector:
                $c = $this->divideVector($b);
                break;

            case is_int($b) or is_float($b):
                $c = $this->divideScalar($b);
                break;

            default:
                throw new InvalidArgumentException('Cannot divide matrix'
                    . ' with a ' . gettype($b) . '.');
        }

        return $c;
    }
889094
public function stratifiedFold(int $k = 10) : array
    {
        if ($k < 2) {
            throw new InvalidArgumentException('Cannot create less than'
                . " 2 folds, $k given.");
        }

        $folds = [];

        for ($i = 0; $i < $k; $i++) {
            $samples = $labels = [];

            foreach ($this->_stratify() as $label => $stratum) {
                $n = (int) floor(count($stratum) / $k);

                $samples = array_merge($samples, array_slice($stratum, $i * $n, $n));
                $labels = array_merge($labels, array_fill(0, $n, $label));
            }

            $folds[] = self::quick($samples, $labels);
        }

        return $folds;
    }
570283
private function convertColumnsToRawFields(array $columns) {
        $columnMaps = $this->getModelsMetaData()->getColumnMap($this);
        foreach($columns as $key => $value) {
            if(!array_key_exists($value, $columnMaps)) {
                //Look up in the changed values
                $index = array_search($value, $columnMaps);
                if($index === false) {
                    throw new \InvalidArgumentException('Invalid Column, could not find: ' . $value);
                } else {
                    $columns[$key] = $index;
                }
            }
        }
        return $columns;
    }
499098
public static function iterableToArray($iterable): array
    {
        if (is_array($iterable)) {
            return $iterable;
        }

        if ($iterable instanceof \Traversable) {
            return iterator_to_array($iterable);
        }

        throw new \InvalidArgumentException('Iterable is expected.');
    }
490938
protected function rebase(array $a)
    {
        $res = [];
        ksort($a);
        foreach(array_values($a) as $row) {
            ksort($row);
            $res[] = array_values($row);
        }

        return $res;
    }
936601
public static function flatten($array, $glue = ':', $reset = true, $indexed = true)
    {
        static $return = array();
        static $curr_key = array();

        if ($reset) {
            $return = array();
            $curr_key = array();
        }

        foreach ($array as $key => $val) {
            $curr_key[] = $key;
            if (is_array($val) and ($indexed or array_values($val) !== $val)) {
                static::flatten_assoc($val, $glue, false);
            } else {
                $return[implode($glue, $curr_key)] = $val;
            }
            array_pop($curr_key);
        }
        return $return;
    }
346210
public function mutate($objects)
	{
		$objects = call_user_func_array(array($this, 'flattenArguments'), func_get_args());
		$collect = [];

		foreach ($objects as $obj) {
			if ($obj instanceof AbstractGenerator) {
				array_push($collect, $obj);
				continue;
			}

			throw new InvalidArgumentException(sprintf('Mutable objects must be instances of %s.', AbstractGenerator::class));
		}
		
		$this->mutates = array_merge(static::objectDiffInArrays($this->mutates, $collect), $collect);
		
		return $this;
	}
104021
public static function stack(array $datasets) : self
    {
        $samples = $labels = [];

        foreach ($datasets as $dataset) {
            if (!$dataset instanceof self) {
                throw new InvalidArgumentException('Dataset must be'
                    . ' an instance of Labeled, ' . get_class($dataset)
                    . ' given.');
            }

            $samples = array_merge($samples, $dataset->samples());
            $labels = array_merge($labels, $dataset->labels());
        }

        return self::quick($samples, $labels);
    }