Skip to content Skip to sidebar Skip to footer

Python's Random Module Made Inaccessible

When I call random.sample(arr,length) an error returns random_sample() takes at most 1 positional argument (2 given).I've tried importing numpy under a different name, which doesn'

Solution 1:

When you use from numpy import * you import all items that are in the numpy namespace into your scripts namespace. When you do this any functions/variables/etc that have the same name will be overwritten by the items from the numpy namespace.

numpy has a subpackage called numpy.random which then overwrote your random import as the code below shows:

import random

# Here random is the Python stdlib random package.from numpy import *

# As numpy has a random package, numpy.random, # random is now the numpy.random package as it has been overwritten.

In this case you should instead use import numpy as np which will allow you to access both:

import random

# random now contains the stdlib randompackage

import numpy as np

# np.random now contains the numpy.randompackage

Solution 2:

Your points is List type, so you should convert it to an array

points = np.asarray(point)

Also, you should use import random

Post a Comment for "Python's Random Module Made Inaccessible"