<div class="ExternalShit23429812"><p>This is a paragraoph.. </p><br><p>What</p><p>Yeah right</p><p>Paragraph</p><p>and Paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph yes yes yes</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p></div>
这是我的代码,如果import numpy as np
import sklearn
import sklearn.datasets
from sklearn import svm
x = np.array([1,3,67,8])
print(x)
print(type(x))
if type(x) != int:
y = x.astype(int)
print(y)
print(type(y))
else:
print ("X is already an integer")
不是整数然后将其转换为整数,则将其打印为整数但奇怪的是即使x
是if
,x
语句中的代码也会执行整数或浮点数。
答案 0 :(得分:1)
我相信这就是你要找的东西。要检查值是否为整数(即使在float
数组中),您可以测试x == int(x)
。
import numpy as np
arr = np.array([1, 3, 67, 8, 7.5])
print(arr, type(arr))
for x in arr:
if x != int(x):
y = x.astype(int)
print(y, type(y))
else:
print(str(int(x)) + ' is already an integer')
# [ 1. 3. 67. 8. 7.5] <class 'numpy.ndarray'>
# 1 is already an integer
# 3 is already an integer
# 67 is already an integer
# 8 is already an integer
# 7 <class 'numpy.int32'>