如何在python中获取整数索引?

时间:2017-05-02 03:51:13

标签: python

我想分割数据。但是当我打印训练时,它说split_index应该是整数。我的样本和split_index都是整数,那么我应该在哪里需要整数?

sample = np.arange(10).reshape(10,1)
print sample
print len(sample)
split = 0.7
split_index = floor(len(sample)*split)
print split_index
training = sample[:split_index]
print training

2 个答案:

答案 0 :(得分:1)

import numpy as np
from math import floor
sample = np.arange(10).reshape(10, 1)
print sample
print len(sample)
split = 0.7
split_index = int(floor(len(sample)*split))
print split_index
training = sample[:split_index]
print training

答案 1 :(得分:1)

floor()返回一个浮点数。

  

将x的底限作为float返回,最大整数值小于或等于x。

例如:

>>> floor(5.2)
5.0

您需要将您的floored值转换为整数。

split_index = int(floor(len(sample)*split))