code: def pca_reduce(X: Union[AnyNDArray, tf.Tensor], Q: int) -> AnyNDArray:
      """
      A helpful function for linearly reducing the dimensionality of the data X
      to Q.
      :param X: data array of size N (number of points) x D (dimensions)
      :param Q: Number of latent dimensions, Q < D
      :return: PCA projection array of size N x Q.
      """
      if Q > X.shape[1]:  # pragma: no cover
          raise ValueError(
              "Cannot have more latent dimensions than observed"
          )
      if isinstance(X, tf.Tensor):
          X = X.numpy()
      assert isinstance(X, np.ndarray)  # Hint for numpy
      # TODO why not use tf.linalg.eigh?
      evals, evecs = np.linalg.eigh(np.cov(X.T))
      W = evecs[:, -Q:]
      return cast(AnyNDArray, (X - X.mean(0)).dot(W))
  
file_path: GPflow/tests/gpflow/utilities/test_ops.py
imports: from typing import Union, cast
  import numpy
  import pytest
  import tensorflow
  from check_shapes import check_shapes
  import gpflow
  from gpflow.base import AnyNDArray
  from gpflow.utilities.ops import difference_matrix
label_desc: Use image node_id 1 for calling a global function with example usage: pca_reduce(X, Q) and returns: cast