python函数

Numpy的 tile() 函数,就是将原矩阵横向、纵向地复制。tile 是瓷砖的意思,顾名思义,这个函数就是把数组像瓷砖一样铺展开来。

mat = array([[1,2], [3, 4]])

横向:
tile(mat, (1, 4))
结果:
在这里插入图片描述
纵向:
tile(mat, (3, 1))
在这里插入图片描述

np.newaxis 为 numpy.ndarray(多维数组)增加一个轴
img[h][w][c]
img[np.newaxis,…]

input_y_image = input_y_image[:, :, np.newaxis]

分离扩展名:os.path.splitext()
获取路径名:os.path.dirname()
获取文件名:os.path.basename()

切片后的变量与原始的数据共享同一数据存储。而这种特点是专门针对大数据的处理而定制的。然而,在日常的使用中总有需要对数据进行拷贝的时候,此时需要的便是显式的数据复制操作copy()。
np.lib.pad
a = [1, 2, 3, 4, 5]
np.lib.pad(a, (2,3), ‘constant’, constant_values=(4, 6))
array([4, 4, 1, 2, 3, 4, 5, 6, 6, 6])
pad_width是在各维度的各个方向上想要填补的长度,如((2,3),(4,5)),如果直接输入一个整数,则说明各个维度和各个方向所填补的长度都一样。
image = np.lib.pad(image, ((0, h_align_8 - src_h), (0, w_align_8 - src_w), (0, 0)), ‘constant’,
constant_values=(0, 0))
表示三个维度,每个维度的before与 after 。分别填充值 constant_values=(0, 0))

squeeze 函数:从数组的形状中删除单维度条目,即把shape中为1的维度去掉
用法:numpy.squeeze(a,axis = None)
1)a表示输入的数组;
2)axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
3)axis的取值可为None 或 int 或 tuple of ints, 可选。若axis为空,则删除所有单维度的条目;
4)返回值:数组
5) 不会修改原数组;

合并数组
y = y[:, :, np.newaxis]
cb = cb[:, :, np.newaxis]
cr = cr[:, :, np.newaxis]
yuv_image = np.concatenate((y, cb, cr),axis=2)

注意在python2.7 里,浮点数必须使用 min_mse_num = 0.0,否则出现意想不到的错误。

python程序安装:
快速查找Python安装路径方法

import sys
print(sys.path)
在这里插入图片描述
在目录python36目录执行安装
cd C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts
如果下面的程序安装出错,可以考虑把程序重新安装一次。

安装PIL
pip install Pillow // from PIL import Image
安装pyautogui
pip install pyautogui
安装numpy
pip install numpy
安装cv2
pip install opencv-python
sudo apt-get install python3-pip
sudo pip3 install opencv-python
安装matplotlib
pip install matplotlib

map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回。
lines = open(win_offset_file).readlines()
for line in lines:
str1 = line.rstrip()
#print(str1)
curLine=line.strip().split(" ")
#print(curLine)
w_h=map(int, curLine)
print(w_h)

def f(x):
return x*x
print map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
输出结果:
[1, 4, 9, 10, 25, 36, 49, 64, 81]

切片:
input_y_image = image[:, :, 0]

np.prod()函数用来计算所有元素的乘积

flatten函数

>> a = [[1,3],[2,4],[3,5]]  
>>> a = mat(a)  
>>> y = a.flatten()  
>>> y  
matrix([[1, 3, 2, 4, 3, 5]])  
>>> y = a.flatten().A  
>>> y  
array([[1, 3, 2, 4, 3, 5]])  
>>> shape(y)  
(1, 6)  
>>> shape(y[0])  
(6,)  
>>> y = a.flatten().A[0]  
>>> y  
array([1, 3, 2, 4, 3, 5]) 

from matplotlib import pyplot as plt
plt.plot([5,6,7,8],[7,3,8,3])
plt.show()

from matplotlib import pyplot as plt
plt.scatter([5,6,7,8],[7,3,8,3])
plt.show()

#frombuffer将data以流的形式读入转化成ndarray对象
import numpy as np
s = b’Hello World’
a = np.frombuffer(s, dtype = ‘S1’)
print(a) #[b’H’ b’e’ b’l’ b’l’ b’o’ b’ ’ b’W’ b’o’ b’r’ b’l’ b’d’]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风口上的传奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值