本文整理汇总了C#中System.Matrix.Row方法的典型用法代码示例。如果您正苦于以下问题:C# Matrix.Row方法的具体用法?C# Matrix.Row怎么用?C# Matrix.Row使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Matrix
的用法示例。
在下文中一共展示了Matrix.Row方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: determineEuclideanDistMX
public Matrix<double> determineEuclideanDistMX(Matrix<double> objCoords)
{
int nObj = objCoords.RowCount;
Matrix<double> distanceMX = Matrix<double>.Build.Dense(nObj, nObj);
for (int idx = 0; idx < nObj; idx++)
{
for (int subIdx = 0; subIdx < nObj; subIdx++)
{
distanceMX[idx, subIdx] = Distance.Euclidean<double>(objCoords.Row(idx), objCoords.Row(subIdx));
}
}
return distanceMX;
}
示例2: GetTopKNeighborsByUser
/// <summary>
/// Get the top K neighbors of the target user.
/// </summary>
/// <param name="similaritiesByUser">The similarities matrix.</param>
/// <param name="indexOfTargetUser">The index of target user.</param>
/// <param name="K">The number of neighbors.</param>
/// <returns>Each key is one user of the top K neighbors,
/// and the value is his similarity to the target user.</returns>
public static Dictionary<int, double> GetTopKNeighborsByUser(Matrix<double> similaritiesByUser, int indexOfTargetUser, int K)
{
Debug.Assert(similaritiesByUser.RowCount == similaritiesByUser.ColumnCount, "The similarities should be in a square matrix.");
Debug.Assert(similaritiesByUser.RowCount > K, "The total number of users is less than K + 1 ");
Dictionary<int, double> similaritiesByTopKUser = new Dictionary<int, double>(K);
// They will be sorted soon
List<double> similaritiesOfNeighborsSortedBySimilarity = similaritiesByUser.Row(indexOfTargetUser).ToList();
List<int> indexesOfNeighborsSortedBySimilarity = Enumerable.Range(0, similaritiesOfNeighborsSortedBySimilarity.Count).ToList();
// To exclute the target user himself from neighbors
similaritiesOfNeighborsSortedBySimilarity[indexOfTargetUser] = double.MinValue;
// Sort the neighbors' indexes according to their similarities to the target user
Sorting.Sort<double, int>(similaritiesOfNeighborsSortedBySimilarity, indexesOfNeighborsSortedBySimilarity);
// Make it descending order by similarity
similaritiesOfNeighborsSortedBySimilarity.Reverse();
indexesOfNeighborsSortedBySimilarity.Reverse();
for (int i = 0; i < K; ++i)
{
// Be very careful about the index
// indexesOfNeighborsSortedBySimilarity[0] will give
// the index (in similaritiesByUser) of the most similar neighbor
// and i is the index in the sorted similaritiesOfNeighbors
similaritiesByTopKUser[indexesOfNeighborsSortedBySimilarity[i]] = similaritiesOfNeighborsSortedBySimilarity[i];
}
return similaritiesByTopKUser;
}
示例3: Molecule
public Molecule(double[,] inputAtoms, bool getDirections)
{
numAtoms = inputAtoms.GetLength(0);
atoms = DenseMatrix.OfArray(inputAtoms);
centroid = atoms.ColumnSums() / (double)numAtoms;
if (getDirections)
{
for (int i = 0; i < 3; i++)
{
atoms.SetColumn(i, atoms.Column(i) - centroid[i]);
}
direction = atoms.Svd(true).VT.Row(0);
if ((atoms.Row(atoms.RowCount - 1) - atoms.Row(0)).DotProduct(direction) < 0)
{
direction *= -1;
}
}
}
示例4: determineWeightedEuclideanDistMX
public Matrix<double> determineWeightedEuclideanDistMX(Matrix<double> objCoords, double[] weights)
{
int nObj = objCoords.RowCount;
int dim = objCoords.ColumnCount;
Matrix<double> distanceMX = Matrix<double>.Build.Dense(nObj, nObj);
for (int idx = 0; idx < nObj; idx++)
{
for (int subIdx = 0; subIdx < nObj; subIdx++)
{
double currentDist = 0.0;
for (int idxDim = 0; idxDim < dim; idxDim++)
{
currentDist += weights[idxDim] *
Math.Pow(objCoords.Row(idx)[idxDim] - objCoords.Row(subIdx)[idxDim], 2);
}
distanceMX[idx, subIdx] = Math.Sqrt(currentDist);
}
}
return distanceMX;
}
示例5: computeMiddlenessOfObjs
private Dictionary<double, int> computeMiddlenessOfObjs(Matrix<double> distanceMX)
{
int nObj = distanceMX.RowCount;
Matrix<double> middlenessMX = Matrix<double>.Build.Dense(nObj, nObj,
(i, j) => distanceMX[i, j] / distanceMX.Row(i).Sum());
Dictionary<double, int> dictOfMiddleness = new Dictionary<double, int>();
for (int idx = 0; idx < nObj; idx++)
{
double currentMiddleness = middlenessMX.Column(idx).Sum();
dictOfMiddleness[currentMiddleness] = idx;
}
return dictOfMiddleness;
}
示例6: SplitDataSet
static Matrix<double> SplitDataSet(Matrix<double> dataSet, int axis, double value)
{
var retDataList = new List<Vector<double>>();
for(var i = 0;i<dataSet.RowCount;i++)
{
var featVec = dataSet.Row(i);
if (featVec[axis] == value)
{
var reducedFeatVec = Vector.Build.DenseOfEnumerable(
featVec.Take(axis).Concat(featVec.Skip(axis+1)));
retDataList.Add(reducedFeatVec);
}
}
return Matrix.Build.DenseOfRowVectors(retDataList);
}
示例7: CalcShannonEnt
static double CalcShannonEnt(Matrix<double> dataSet, string[] labels)
{
var numEntries = dataSet.RowCount;
var labelCounts = new Dictionary<string, int>();
for(var i = 0;i<numEntries;i++)
{
var featVec = dataSet.Row(i);
var currentLabel = labels[i];
if (!labelCounts.ContainsKey(currentLabel)) labelCounts.Add(currentLabel, 0);
labelCounts[currentLabel] += 1;
}
var shannonEnt = 0.0;
foreach(var key in labelCounts.Keys)
{
var prob = (double)(labelCounts[key]) / (double)numEntries;
shannonEnt -= prob * Math.Log(prob, 2);
}
return shannonEnt;
}
示例8: BuildModel
public ILinearRegressionModel BuildModel(Matrix<double> matrixX, Vector<double> vectorY, ILinearRegressionParams regressionParams)
{
var weightsVector = Vector<double>.Build.DenseOfArray(
Enumerable.Range(0, matrixX.ColumnCount)
.Select(fIdx => randomizer.NextDoubleInRange(weightsMin, weightsMax))
.ToArray());
double previousIterError = double.PositiveInfinity;
for (int iterNo = 0; iterNo < iterations; iterNo++)
{
var vectorYHat = matrixX.Multiply(weightsVector);
var diffVector = vectorYHat.Subtract(vectorY);
var currentIterError = diffVector.Select(Math.Abs).Sum();
if ((currentIterError <= stopLerningError) ||
currentIterError.AlmostEqual(previousIterError, 5))
{
break;
}
for (int weightIdx = 0; weightIdx < weightsVector.Count; weightIdx++)
{
var diffSums = 0.0;
for (int rowIdx = 0; rowIdx < matrixX.RowCount; rowIdx++)
{
diffSums += diffVector[rowIdx] * matrixX.Row(rowIdx)[weightIdx];
}
var newWeightValue = CalculateWeightUpdate(
matrixX,
regressionParams,
diffSums,
weightsVector[weightIdx]);
weightsVector[weightIdx] = newWeightValue;
}
previousIterError = currentIterError;
}
return new LinearRegressionModel(weightsVector);
}
示例9: OneVsOneCoef
/// <summary>
/// Generate primal coefficients from dual coefficients
/// for the one-vs-one multi class LibSVM in the case
/// of a linear kernel.
/// </summary>
private static Vector<double>[] OneVsOneCoef(
Matrix<double> dualCoef,
int[] nSupport,
Matrix<double> supportVectors)
{
// get 1vs1 weights for all n*(n-1) classifiers.
// this is somewhat messy.
// shape of dual_coef_ is nSV * (n_classes -1)
// see docs for details
int nClass = dualCoef.RowCount + 1;
// XXX we could do preallocation of coef but
// would have to take care in the sparse case
var coef = new List<Vector<double>>();
var svLocs = CumSum(new[] { 0 }.Concat(nSupport).ToArray());
for (int class1 = 0; class1 < nClass; class1++)
{
// SVs for class1:
var sv1 = supportVectors.SubMatrix(
svLocs[class1],
svLocs[class1 + 1] - svLocs[class1],
0,
supportVectors.ColumnCount);
for (int class2 = class1 + 1; class2 < nClass; class2++)
{
// SVs for class1:
var sv2 = supportVectors.SubMatrix(
svLocs[class2],
svLocs[class2 + 1] - svLocs[class2],
0,
supportVectors.ColumnCount);
// dual coef for class1 SVs:
var alpha1 = dualCoef.Row(class2 - 1).SubVector(
svLocs[class1],
svLocs[class1 + 1] - svLocs[class1]);
// dual coef for class2 SVs:
var alpha2 = dualCoef.Row(class1).SubVector(
svLocs[class2],
svLocs[class2 + 1] - svLocs[class2]);
// build weight for class1 vs class2
coef.Add((alpha1 * sv1) + (alpha2 * sv2));
}
}
return coef.ToArray();
}
示例10: ColumnsHead
private static IList<string> ColumnsHead(HtmlNode head)
{
#if NET20
var rows = IEnumerableExtensionMethods.ToList(head.Descendants("tr"));
#else
var rows = head.Descendants("tr").ToList();
#endif
if (0 == rows.Count)
{
return new List<string>();
}
var matrix = new Matrix<string>();
foreach (var cell in rows[0].Descendants("th"))
{
var colspan = cell.Attributes["colspan"];
if (null == colspan)
{
matrix.Width++;
continue;
}
for (var i = 0; i < XmlConvert.ToInt32(colspan.Value); i++)
{
matrix.Width++;
}
}
var carry = new List<int>();
for (var i = 0; i < matrix.Width; i++)
{
carry.Add(0);
}
var y = 0;
foreach (var row in rows)
{
var x = 0;
matrix.Height++;
foreach (var cell in row.Descendants("th"))
{
while (0 != carry[x])
{
matrix[x, y] = matrix[x, y - 1];
carry[x]--;
x++;
}
var rowspan = cell.Attributes["rowspan"];
if (null != rowspan)
{
carry[x] = XmlConvert.ToInt32(rowspan.Value);
}
var colspan = cell.Attributes["colspan"];
var name = ColumnName(cell);
var index = 1;
for (var i = 0; i < (null == colspan ? 1 : XmlConvert.ToInt32(colspan.Value)); i++)
{
matrix[x++, y] = string.Format(CultureInfo.InvariantCulture, null == colspan ? "{0}" : "{0} ({1})", name, index++);
}
}
y++;
}
#if NET20
var list = new List<string>();
foreach (var element in matrix.Row(matrix.Height - 1))
{
list.Add(element);
}
return list;
#else
return matrix.Row(matrix.Height - 1).ToList();
#endif
}
示例11: GetCorrelateedWords
private List<CorrelatedWord> GetCorrelateedWords(string inputWord, List<string> allWords, Matrix<double> wordsWordsMatrix)
{
var stemedInputWord = _ps.stemTerm(inputWord);
var simillarWords = allWords.Where(w => _ps.stemTerm(w).Equals(stemedInputWord));
var simillarWordsIndexes = simillarWords.Select(w => allWords.IndexOf(w));
var correlatedWords = new List<CorrelatedWord>();
foreach (var correlatedWordIndex in simillarWordsIndexes)
{
for (int i = 0; i < wordsWordsMatrix.Row(correlatedWordIndex).Count; i++)
{
correlatedWords.Add(new CorrelatedWord() { Word = allWords[i], Correlation = wordsWordsMatrix.Row(correlatedWordIndex)[i] });
}
}
return correlatedWords;
}
示例12: op_Row_int_whenEmpty
public void op_Row_int_whenEmpty()
{
var matrix = new Matrix<string>();
Assert.Throws<ArgumentOutOfRangeException>(() => matrix.Row(0).ToList());
}
示例13: op_Row_intNegative
public void op_Row_intNegative()
{
var matrix = new Matrix<string>();
Assert.Throws<ArgumentOutOfRangeException>(() => matrix.Row(-1).ToList());
}
示例14: op_Row_int
public void op_Row_int()
{
var matrix = new Matrix<string>(3, 3);
matrix[0, 0] = "0,0";
matrix[0, 1] = "0,1";
matrix[0, 2] = "0,2";
matrix[1, 0] = "1,0";
matrix[1, 1] = "1,1";
matrix[1, 2] = "1,2";
matrix[2, 0] = "2,0";
matrix[2, 1] = "2,1";
matrix[2, 2] = "2,2";
for (var y = 0; y < 3; y++)
{
var x = 0;
foreach (var actual in matrix.Row(y))
{
var expected = "{0},{1}".FormatWith(x, y);
Assert.Equal(expected, actual);
x++;
}
}
}
示例15: CalcSquaredError
private void CalcSquaredError(Matrix<double> trainingData, Matrix<double> trainingClasses, out double squaredError, out double percentError)
{
squaredError = 0;
percentError = 0;
for (int i = 0; i < trainingData.RowCount; i++)
{
var a = Classify(trainingData.Row(i));
var expected = trainingClasses.Row(i);
squaredError += (a.Subtract(expected).Select(x => x * x).Sum());
if (expected[a.MaximumIndex()] == 0)
percentError++;
}
percentError /= trainingData.RowCount;
}