2016-11-12 60 views
0

的矩阵,正如我在标题中提到的,我无法使其在我的代码中发生。这是我的代码,我尝试使用这两种方法,但最终仍然无法工作。如何使用numpy.random生成int数字

# 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')) 
+1

你应该明确地告诉我们哪一行不行,你得到了什么结果,以及你想得到什么结果。 –

回答

1

为创建随机整数矩阵,你会写

import numpy as np 
x = np.random.randint(low=0,high=100,size=(7,3)) 
print x 

low是可以得出的最小整数,并且high是可以得出的最大整数加一个(即high=100表示可以绘制的最大整数是99)。 size确定将返回的numpy数组的形状。上面的代码的输出(给我的机器上使用的随机种子)是:

array([[15, 97, 2], 
     [88, 3, 6], 
     [64, 97, 13], 
     [18, 44, 75], 
     [ 4, 59, 10], 
     [97, 83, 73], 
     [97, 21, 28]]) 

然后,您可以转换成熊猫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'))