我创建了一个python脚本,其中有4个空列表,代码在这些列表上创建一个随机数,最后将其保存为Excel文件。问题在于它用浮点数创建数字,我只想要整数!有人能帮助我吗?
这是我的代码:
import numpy as np
import openpyxl
import random
from openpyxl import Workbook
# Create workbook object
wb = openpyxl.Workbook()
sheet = wb.get_active_sheet()
sheet.title = 'Sheet #1'
# Generate data
Timestamp = [0, 1, 2, 3, 4, 5, 6, 7,8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
Subtotal_vest = []
Subtotal_NorthEast = []
Subtotal_south = []
Others = []
for i in Timestamp:
Subtotal_vest.append(random.gauss(3640, 25)),
Subtotal_NorthEast.append(random.gauss(3832, 25)),
Subtotal_south.append(random.gauss(2592, 25)),
Others.append(random.gauss(1216, 25)),
#y = x * x
# Add titles in the first row of each column
sheet.cell(row=1, column=1).value = 'Timestamp'
sheet.cell(row=1, column=2).value = 'Vest'
sheet.cell(row=1, column=3).value = 'North east'
sheet.cell(row=1, column=4).value = 'South'
sheet.cell(row=1, column=5).value = 'Others'
#sheet.cell(row=1, column=2).value = 'Y values'
# Loop to set the value of each cell
for inputs in range(0, len(Timestamp)):
sheet.cell(row=inputs + 2, column=1).value = Timestamp[inputs]
sheet.cell(row=inputs + 2, column=2).value = Subtotal_vest[inputs]
sheet.cell(row=inputs + 2, column=3).value = Subtotal_NorthEast[inputs]
sheet.cell(row=inputs + 2, column=4).value = Subtotal_south[inputs]
sheet.cell(row=inputs + 2, column=5).value = Others[inputs]
# Finally, save the file and give it a name
wb.save('Excel/Matrix.xlsx')
答案 0 :(得分:2)
把它扔到那里作为另一种选择。对于任意分布,您可以创建其累积分布函数(CDF),然后从统一随机分布中抽取该样本百分位数。然后,您需要将它们转换为整数,但这可以让您从任何所需的分布中进行绘制!
from scipy.stats import norm
import matplotlib.pyplot as plt
# Replace `mean` and `std` below with your existing means and standard deviations.
draw = norm.ppf(np.random.random(1000), loc=mean, scale=std) # 1000 samples, for example
plt.hist(draw.astype(int))
这种方法的一个好处是它适用于您可以创建CDF的任何发行版;即使你只能用数据来定义数字!
或者,您可以使用二项分布逼近正态分布,二项分布是离散的。一般来说,B(n, p) ~ N(n*p, sqrt(n*p*(1-p)))
:
draw = np.random.binomial(n, p, size=1000)
您必须设置n
和p
,才能从原始均值和标准差中求解n*p = mean
和sqrt(n*p*(1-p)) = std
。
答案 1 :(得分:1)
或者您可以使用numpy.random.normal():
import numpy as np
Timestamp = [0, 1, 2, 3, 4, 5, 6, 7,8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
timestamps = len(Timestamp)
Subtotal_vest = np.random.normal(3640, 25, timestamps).astype(int)
Subtotal_NorthEast = np.random.normal(3832, 25, timestamps).astype(int)
Subtotal_south = np.random.normal(2592, 25, timestamps).astype(int)
Others = np.random.normal(1216, 25, timestamps).astype(int)
答案 2 :(得分:0)
如果您对使用统一分布绘制的随机数字感到满意,则可以使用random.randint
代替random.gauss
。
答案 3 :(得分:0)
您是否尝试使用int(random.gauss(...))
?
答案 4 :(得分:0)
将random.gauss返回的值转换为int将执行:
import numpy as np
import openpyxl
import random
from openpyxl import Workbook
# Create workbook object
wb = openpyxl.Workbook()
sheet = wb.get_active_sheet()
sheet.title = 'Sheet #1'
# Generate data
timestamp = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23
]
subtotal_vest = []
subtotal_north_east = []
subtotal_south = []
others = []
for i in timestamp:
subtotal_vest.append(int(random.gauss(3640, 25))),
subtotal_north_east.append(int(random.gauss(3832, 25))),
subtotal_south.append(int(random.gauss(2592, 25))),
others.append(int(random.gauss(1216, 25))),
#y = x * x
# Add titles in the first row of each column
sheet.cell(row=1, column=1).value = 'Timestamp'
sheet.cell(row=1, column=2).value = 'Vest'
sheet.cell(row=1, column=3).value = 'North east'
sheet.cell(row=1, column=4).value = 'South'
sheet.cell(row=1, column=5).value = 'Others'
#sheet.cell(row=1, column=2).value = 'Y values'
# Loop to set the value of each cell
for inputs in range(0, len(timestamp)):
sheet.cell(row=inputs + 2, column=1).value = timestamp[inputs]
sheet.cell(row=inputs + 2, column=2).value = subtotal_vest[inputs]
sheet.cell(row=inputs + 2, column=3).value = subtotal_north_east[inputs]
sheet.cell(row=inputs + 2, column=4).value = subtotal_south[inputs]
sheet.cell(row=inputs + 2, column=5).value = others[inputs]
# Finally, save the file and give it a name
wb.save('Matrix.xlsx')
答案 5 :(得分:0)
替换
Subtotal_vest = []
Subtotal_NorthEast = []
Subtotal_south = []
Others = []
for i in Timestamp:
Subtotal_vest.append(random.gauss(3640, 25)),
Subtotal_NorthEast.append(random.gauss(3832, 25)),
Subtotal_south.append(random.gauss(2592, 25)),
Others.append(random.gauss(1216, 25)),
带
Subtotal_vest = [int(round(random.gauss(3640, 25))) for _ in Timestamp]
Subtotal_NorthEast = [int(round(random.gauss(3832, 25))) for _ in Timestamp]
Subtotal_south = [int(round(random.gauss(2592, 25))) for _ in Timestamp]
Others = [int(round(random.gauss(1216, 25))) for _ in Timestamp]
round
舍入到最接近的整数&然后int
将浮点数转换为整数。
答案 6 :(得分:0)
使用int(rd.gauss(mu,sigma))。
大卫
答案 7 :(得分:0)
使用random.randint(0,10)
代替random.gauss
。 random.randint()生成随机整数值。