numpy使用文件中的数据、图像处理等

来源:互联网 发布:python reshape函数 编辑:程序博客网 时间:2024/06/04 01:01

1、直接在编辑器中查看文件内容


读取文件内容到data变量中:

data = np.loadtxt('data/populations.txt')year, hares, lynxes, carrots = data.T  # trick: columns to variables
再进行绘制:

from matplotlib import pyplot as pltplt.axes([0.2, 0.1, 0.5, 0.8]) plt.plot(year, hares, year, lynxes, year, carrots) plt.legend(('Hare', 'Lynx', 'Carrot'), loc=(1.05, 0.5)) 
输出:

<matplotlib.legend.Legend at 0x10f8bc710>

再进行一些计算,相应的计算可参考前一篇文章

2、读取图片

from IPython.display import ImageImage(filename='images/numpy_image.png')
输出即为一张图片

3、增加一个新的轴,转变为二维数组(np.newaxis)


4、numpy.ogrid函数允许直接创建前面示例的向量x和y,并具有两个“重要维度”:

5、因此,一旦我们必须处理网格上的计算,np.ogrid就非常有用。 另一方面,np.mgrid直接提供充满索引的矩阵:

6、矩阵形状的控制

(1)Flattening(扁平化):如二维数组转化为一维数组,数组转置后再进行扁平化

(2)Reshaping(重塑):

参数为-1时,其对应的维度是推测出来的

7、增加一个维度:

使用np.newaxis对象进行索引可以让我们在一个数组中添加一个轴


8、维度打乱(Dimension shuffling)

9、Resizing

可以使用ndarray.resize更改数组的大小,以0进行填充


10、排序

(1)np.sort(a,axis=1)      axis=1时按行排序,axis=0时按列排序

(2)np.argsort()仍然是排序,但是排序返回的是序列对应索引的下标



原创粉丝点击