[英]how to use numpy.random to generate a matrix of int number
as I have mentioned in title, I fail to make it happened in my code. 正如我在标题中提到的那样,我未能在代码中实现它。 Here is my code, I try to use the two methods, but eventually it still cannot work. 这是我的代码,我尝试使用这两种方法,但最终仍然无法正常工作。
# using python 2.7.12
import pandas as pd
dates = pd.date_range('20141001', periods = 7)
import numpy as np
dates1 = pd.DataFrame(np.random.randn(7,3), index =dates, columns = list('ABC'))
print dates1
dates2 = pd.DataFrame(np.random.randint(0,100), index =dates, columns = list('ABC'))
To create a matrix of random integers, you would write 要创建随机整数矩阵,您需要编写
import numpy as np
x = np.random.randint(low=0,high=100,size=(7,3) )
print x
low
is the minimum integer that can be drawn, and high
is the maximum integer plus one that can be drawn (ie high=100
means the maximum integer that can be drawn is 99). low
是可以绘制的最小整数, high
是最大整数加上可以绘制的整数(即high=100
表示可以绘制的最大整数为99)。 size
determines the shape of the numpy array that will be returned. size
确定将返回的numpy数组的形状。 The output of the above code (given the random seed used on my machine) is: 上面代码的输出(考虑到我的机器上使用的随机种子)是:
array([[15, 97, 2],
[88, 3, 6],
[64, 97, 13],
[18, 44, 75],
[ 4, 59, 10],
[97, 83, 73],
[97, 21, 28]])
You can then cast this into a pandas DataFrame
as you were before: 然后,您可以像以前一样将其转换为pandas DataFrame
:
import numpy as np
import pandas as pd
dates2 = pd.DataFrame(np.random.randint(low=0,high=100,size=(7,3)),\
index =dates, columns = list('ABC'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.