本文整理汇总了C++中MatType::col方法的典型用法代码示例。如果您正苦于以下问题:C++ MatType::col方法的具体用法?C++ MatType::col怎么用?C++ MatType::col使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MatType
的用法示例。
在下文中一共展示了MatType::col方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
const BallBound<VecType>&
BallBound<VecType>::operator|=(const MatType& data)
{
if (radius < 0)
{
center = data.col(0);
radius = 0;
}
// Now iteratively add points. There is probably a closed-form solution to
// find the minimum bounding circle, and it is probably faster.
for (size_t i = 1; i < data.n_cols; ++i)
{
double dist = metric::EuclideanDistance::Evaluate(center, (VecType)
data.col(i)) - radius;
if (dist > 0)
{
// Move (dist / 2) towards the new point and increase radius by
// (dist / 2).
arma::vec diff = data.col(i) - center;
center += 0.5 * diff;
radius += 0.5 * dist;
}
}
return *this;
}
示例2:
const BallBound<MetricType, VecType>&
BallBound<MetricType, VecType>::operator|=(const MatType& data)
{
if (radius < 0)
{
center = data.col(0);
radius = 0;
}
// Now iteratively add points.
for (size_t i = 0; i < data.n_cols; ++i)
{
const ElemType dist = metric->Evaluate(center, (VecType) data.col(i));
// See if the new point lies outside the bound.
if (dist > radius)
{
// Move towards the new point and increase the radius just enough to
// accommodate the new point.
const VecType diff = data.col(i) - center;
center += ((dist - radius) / (2 * dist)) * diff;
radius = 0.5 * (dist + radius);
}
}
return *this;
}
示例3: EmptyCluster
size_t MaxVarianceNewCluster::EmptyCluster(const MatType& data,
const size_t emptyCluster,
const arma::mat& oldCentroids,
arma::mat& newCentroids,
arma::Col<size_t>& clusterCounts,
MetricType& metric,
const size_t iteration)
{
// If necessary, calculate the variances and assignments.
if (iteration != this->iteration || assignments.n_elem != data.n_cols)
Precalculate(data, oldCentroids, clusterCounts, metric);
this->iteration = iteration;
// Now find the cluster with maximum variance.
arma::uword maxVarCluster;
variances.max(maxVarCluster);
// Now, inside this cluster, find the point which is furthest away.
size_t furthestPoint = data.n_cols;
double maxDistance = -DBL_MAX;
for (size_t i = 0; i < data.n_cols; ++i)
{
if (assignments[i] == maxVarCluster)
{
const double distance = std::pow(metric.Evaluate(data.col(i),
newCentroids.col(maxVarCluster)), 2.0);
if (distance > maxDistance)
{
maxDistance = distance;
furthestPoint = i;
}
}
}
// Take that point and add it to the empty cluster.
newCentroids.col(maxVarCluster) *= (double(clusterCounts[maxVarCluster]) /
double(clusterCounts[maxVarCluster] - 1));
newCentroids.col(maxVarCluster) -= (1.0 / (clusterCounts[maxVarCluster] - 1.0)) *
arma::vec(data.col(furthestPoint));
clusterCounts[maxVarCluster]--;
clusterCounts[emptyCluster]++;
newCentroids.col(emptyCluster) = arma::vec(data.col(furthestPoint));
assignments[furthestPoint] = emptyCluster;
// Modify the variances, as necessary.
variances[emptyCluster] = 0;
// One has already been subtracted from clusterCounts[maxVarCluster].
variances[maxVarCluster] = (1.0 / (clusterCounts[maxVarCluster])) *
((clusterCounts[maxVarCluster] + 1) * variances[maxVarCluster] - maxDistance);
// Output some debugging information.
Log::Debug << "Point " << furthestPoint << " assigned to empty cluster " <<
emptyCluster << ".\n";
return 1; // We only changed one point.
}
示例4: PerformSplit
size_t PerformSplit(MatType& data,
const size_t begin,
const size_t count,
const typename SplitType::SplitInfo& splitInfo,
std::vector<size_t>& oldFromNew)
{
// This method modifies the input dataset. We loop both from the left and
// right sides of the points contained in this node.
size_t left = begin;
size_t right = begin + count - 1;
// First half-iteration of the loop is out here because the termination
// condition is in the middle.
while ((left <= right) &&
(SplitType::AssignToLeftNode(data.col(left), splitInfo)))
left++;
while ((!SplitType::AssignToLeftNode(data.col(right), splitInfo)) &&
(left <= right) && (right > 0))
right--;
// Shortcut for when all points are on the right.
if (left == right && right == 0)
return left;
while (left <= right)
{
// Swap columns.
data.swap_cols(left, right);
// Update the indices for what we changed.
size_t t = oldFromNew[left];
oldFromNew[left] = oldFromNew[right];
oldFromNew[right] = t;
// See how many points on the left are correct. When they are correct,
// increase the left counter accordingly. When we encounter one that isn't
// correct, stop. We will switch it later.
while (SplitType::AssignToLeftNode(data.col(left), splitInfo) &&
(left <= right))
left++;
// Now see how many points on the right are correct. When they are correct,
// decrease the right counter accordingly. When we encounter one that isn't
// correct, stop. We will switch it with the wrong point we found in the
// previous loop.
while ((!SplitType::AssignToLeftNode(data.col(right), splitInfo)) &&
(left <= right))
right--;
}
Log::Assert(left == right + 1);
return left;
}
示例5: EmptyCluster
size_t MaxVarianceNewCluster::EmptyCluster(const MatType& data,
const size_t emptyCluster,
const MatType& centroids,
arma::Col<size_t>& clusterCounts,
arma::Col<size_t>& assignments)
{
// First, we need to find the cluster with maximum variance (by which I mean
// the sum of the covariance matrices).
arma::vec variances;
variances.zeros(clusterCounts.n_elem); // Start with 0.
// Add the variance of each point's distance away from the cluster. I think
// this is the sensible thing to do.
for (size_t i = 0; i < data.n_cols; i++)
{
variances[assignments[i]] += arma::as_scalar(
arma::var(data.col(i) - centroids.col(assignments[i])));
}
// Now find the cluster with maximum variance.
arma::uword maxVarCluster;
variances.max(maxVarCluster);
// Now, inside this cluster, find the point which is furthest away.
size_t furthestPoint = data.n_cols;
double maxDistance = -DBL_MAX;
for (size_t i = 0; i < data.n_cols; i++)
{
if (assignments[i] == maxVarCluster)
{
double distance = arma::as_scalar(
arma::var(data.col(i) - centroids.col(maxVarCluster)));
if (distance > maxDistance)
{
maxDistance = distance;
furthestPoint = i;
}
}
}
// Take that point and add it to the empty cluster.
clusterCounts[maxVarCluster]--;
clusterCounts[emptyCluster]++;
assignments[furthestPoint] = emptyCluster;
// Output some debugging information.
Log::Debug << "Point " << furthestPoint << " assigned to empty cluster " <<
emptyCluster << ".\n";
return 1; // We only changed one point.
}
示例6: while
void Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Train(
const MatType& data,
const arma::Row<size_t>& labels,
const arma::rowvec& instanceWeights)
{
size_t j, i = 0;
bool converged = false;
size_t tempLabel;
arma::uword maxIndexRow, maxIndexCol;
arma::mat tempLabelMat;
LearnPolicy LP;
const bool hasWeights = (instanceWeights.n_elem > 0);
while ((i < maxIterations) && (!converged))
{
// This outer loop is for each iteration, and we use the 'converged'
// variable for noting whether or not convergence has been reached.
i++;
converged = true;
// Now this inner loop is for going through the dataset in each iteration.
for (j = 0; j < data.n_cols; j++)
{
// Multiply for each variable and check whether the current weight vector
// correctly classifies this.
tempLabelMat = weights.t() * data.col(j) + biases;
tempLabelMat.max(maxIndexRow, maxIndexCol);
// Check whether prediction is correct.
if (maxIndexRow != labels(0, j))
{
// Due to incorrect prediction, convergence set to false.
converged = false;
tempLabel = labels(0, j);
// Send maxIndexRow for knowing which weight to update, send j to know
// the value of the vector to update it with. Send tempLabel to know
// the correct class.
if (hasWeights)
LP.UpdateWeights(data.col(j), weights, biases, maxIndexRow, tempLabel,
instanceWeights(j));
else
LP.UpdateWeights(data.col(j), weights, biases, maxIndexRow,
tempLabel);
}
}
}
}
示例7: Initialize
inline static void Initialize(const MatType& V,
const size_t r,
arma::mat& W,
arma::mat& H)
{
const size_t n = V.n_rows;
const size_t m = V.n_cols;
if (columnsToAverage > m)
{
Log::Warn << "Number of random columns (columnsToAverage) is more than "
<< "the number of columns available in the V matrix; weird results "
<< "may ensue!" << std::endl;
}
W.zeros(n, r);
// Initialize W matrix with random columns.
for (size_t col = 0; col < r; col++)
{
for (size_t randCol = 0; randCol < columnsToAverage; randCol++)
{
// .col() does not work in this case, as of Armadillo 3.920.
W.unsafe_col(col) += V.col(math::RandInt(0, m));
}
}
// Now divide by p.
W /= columnsToAverage;
// Initialize H to random values.
H.randu(r, m);
}
示例8: Precalculate
void MaxVarianceNewCluster::Precalculate(const MatType& data,
const arma::mat& oldCentroids,
arma::Col<size_t>& clusterCounts,
MetricType& metric)
{
// We have to calculate the variances of each cluster and the assignments of
// each point. This is most easily done by iterating through the entire
// dataset.
variances.zeros(oldCentroids.n_cols);
assignments.set_size(data.n_cols);
// Add the variance of each point's distance away from the cluster. I think
// this is the sensible thing to do.
for (size_t i = 0; i < data.n_cols; ++i)
{
// Find the closest centroid to this point.
double minDistance = std::numeric_limits<double>::infinity();
size_t closestCluster = oldCentroids.n_cols; // Invalid value.
for (size_t j = 0; j < oldCentroids.n_cols; j++)
{
const double distance = metric.Evaluate(data.col(i), oldCentroids.col(j));
if (distance < minDistance)
{
minDistance = distance;
closestCluster = j;
}
}
assignments[i] = closestCluster;
variances[closestCluster] += std::pow(metric.Evaluate(data.col(i),
oldCentroids.col(closestCluster)), 2.0);
}
// Divide by the number of points in the cluster to produce the variance,
// unless the cluster is empty or contains only one point, in which case we
// set the variance to 0.
for (size_t i = 0; i < clusterCounts.n_elem; ++i)
if (clusterCounts[i] <= 1)
variances[i] = 0;
else
variances[i] /= clusterCounts[i];
}
示例9:
void UBTreeSplit<BoundType, MatType>::InitializeAddresses(const MatType& data)
{
addresses.resize(data.n_cols);
// Calculate all addresses.
for (size_t i = 0; i < data.n_cols; i++)
{
addresses[i].first.zeros(data.n_rows);
bound::addr::PointToAddress(addresses[i].first, data.col(i));
addresses[i].second = i;
}
}
示例10: Cluster
inline static void Cluster(const MatType& data,
const size_t clusters,
arma::mat& centroids)
{
centroids.set_size(data.n_rows, clusters);
for (size_t i = 0; i < clusters; ++i)
{
// Randomly sample a point.
const size_t index = math::RandInt(0, data.n_cols);
centroids.col(i) = data.col(index);
}
}
示例11: predictedLabels
void Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Classify(
const MatType& test,
arma::Row<size_t>& predictedLabels)
{
arma::vec tempLabelMat;
arma::uword maxIndex = 0;
// Could probably be faster if done in batch.
for (size_t i = 0; i < test.n_cols; i++)
{
tempLabelMat = weights.t() * test.col(i) + biases;
tempLabelMat.max(maxIndex);
predictedLabels(0, i) = maxIndex;
}
}
示例12: Cluster
void RefinedStart::Cluster(const MatType& data,
const size_t clusters,
arma::mat& centroids) const
{
// This will hold the sampled datasets.
const size_t numPoints = size_t(percentage * data.n_cols);
MatType sampledData(data.n_rows, numPoints);
// vector<bool> is packed so each bool is 1 bit.
std::vector<bool> pointsUsed(data.n_cols, false);
arma::mat sampledCentroids(data.n_rows, samplings * clusters);
for (size_t i = 0; i < samplings; ++i)
{
// First, assemble the sampled dataset.
size_t curSample = 0;
while (curSample < numPoints)
{
// Pick a random point in [0, numPoints).
size_t sample = (size_t) math::RandInt(data.n_cols);
if (!pointsUsed[sample])
{
// This point isn't used yet. So we'll put it in our sample.
pointsUsed[sample] = true;
sampledData.col(curSample) = data.col(sample);
++curSample;
}
}
// Now, using the sampled dataset, run k-means. In the case of an empty
// cluster, we re-initialize that cluster as the point furthest away from
// the cluster with maximum variance. This is not *exactly* what the paper
// implements, but it is quite similar, and we'll call it "good enough".
KMeans<> kmeans;
kmeans.Cluster(sampledData, clusters, centroids);
// Store the sampled centroids.
sampledCentroids.cols(i * clusters, (i + 1) * clusters - 1) = centroids;
pointsUsed.assign(data.n_cols, false);
}
// Now, we run k-means on the sampled centroids to get our final clusters.
KMeans<> kmeans;
kmeans.Cluster(sampledCentroids, clusters, centroids);
}
示例13: values
bool RPTreeMaxSplit<BoundType, MatType>::GetSplitVal(
const MatType& data,
const size_t begin,
const size_t count,
const arma::Col<ElemType>& direction,
ElemType& splitVal)
{
const size_t maxNumSamples = 100;
const size_t numSamples = std::min(maxNumSamples, count);
arma::uvec samples;
// Get no more than numSamples distinct samples.
math::ObtainDistinctSamples(begin, begin + count, numSamples, samples);
arma::Col<ElemType> values(samples.n_elem);
// Find the median of scalar products of the samples and the normal vector.
for (size_t k = 0; k < samples.n_elem; k++)
values[k] = arma::dot(data.col(samples[k]), direction);
const ElemType maximum = arma::max(values);
const ElemType minimum = arma::min(values);
if (minimum == maximum)
return false;
splitVal = arma::median(values);
// Add a random deviation to the median.
// This algorithm differs from the method suggested in the random projection
// tree paper, for two reasons:
// 1. Evaluating the method proposed in the paper is time-consuming, since
// we must solve the furthest-pair problem.
// 2. The proposed method does not appear to guarantee that a valid split
// value will be generated (i.e. it can produce a split value where there
// may be no points on the left or the right).
splitVal += math::Random((minimum - splitVal) * 0.75,
(maximum - splitVal) * 0.75);
if (splitVal == maximum)
splitVal = minimum;
return true;
}
示例14: HyperplaneType
bool MeanSpaceSplit<MetricType, MatType>::SplitSpace(
const typename HyperplaneType::BoundType& bound,
const MatType& data,
const arma::Col<size_t>& points,
HyperplaneType& hyp)
{
typename HyperplaneType::ProjVectorType projVector;
double midValue;
if (!SpaceSplit<MetricType, MatType>::GetProjVector(bound, data, points,
projVector, midValue))
return false;
double splitVal = 0.0;
for (size_t i = 0; i < points.n_elem; i++)
splitVal += projVector.Project(data.col(points[i]));
splitVal /= points.n_elem;
hyp = HyperplaneType(projVector, splitVal);
return true;
}
示例15:
void MeanShift<UseKernel, KernelType, MatType>::GenSeeds(
const MatType& data,
const double binSize,
const int minFreq,
MatType& seeds)
{
typedef arma::colvec VecType;
std::map<VecType, int, less<VecType> > allSeeds;
for (size_t i = 0; i < data.n_cols; ++i)
{
VecType binnedPoint = arma::floor(data.unsafe_col(i) / binSize);
if (allSeeds.find(binnedPoint) == allSeeds.end())
allSeeds[binnedPoint] = 1;
else
allSeeds[binnedPoint]++;
}
// Remove seeds with too few points. First we count the number of seeds we
// end up with, then we add them.
std::map<VecType, int, less<VecType> >::iterator it;
size_t count = 0;
for (it = allSeeds.begin(); it != allSeeds.end(); ++it)
if (it->second >= minFreq)
++count;
seeds.set_size(data.n_rows, count);
count = 0;
for (it = allSeeds.begin(); it != allSeeds.end(); ++it)
{
if (it->second >= minFreq)
{
seeds.col(count) = it->first;
++count;
}
}
seeds *= binSize;
}