Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:01:12+00:00 2026-06-15T19:01:12+00:00

Good afternoon. I am faced with a PCA task which simply involves reducing the

  • 0

Good afternoon.

I am faced with a PCA task which simply involves reducing the dimensionality of a vector. I’m not interested in a two-dimensional matrix in this case, but merely a D-dimensional vector which I would like to project along it’s K principal eigenvectors.

In order to implement PCA, I need to retrieve the covariance matrix of this vector. Let’s try to do this on an example vector:

someVec = np.array([[1.0, 1.0, 2.0, -1.0]])

I’ve defined this vector as a 1 X 4 matrix, i.e a row vector, in order to make it compatible with numpy.cov. Taking the covariance matrix of this vector through numpy.cov will yield a scalar covariance matrix, because numpy.cov makes the assumption that the features are in the rows:

print np.cov(someVec)
1.58333333333

but this is (or rather, should be) merely a difference in dimensionality assumptions, and taking the covariance of the transpose vector should work fine, right? Except that it doesn’t:

print np.cov(someVec.T)
/usr/lib/python2.7/site-packages/numpy/lib/function_base.py:2005: RuntimeWarning:                  
invalid value encountered in divide
return (dot(X, X.T.conj()) / fact).squeeze()
[[ nan  nan  nan  nan]
[ nan  nan  nan  nan]
[ nan  nan  nan  nan]
[ nan  nan  nan  nan]]

I’m not exactly sure what I’ve done wrong here. Any advice?

Thanks,

Jason

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:01:13+00:00Added an answer on June 15, 2026 at 7:01 pm

    If you want to pass in the transpose, you’ll need to set rowvar to zero.

    In [10]: np.cov(someVec, rowvar=0)
    Out[10]: array(1.5833333333333333)
    
    In [11]: np.cov(someVec.T, rowvar=0)
    Out[11]: array(1.5833333333333333)
    

    From the docs:

    rowvar : int, optional

    If rowvar is non-zero (default), then each row
    represents a variable, with observations in the columns. Otherwise,
    the relationship is transposed: each column represents a variable,
    while the rows contain observations.

    If you want to find a full covariance matrix, you’ll need more than one observation. With a single observation, and numpy’s default estimator, NaN is exactly what you’d expect. If you would like to have normalization done by N instead of (N-1), you can pass in a 1 to the bias.

    In [12]: np.cov(someVec.T, bias=1)
    Out[12]:
    array([[ 0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.]])
    

    Again, from the docs.

    bias : int, optional

    Default normalization is by (N – 1), where N is
    the number of observations given (unbiased estimate). If bias is 1,
    then normalization is by N. These values can be overridden by using
    the keyword ddof in numpy versions >= 1.5.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good afternoon In Java, I have HashSet which contain list of Object User which
Good afternoon, i am currently wrestling with an old .dll which functionality I have
Good afternoon all, I have a class which looks like this: public class Grapheme
Good afternoon, I have a C++ class Range which implements a operator < for
Good Afternoon, I have a an Excel template file - which contains Macros -
Good afternoon Overflowers! ;) What I want to do: I'm interested in verifying transferred
Good afternoon, I have a doubt. I have a div with two input button
Good afternoon, I'm having serious troubles getting my database back up and running. I
Good Afternoon all , I have downloaded data from a web service and im
Good afternoon, We are using the latest C/C++ version of PCRE on WINDOWS Visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.