PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 13, 2022

[FIXED] How to store randomly generated lists in python

 August 13, 2022     output, python, save     No comments   

Issue

I am a mathematician with limited programming experience, but am building a neural network to generate curves with certain properties. I've built a training set which looks like a list of 1000 randomly generated numbers between -1 and 1 as follows:

import random

N = 1000     # Number of points in training set
Training_set = []
for i in range(N):
     Training_set.append(random.uniform(-1, 1)) 

This works just fine, but I'd like to save the list Training_set so that I can modify the script and see how the resulting cost function changes with respect to the same training set. Currently, a new training set is generated every time I run the script. I tried writing the above code in a separate python file and importing it to the file where I'm running analysis on the neural network, but to my dismay, a new training set is generated every time even with imports. Copy-pasting is the simplest option in terms of saving randomly generated data, but with such a large list, that seems a bit ridiculous to me (especially if I want to keep scaling up N).

How can I save the data generated in one file somewhere else in a way that it can be called upon again later? This seems like a fairly basic and fundamental thing, I'm sure there's a lot of references out there that address this, but I didn't even know how to search for it. So if someone can point me in the right direction (either explicitly or with a reference), it would be greatly appreciated.


Solution

If you want to have the same values everytime you run the program, you can use random.seed.

import random
random.seed(1) # can be any positive int value

N = 1000     # Number of points in training set
Training_set = []
for i in range(N):
     Training_set.append(random.uniform(-1, 1))

Another option is to export your data to a .csv or .txt, for example, and then read from the file.



Answered By - Amari
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing