python reshape shape astype 理解

x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
from numpy import *
l = zeros((5,4))#构建一个5*4的零矩阵
for i in range(5):#给该矩阵赋值
    for j in range(4):
        l[i][j] = i * 5 + j
print(l)#打印赋值后的矩阵
print(shape(l))#输出l的行列值
print(l.shape[0])#输出l的行数值
print(l.shape[1])#输出l的列数值

python 图像读入 reshape尺寸时的问题

#coding=utf-8
import matplotlib.pyplot as plt
import matplotlib.image as mimage


image=mimage.imread('lala.jpg')

print image.shape
# show a picture
image=image.reshape(1,-1)  
#-1是根据数组大小进行维度的自动推断

 #若使用的是image=image.reshape成一行,分别为R一块, G块 ,B一块
# t=imgX1[222,:].reshape(3,32,32)
# print('t=  ' ,t.shape)
# image=np.transpose(t,(1,2,0))
image=image.reshape(1186,1920,3)

print(image.shape)

plt.imshow(image)
plt.axis('off')
plt.show()

 

/ 如果将浮点数转换为整数,则小数部分会被截断
In [7]: arr2 = np.array([1.1, 2.2, 3.3, 4.4, 5.3221])

In [8]: arr2
Out[8]: array([ 1.1   ,  2.2   ,  3.3   ,  4.4   ,  5.3221])

// 查看当前数据类型
In [9]: arr2.dtype
Out[9]: dtype('float64')

// 转换数据类型  float -> int
In [10]: arr2.astype(np.int32)
Out[10]: array([1, 2, 3, 4, 5], dtype=int32)
数据类型转换
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
#数据归一化(0,1)
x_train /= 255
x_test /= 255

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值