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

  • Home
  • SEARCH
  • 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 6783769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:54:36+00:00 2026-05-26T16:54:36+00:00

I am using Numpy to store data into matrices. Coming from R background, there

  • 0

I am using Numpy to store data into matrices. Coming from R background, there has been an extremely simple way to apply a function over row/columns or both of a matrix.

Is there something similar for python/numpy combination? It’s not a problem to write my own little implementation but it seems to me that most of the versions I come up with will be significantly less efficient/more memory intensive than any of the existing implementation.

I would like to avoid copying from the numpy matrix to a local variable etc., is that possible?

The functions I am trying to implement are mainly simple comparisons (e.g. how many elements of a certain column are smaller than number x or how many of them have absolute value larger than y).

  • 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-05-26T16:54:36+00:00Added an answer on May 26, 2026 at 4:54 pm

    Almost all numpy functions operate on whole arrays, and/or can be told to operate on a particular axis (row or column).

    As long as you can define your function in terms of numpy functions acting on numpy arrays or array slices, your function will automatically operate on whole arrays, rows or columns.

    It may be more helpful to ask about how to implement a particular function to get more concrete advice.


    Numpy provides np.vectorize and np.frompyfunc to turn Python functions which operate on numbers into functions that operate on numpy arrays.

    For example,

    def myfunc(a,b):
        if (a>b): return a
        else: return b
    vecfunc = np.vectorize(myfunc)
    result=vecfunc([[1,2,3],[5,6,9]],[7,4,5])
    print(result)
    # [[7 4 5]
    #  [7 6 9]]
    

    (The elements of the first array get replaced by the corresponding element of the second array when the second is bigger.)

    But don’t get too excited; np.vectorize and np.frompyfunc are just syntactic sugar. They don’t actually make your code any faster. If your underlying Python function is operating on one value at a time, then np.vectorize will feed it one item at a time, and the whole
    operation is going to be pretty slow (compared to using a numpy function which calls some underlying C or Fortran implementation).


    To count how many elements of column x are smaller than a number y, you could use an expression such as:

    (array['x']<y).sum()
    

    For example:

    import numpy as np
    array=np.arange(6).view([('x',np.int),('y',np.int)])
    print(array)
    # [(0, 1) (2, 3) (4, 5)]
    
    print(array['x'])
    # [0 2 4]
    
    print(array['x']<3)
    # [ True  True False]
    
    print((array['x']<3).sum())
    # 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
I am trying to read in data from a text file using numpy.loadtxt with
Using NumPy , a matrix A has n rows and m columns, and I
Basically, what is the best way to go about storing and using dense matrices
Has anyone tried compiling SciPy 0.7.1 on Windows using numpy-1.3.0 that was built with
I am trying to convert a PIL image into an array using NumPy. I
I'm using a numpy object_ array to store variable length strings, e.g. a =
I'm trying to add two images together using NumPy and PIL. The way I
I've been using Numpy's numpy.random.exponential function for a while. I now see that Python's
I am using numpy and I want to store the result of a calculation

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.