这里的代码是截取的我的代码片段,或许难以阅读,有不理解的地方欢迎交流
- 生成空列表及末尾添加
x=[]
x.append(img_path[j])
- 图像矩阵和一维数组转化
img_ndarray=numpy.asarray(img,dtype='float64')/256 #将图像转化为数组并将像素转化到0-1之间
data[d-1]=numpy.ndarray.flatten(img_ndarray) #将图像的矩阵形式转化为一维数组保存到data中
- 将矩阵中浮点数转化为int类型
data_label=data_label.astype(numpy.int) #将标签转化为int类型
- Python生成一个范围内的随机整数。其中参数a是下限,参数b是上限
print random.randint(12, 20) #生成的随机数n: 12 <= n <= 20
numpy教程
y = 2.5
print type(y) # 输出 "<type 'float'>"
print y ** 2 # 输出 "6.25"
#Python中没有 x++ 和 x-- 的操作符。
#布尔型:Python用英语实现了所有的布尔逻辑,而不是操作符(&&和||等)。
t = True
f = False
print type(t) # 输出 "<type 'bool'>"
print t and f # 逻辑与 AND; prints "False"
print t or f # 逻辑或 OR; prints "True"
print not t # 逻辑非 NOT; prints "False"
print t != f # 逻辑异或 XOR; prints "True"
#字符串:Python对字符串的支持非常棒。
hello = 'hello' # 字符串可以用单引号,也可以用双引号
world = "world"
print hello # Prints "hello"
print len(hello) # 字符串长度 prints "5"
hw = hello + ' ' + world # 字符串连接
print hw # prints "hello world"
hw12 = '%s %s %d' % (hello, world, 12) # 格式化输出
print hw12 # prints "hello world 12"
#字符串对象有一系列有用的方法,比如:
s = "hello"
print s.capitalize()