首 页 行业热点 新车 试驾评测 养车用车 车型库

python相关 random.rand

发布网友

我来回答

5个回答

热心网友

这是使用的numpy模块中的随机函数,应该先导入这个模块:

from numpy import *

然后就可以使用你的random.rand(4,4)了。

不过,不建议这样导入,有可能与导入的其它模块产生冲突,建议:

import numpy as np
np.random.rand(4,4)

热心网友

首先安装numpy库

from numpy import *

热心网友

付费内容限时免费查看回答random指随机的意思有,三种写法:

第一种:

import random #导入random模块

random.randint(1,100) #从1到100中取一个整数

第二种:

import random #导入random模块

random.uniform(1,100) #从1到100中取一个小数

第二种:

import random #导入random模块

a=['1','2','3']#列表

random.choice(a) #从a中取一个整数

这些可以变成:

import random #导入random模块

a=random.randint(1,100) #从1到100中取一个整数

print(a)#把从1到100中取一个整数输出

import random #导入random模块

print(random.randint(1,100)) #从1到100中取一个整数并输出

热心网友

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 1.2.0 -- An enhanced Interactive Python.
? -> Introction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.

In [1]: random.rand(4,4)
Out[1]:
array([[ 0.76744987, 0.61668256, 0.06223992, 0.74588473],
[ 0.3084096 , 0.09108339, 0.52193784, 0.28454493],
[ 0.82903201, 0.49345398, 0.25438691, 0.65500096],
[ 0.736372, 0.78418204, 0.23357709, 0.760083]])

热心网友

python标准 random 库里根本没有 rand 这个函数,从你贴的图看,似乎是随机产生4x4的随机数组,而图上的打印也不符合标准 array 库的定义。 要么你看的是一本神书,不知道谁乱写的,要么他里面用到的 random.rand 是自己写的一个函数,可以肯定不是python标准库。追问我看的是《机器学习实战》,他就这么写的...就第一章,卡住了

追答可能这本书写的太早了,用的是很多年前的python,也许那时候的python是这么用的(我也不确定)。你看一下书里面有没有讲它用的是什么版本的python,如果没有,那只能建议你看看能不能找到这本书的最新版本(很可能是英文版),也有可能是翻译的人搞错了。不管怎么样,在现有的python中,上面的代码是完全不能存在的。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com