Python Forum
NameError: name 'rand' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'rand' is not defined
#1
From the book...
We'll start by creating a random set of 10 points on a two-dimensional plane. Using the standard convention, we'll arrange these in a 10×2 array
import numpy as np
X = rand.rand(10, 2)
print(X)
Error:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-4-ac81983deccd> in <module> 1 import numpy as np 2 import random ----> 3 X = rand.rand(10, 2) 4 print(X) NameError: name 'rand' is not defined
Any idea how to define this second .rand?
Reply
#2
In[9] rand = np.random.RandomState(42)
so, your code would be
import numpy as np
rand = np.random.RandomState(42)
X = rand.rand(10, 2)
print(X)
Output:
[[0.37454012 0.95071431] [0.73199394 0.59865848] [0.15601864 0.15599452] [0.05808361 0.86617615] [0.60111501 0.70807258] [0.02058449 0.96990985] [0.83244264 0.21233911] [0.18182497 0.18340451] [0.30424224 0.52475643] [0.43194502 0.29122914]]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Yeah, it makes sense although number 42 in parenthesis is still a mystery.
Reply
#4
Note, that is if you want the same seed 42 as in the example code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Jun-17-2020, 07:07 PM)Truman Wrote: number 42 in parenthesis is still a mystery.
You know, 42 - The Answer to the Ultimate Question of Life, The Universe, and Everything.
I am sure they deliberately choose it as seed
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
By the way, is there a chance that you also take a look at the topic "Computing the distance between each pair of points". These double colons are still making me headache.

(Jun-17-2020, 07:09 PM)buran Wrote:
(Jun-17-2020, 07:07 PM)Truman Wrote: number 42 in parenthesis is still a mystery.
You know, 42 - The Answer to the Ultimate Question of Life, The Universe, and Everything.
I am sure they deliberately choose it as seed

Yeah, I'll have to ask my numerologist what 42 represents...
Reply
#7
(Jun-17-2020, 07:10 PM)Truman Wrote: Yeah, I'll have to ask my numerologist what 42 represents...
(Jun-17-2020, 07:09 PM)buran Wrote: You know, 42 - The Answer to the Ultimate Question of Life, The Universe, and Everything..
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Jun-17-2020, 07:12 PM)buran Wrote:
(Jun-17-2020, 07:10 PM)Truman Wrote: Yeah, I'll have to ask my numerologist what 42 represents...
(Jun-17-2020, 07:09 PM)buran Wrote: You know, 42 - The Answer to the Ultimate Question of Life, The Universe, and Everything..

The Hitchhiker's Guide to the Galaxy had some good parts and some not that good. Anyway, the author was an atheist so I don't think that I can get those answers that I need from him. Angel
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: name 'predicted_labels' is not defined hobbyist 2 710 Dec-08-2023, 02:18 PM
Last Post: hobbyist
  NameError mlab not defined PythonAndArduino 5 6,947 Nov-08-2017, 06:47 PM
Last Post: nilamo
  Python : NameError: name 'total_wait' is not defined carla 3 5,215 Jun-04-2017, 05:49 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020