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 804289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:53:42+00:00 2026-05-14T23:53:42+00:00

How can a list of vectors be elegantly normalized, in NumPy? Here is an

  • 0

How can a list of vectors be elegantly normalized, in NumPy?

Here is an example that does not work:

from numpy import *

vectors = array([arange(10), arange(10)])  # All x's, then all y's
norms = apply_along_axis(linalg.norm, 0, vectors)

# Now, what I was expecting would work:
print vectors.T / norms  # vectors.T has 10 elements, as does norms, but this does not work

The last operation yields “shape mismatch: objects cannot be broadcast to a single shape”.

How can the normalization of the 2D vectors in vectors be elegantly done, with NumPy?

Edit: Why does the above not work while adding a dimension to norms does work (as per my answer below)?

  • 1 1 Answer
  • 5 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-05-14T23:53:43+00:00Added an answer on May 14, 2026 at 11:53 pm

    Well, unless I missed something, this does work:

    vectors / norms
    

    The problem in your suggestion is the broadcasting rules.

    vectors  # shape 2, 10
    norms  # shape 10
    

    The shape do not have the same length! So the rule is to first extend the small shape by one on the left:

    norms  # shape 1,10
    

    You can do that manually by calling:

    vectors / norms.reshape(1,-1)  # same as vectors/norms
    

    If you wanted to compute vectors.T/norms, you would have to do the reshaping manually, as follows:

    vectors.T / norms.reshape(-1,1)  # this works
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Cosine Similarity article on Wikipedia Can you show the vectors here (in a list
Can anyone explain why I can rebind list but not +? (binding [list vector]
Is there a tool that can list the exact versions and public keys of
I need a control that can list data in 3 columns, I want to
Is there any way that I can list the pages which are currently stored
SELECT name FROM sys.databases -- this can list all database name in the server
I'm having a go at mod_rewrite so that the client can list better urls
Like in Title I want to get position from List<Vector2> positions = new List<Vector2>{/*here
I want to build a TreeModel from some List s that contain the source
I have some vectors of experimental data that I need to massage, for example:

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.