class SparseVector in Recommender API 7.6
Same name and namespace in other branches
- 6.2 Matrix.php \SparseVector
Sparse Vector takes care of missing data.
Hierarchy
- class \Vector
- class \SparseVector
Expanded class hierarchy of SparseVector
3 string references to 'SparseVector'
- Matrix::row_vectors in classes/
Matrix.php - Only return the row vectors that have at least one element. Work for both RealMatrix and SparseMatrix
- Vector::create in classes/
Matrix.php - Factory method to create a vector. Note, no parameter checking. Array index has to be [0..n), otherwise program unstable
- Vector::wrap in classes/
Matrix.php - Factory method. Wrap the array of numbers into the vector. Note: passing by reference!
File
- classes/
Matrix.php, line 294
View source
class SparseVector extends Vector {
// this returns 2 arrays with the same length, both equal to the length of the intersection.
function common_items(&$vector) {
// for compatibility, we don't use pass by reference
//$keys = array_intersect_key(&$this->values, &$vector->values);
$keys = array_intersect_key($this->values, $vector->values);
if (count($keys) == 0) {
return NULL;
}
$array_a = array();
$array_b = array();
foreach ($keys as $key => $value) {
$array_a[] = $this->values[$key];
$array_b[] = $vector->values[$key];
}
$subset = array();
$subset[] = Vector::wrap('RealVector', $array_a);
$subset[] = Vector::wrap('RealVector', $array_b);
return $subset;
}
function covariance(&$vector) {
$subset = $this
->common_items($vector);
return $subset === NULL ? NAN : $subset[0]
->covariance($subset[1]);
}
function correlation(&$vector) {
$subset = $this
->common_items($vector);
if ($subset === NULL) {
return NAN;
}
$covariance = $subset[0]
->covariance($subset[1]);
$std_a = $subset[0]
->std(TRUE);
$std_b = $subset[1]
->std(TRUE);
return $std_a == 0 || $std_b == 0 ? NAN : $covariance / ($std_a * $std_b);
}
// return the mean value of $this, not $vector.
function intersect_mean(&$vector) {
$subset = $this
->common_items($vector);
if ($subset === NULL) {
return NAN;
}
return $subset[0]
->mean(TRUE);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SparseVector:: |
function | |||
SparseVector:: |
function |
Compute correlation with $vector. No caching option.
Works for RealVector. SparseVector needs additional handling. Overrides Vector:: |
||
SparseVector:: |
function |
Compute covariance with $vector. No caching option.
Works for RealVector. SparseVector needs additional handling. Overrides Vector:: |
||
SparseVector:: |
function | |||
Vector:: |
protected | property | ||
Vector:: |
protected | property | ||
Vector:: |
protected | property | ||
Vector:: |
protected | property | ||
Vector:: |
protected | property | ||
Vector:: |
function | Count the number of vectors. This works for SparseVector too. It only counts valid numbers, not the size the vector is supposed to be. | ||
Vector:: |
static | function | Factory method to create a vector. Note, no parameter checking. Array index has to be [0..n), otherwise program unstable | |
Vector:: |
function | |||
Vector:: |
function | Calculate the mean. This works for SparseMatrix too. | ||
Vector:: |
function | |||
Vector:: |
function | |||
Vector:: |
function | Calculate the variance. This works for SparseMatrix too. | ||
Vector:: |
static | function | Factory method. Wrap the array of numbers into the vector. Note: passing by reference! | |
Vector:: |
protected | function |