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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:20:41+00:00 2026-06-08T03:20:41+00:00

I have a 3000×300 matrix file (float). when I read and convert to float,

  • 0

I have a 3000×300 matrix file (float). when I read and convert to float, I am getting float64, which is default in python. I tried numpy and map() to convert it to float32() but they both seem very inefficient.

my code:

x = open(readFrom, 'r').readlines()
y = [[float(i) for i in s.split()] for s in x]

time taken: 0:00:00.996000

numpy implementation:

x = open(readFrom, 'r').readlines()
y = [[np.float32(i) for i in s.split()] for s in x]

time taken: 0:00:06.093000

map()

x = open(readFrom, 'r').readlines()
y = [map(np.float32, s.split()) for s in x]

time taken: 0:00:05.474000

How can I convert to float32 very efficiently?

Thank you.

Update:

numpy.loadtxt() or numpy.genfromtxt() not working (giving memory error) for huge file. I have posted a question related to that and the approach I presented here works well for huge matrix file (50,000×5000). here is the question

  • 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-08T03:20:43+00:00Added an answer on June 8, 2026 at 3:20 am

    If memory is a problem, and if you know the size of the field ahead of time, you probably don’t want to read the entire file in the first place. Something like this is probably more appropriate:

    #allocate memory (np.empty would work too and be marginally faster, 
    #                 but probably not worth mentioning).
    a=np.zeros((3000,300),dtype=np.float32)  
    with open(filename) as f:
        for i,line in enumerate(f):
            a[i,:]=map(np.float32,line.split()) 
    

    from a couple quick (and surprising) tests on my machine, it appears that the map may not even be necessary:

    a=np.zeros((3000,300),dtype=np.float32)  
    with open(filename) as f:
        for i,line in enumerate(f):
            a[i,:]=line.split() 
    

    This might not be the fastest, but certainly it’ll be the most memory efficient way to do it.

    Some tests:

    import numpy as np
    
    def func1():   #No map -- And pretty speedy :-).
        a=np.zeros((3000,300),dtype=np.float32)
        with open('junk.txt') as f:
            for i,line in enumerate(f):
                a[i,:]=line.split()
    
    def func2():
        a=np.zeros((3000,300),dtype=np.float32)
        with open('junk.txt') as f:
            for i,line in enumerate(f):
                a[i,:]=map(np.float32,line.split())
    
    def func3():
        a=np.zeros((3000,300),dtype=np.float32)
        with open('junk.txt') as f:
            for i,line in enumerate(f):
                a[i,:]=map(float,line.split())
    
    import timeit
    
    print timeit.timeit('func1()',setup='from __main__ import func1',number=3)  #1.36s
    print timeit.timeit('func2()',setup='from __main__ import func2',number=3)  #11.53s
    print timeit.timeit('func3()',setup='from __main__ import func3',number=3)  #1.72s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which needs to hold approximately 3000 open file descriptors in
I have a link which goes to the default edit page. http://127.0.0.1:3000/calendars/16/edit I want
I have around 3000 SWF files, some are interactive which has lots of actionscripts
i have a list like this [peter,1000,michell,2000,kelly,3000] and i would like to convert to
I have an access 2007 table with about 3000 records, to which I have
I have a string which has say 3000 characters, now I want to split
I have a file collection (3000 files) in a FileInfoCollection. I want to process
I have a matrix and a vector each with 3000 rows: fe = [-0.1850
I have a database which consists of almost 200 tables and 3000 stored procedures.
I have a site which contains around 3000 static and dynamic pages. I think

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.