其他函数
1:map函数和lambda函数
map(function,x) 会将x中的元素依次代入到function中执行,并将所有返回结果以列表形式返回。
map(lambda,x) 同理,如下将[1,2,3]中的元素依次代入到lambda函数中,返回原列表元素+1的列表。
list(map(lambda x: x+1 ,[1,2,3]))
[2, 3, 4]
2:reduce函数
reduce(function,sequence[,inital]), reduce函数第一个参数是函数,第二个参数是序列。
与map不同的是 其function必须有两个输入参数。
map()是将传入的函数依次作用到序列的每个元素,每个元素都是独自被函数“作用”一次;
reduce()是将传人的函数作用在序列的第一个元素得到结果后,把这个结果继续与下一个元素作用(累积计算),最终结果是所有的元素相互作用的结果。
import functools
functools.reduce(lambda x,y : x+y,[1,2,3])
6
numpy库函数
import numpy as np
1: hstack()
hstack() 是将两个数组在水平方向上平铺,hstack(tup) 其中tup是数组序列,比如 [a1,a2] 或者(a1,a2)
npArray1 = np.array([1,2,3])
npArray2 = np.array([4,5,6])
npArray3 = np.hstack([npArray1,npArray2])
print(npArray3)
[1 2 3 4 5 6]
a1 = np.array([[1,2],[3,4],[5,6]])
a2 = np.array([[7,8],[9,10],[11,12]])
a3 = np.hstack((a1,a2))
print(a3)
[[ 1 2 7 8]
[ 3 4 9 10]
[ 5 6 11 12]]
2: ones_like()
返回一个用1填充的跟输入 形状和类型 一致的数组。
a = np.array([[1,2,3],[4,5,6]])
b = np.ones_like(a)
print(b)
[[1 1 1]
[1 1 1]]
3: reshape()
将数组重新组织
a = np.array([[1,2,3],[4,5,6]])
print(a.reshape((3,2)))
print(a.reshape(3,2))
print(a.reshape(-1,1)) # reshape(-1,1)将数据变成一列(6,1)
print(a.reshape(1,-1)) # resape(1,-1)将数据变成一行(1,6)
print(a.reshape(1,2,3,1))
[[1 2]
[3 4]
[5 6]]
[[1 2]
[3 4]
[5 6]]
[[1]
[2]
[3]
[4]
[5]
[6]]
[[1 2 3 4 5 6]]
[[[[1]
[2]
[3]]
[[4]
[5]
[6]]]]
4: random.seed()
当seed()没有参数时,每次生成的随机数是不一样的,而当seed()有参数时,每次生成的随机数是一样的,同时选择不同的参数生成的随机数也不一样
np.random.seed()
print(np.random.random())
np.random.seed()
print(np.random.random())
np.random.seed()
print(np.random.random())
0.21456334543569455
0.013497756168870723
0.01829729448561923
np.random.seed(1)
print(np.random.random())
np.random.seed(1)
print(np.random.random()) #相同的种子产生的随机数相同
np.random.seed(2)
print(np.random.random()) #不同的种子产生的随机数不同
np.random.seed() #没有参数,也会产生不同的随机数
print(np.random.random()) #不设置种子,也会产生不同的随机数b
print(np.random.random())
0.417022004702574
0.417022004702574
0.43599490214200376
0.5442646011037954
0.5025557694083344
5:dot()
np.dot(x,y) 或者 x.dot(y) 是将两个ndarray 类型的数组 像矩阵那样相乘,返回结果仍然是个ndarray
如果x,y 都是矩阵类型的 就可以直接x*y ,但是如果是numpy数组类型的就得使用dot来达到这样的效果
a = np.array([[1,2,3],[4,5,6]]) #(2,3)
b = np.array([[2,3],[4,5],[5,7]]) #(3,2)
print(np.dot(a,b))
print(a.dot(b))
a = np.matrix(a)
b = np.matrix(b)
print(a*b)
[[25 34]
[58 79]]
[[25 34]
[58 79]]
[[25 34]
[58 79]]
6. astype()
astype:转换数组的数据类型。
int32 --> float64 完全ojbk
float64 --> int32 会将小数部分截断
string_ --> float64 如果字符串数组表示的全是数字,也可以用astype转化为数值类型
a = np.array([1.,2.,3.])
a.astype(np.int)
#将float转换为了int
array([1, 2, 3])
7.ravel()
将多维数组转换为一维数组
a = np.array([[1,2,2,3],[2,3,4,5]])
a.ravel()
array([1, 2, 2, 3, 2, 3, 4, 5])
8.random.random 和 random.rand
相同点:两个函数都是在 [0, 1) 的均匀分布中产生随机数。
不同点:参数传递不同。random.random( )接收一个单独的元组,而random.rand( )接收分开的参数
print(np.random.rand(3, 5)) #rand 参数为两个或一个分开的参数
print(np.random.random((3, 5))) #random 参数为元组
[[0.83369757 0.98877995 0.44819364 0.32756853 0.60474976]
[0.27031797 0.79104183 0.05950835 0.29772637 0.39860645]
[0.90326045 0.97684587 0.1943576 0.51744657 0.42567931]]
[[0.65440587 0.0932283 0.14584034 0.08337943 0.88244716]
[0.71498533 0.62923749 0.12127219 0.4071704 0.65591151]
[0.27018508 0.02960932 0.06791291 0.96881022 0.87789949]]
9.meshgrid()
此函数用于生成网格点坐标矩阵
X的每一行都一样,Y的每一列都一样。基于这种强烈的规律性,numpy提供的numpy.meshgrid()函数可以让我们快速生成坐标矩阵X,Y。
假设x的长度为m,y的长度为n,meshgrid(x,y)就会生成X,Y 的shape都为 (n,m)
更详细解释在 https://blog.csdn.net/lllxxq141592654/article/details/81532855
x = np.linspace(0,3,4)
y = np.linspace(0,2,3)
X,Y = np.meshgrid(x,y)
import matplotlib.pyplot as plt
plt.plot(X, Y,
marker='.', # 点的形状为圆点
linestyle='') # 线型为空,也即点与点之间不用线连接
plt.grid(True) #背景显示网格
plt.show()
<Figure size 640x480 with 1 Axes>
10.newaxis
numpy.newaxis 是在所写的位置增加一维
x = np.ones((4,1)) # 现在x的shape是(4,1)
newX = x[:,:,np.newaxis] #在最右侧增加一维后,就变成了(4,1,1)
print(newX.shape)
newX2 = x[np.newaxis,:,:] #在最左侧增加一维后,就变成了(1,4,1)
print(newX2.shape)
newX3 = x[:,np.newaxis,:]#(4,1,1)
print(newX3.shape)
(4, 1, 1)
(1, 4, 1)
(4, 1, 1)
11. numpy.sum
sum(X, axis = 0,keepdims = False)
axis = 0 对于一个二维数组来说,是列相加; axis = 1是行相加;axis 不写就是所有数字相加
keepdims = False 是相加后不保存原维度; keepdims = True 是相加后保持原维度; 默认为False
a = [[1,2,3],
[4,5,6],
[7,8,9]]
np.sum(a)
45
np.sum(a, axis = 0)
array([12, 15, 18])
np.sum(a, axis = 0, keepdims= True)
array([[12, 15, 18]])
np.sum(a, axis = 1)
array([ 6, 15, 24])
np.sum(a, axis = 1, keepdims= True)
array([[ 6],
[15],
[24]])
12. gradient(z)
计算n维数组的梯度,返回和原始数组同样大小的结果。
对于一维数组,其最左边的元素使用一阶向前差分,也就是:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wr5ldO4T-1585117654209)(attachment:image.png)]
其最右边的元素使用一阶向后差分:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D5YBEthg-1585117654211)(attachment:image.png)]
中间元素使用一阶中心差分:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7XHrimDd-1585117654211)(attachment:image.png)]
举例来说,[1,2,3,4] 的梯度
最左边第一个结果 = 2-1 =1;最右边的结果 = 4-3 =1
第二个元素(2对应的位置)也就是中间的元素的结果 =( 3 - 1)/2 = 1
第三个元素(3对应的位置)的结果 = (4 -2)/2 = 1。
所以返回结果为[1,1.5,1,1]
np.gradient(np.array([1,2,3,4]))
array([1., 1., 1., 1.])
对于二维数组,返回结果就回返回两个数组,一个对应第一个维度(行),另一个对应第二个维度(列)。
注意,对第一个维度求梯度的时候,其是拿整行来计算的。
举例来说, [[1, 2, 6],[2,1,1], [3, 4, 5]] 。第一个维度(行)的第一行对应的结果用一阶向前差分: 就是 [2,1,1] - [1,2,6] = [1, -1,-5]
对第二个维度来求梯度的时候,也就是每一行和一维数组求法一样。
np.gradient(np.array([[1, 2, 6],[2,1,1], [3, 4, 5]], dtype=np.float))
[array([[ 1. , -1. , -5. ],
[ 1. , 1. , -0.5],
[ 1. , 3. , 4. ]]), array([[ 1. , 2.5, 4. ],
[-1. , -0.5, 0. ],
[ 1. , 1. , 1. ]])]
13. linspace 和 logspace
linspace 是创建等差数列,logspcae是创建等比数列
linspace/logspace (start,end,num)
linspace中:
从start开始到end结束,创建num个数字组成等差数列。
logspace中:
从10**start开始到 10**end结束,创建num个数字组成等比数列
np.linspace(0,20,100)
array([ 0. , 0.2020202 , 0.4040404 , 0.60606061, 0.80808081,
1.01010101, 1.21212121, 1.41414141, 1.61616162, 1.81818182,
2.02020202, 2.22222222, 2.42424242, 2.62626263, 2.82828283,
3.03030303, 3.23232323, 3.43434343, 3.63636364, 3.83838384,
4.04040404, 4.24242424, 4.44444444, 4.64646465, 4.84848485,
5.05050505, 5.25252525, 5.45454545, 5.65656566, 5.85858586,
6.06060606, 6.26262626, 6.46464646, 6.66666667, 6.86868687,
7.07070707, 7.27272727, 7.47474747, 7.67676768, 7.87878788,
8.08080808, 8.28282828, 8.48484848, 8.68686869, 8.88888889,
9.09090909, 9.29292929, 9.49494949, 9.6969697 , 9.8989899 ,
10.1010101 , 10.3030303 , 10.50505051, 10.70707071, 10.90909091,
11.11111111, 11.31313131, 11.51515152, 11.71717172, 11.91919192,
12.12121212, 12.32323232, 12.52525253, 12.72727273, 12.92929293,
13.13131313, 13.33333333, 13.53535354, 13.73737374, 13.93939394,
14.14141414, 14.34343434, 14.54545455, 14.74747475, 14.94949495,
15.15151515, 15.35353535, 15.55555556, 15.75757576, 15.95959596,
16.16161616, 16.36363636, 16.56565657, 16.76767677, 16.96969697,
17.17171717, 17.37373737, 17.57575758, 17.77777778, 17.97979798,
18.18181818, 18.38383838, 18.58585859, 18.78787879, 18.98989899,
19.19191919, 19.39393939, 19.5959596 , 19.7979798 , 20. ])
np.logspace(0,9,10) #e+03 代表 10的三次方
array([1.e+00, 1.e+01, 1.e+02, 1.e+03, 1.e+04, 1.e+05, 1.e+06, 1.e+07,
1.e+08, 1.e+09])
matplotlib库函数
import matplotlib.pyplot as plt
1. figure()
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
num:图像编号或名称,数字为编号 ,字符串为名称
figsize:指定figure的宽和高,单位为英寸;
dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 1英寸等于2.5cm,A4纸是 21*30cm的纸张
facecolor:背景颜色
edgecolor:边框颜色
frameon:是否显示边框
fig = plt.figure("Num1",figsize=(10,10))
<Figure size 720x720 with 0 Axes>
2. add_subplot()
add_subplot(333)表示3行3列网格中第三个图
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(131)
x = np.linspace(0.10,200)
y = x
ax.plot(x,y,'-')
ax2 = fig.add_subplot(132)
ax2.plot(x/2.,y/2.,'-.')
[<matplotlib.lines.Line2D at 0x2651e31c390>]
Sklearn库函数
1.K折交叉与网格搜索(GridSearchCV())
具体参数使用查看 https://www.cnblogs.com/mdevelopment/p/9634629.html
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
parameters = {'C':[0.0001, 0.001, 0.01, 0.1, 1, 10, 100]}
model = LogisticRegression()
# 参数cv是K折交叉中的K,这里是10折交叉
clf = GridSearchCV(model, parameters, cv=10,return_train_score=True)
2.train_test_split()函数
train_test_split 是按照比例随机分配训练集和测试集的函数
train_test_split(train_data, train_target, test_size, random_state,shuffle)
train_test_split(pandas dataframes, test_size, random_state,shuffle)
①当输入参数为pandas dataframes时,返回也是两个pandas dataframes类型的结果,
一个是包含了训练集和训练集结果pandas dataframes,另一个则是包含了测试集和测试集结果的pandas dataframes
②当输入参数为train_data,train_target时,返回参数为x_tarin,x_test,y_train,y_test
test_size 是将test_size的比例的数据分配给测试集(test),其余部分为训练集(train)
random_state,随机数种子,随机数种子不变,随机情况不变
shuffle:洗牌模式,1)shuffle = False,不打乱样本数据顺序;2)shuffle = True,打乱样本数据顺序
import pandas as pd
import glob
from sklearn.model_selection import train_test_split
l_samp = glob.glob(r"./dataset/*vehicles/*/*")
M_ClassDict = {"non-vehicles" : 0, "vehicles" : 1}
pd_SampClass = pd.DataFrame({
"Sample" : l_samp,
"Class" : list(map(lambda x: M_ClassDict[x], list(map(lambda x: x.split("\\")[1], l_samp))))
})[['Sample', 'Class']]
pd_SampClass_train,pd_SampClass_cv = train_test_split(pd_SampClass, test_size=0.33, random_state=42)
pd_SampClass_train.head()
Sample | Class | |
---|---|---|
6490 | ./dataset\non-vehicles\GTI\image2279.png | 0 |
2736 | ./dataset\non-vehicles\Extras\extra3857.png | 0 |
15541 | ./dataset\vehicles\KITTI_extracted\4374.png | 1 |
1068 | ./dataset\non-vehicles\Extras\extra223.png | 0 |
3660 | ./dataset\non-vehicles\Extras\extra4821.png | 0 |
pd_SampClass_cv.head()
Sample | Class | |
---|---|---|
15314 | ./dataset\vehicles\KITTI_extracted\417.png | 1 |
11728 | ./dataset\vehicles\GTI_Right\image0881.png | 1 |
10379 | ./dataset\vehicles\GTI_Left\image0628.png | 1 |
6323 | ./dataset\non-vehicles\GTI\image2128.png | 0 |
5155 | ./dataset\non-vehicles\GTI\image1077.png | 0 |
import numpy as np
x = np.random.randint(0,20,size=[10,3])
y = np.random.randint(0,2,size=(10,))
y = y.reshape(10,1)
xy = np.hstack((x,y))
print("xy:\n",xy)
x_train,x_test,y_train,y_test = train_test_split(x,y, test_size=0.33, random_state=20,shuffle=True)
xy_train = np.hstack((x_train,y_train))
xy_test = np.hstack((x_test,y_test))
print("xy_train:\n",xy_train)
print("xy_test:\n",xy_test)
xy:
[[ 8 11 13 0]
[10 18 11 0]
[ 8 4 8 0]
[ 8 17 2 0]
[12 1 9 1]
[ 4 2 7 1]
[12 9 13 1]
[ 8 4 0 1]
[ 4 1 10 1]
[ 3 19 2 0]]
xy_train:
[[ 8 11 13 0]
[ 8 4 8 0]
[12 9 13 1]
[ 3 19 2 0]
[12 1 9 1]
[ 8 17 2 0]]
xy_test:
[[ 8 4 0 1]
[10 18 11 0]
[ 4 1 10 1]
[ 4 2 7 1]]
2. StandardScaler
标准化数据,通过计算平均值和方差来实现这一效果 X = (x - x平均)/方差
from sklearn.preprocessing import StandardScaler
X_scalerM = StandardScaler()
fit()函数是计算平均值和方差
transform是用fit计算的平均值和方差,X = (x-均值)/方差,返回标准化的数据
fit_transform 则是有上述两个函数的作用
fit 或 fit_transform 是用在训练集(train)的方法,利用训练集得到的平均值和方差,应用于测试集,所以测试集中只需再使用transform方法即可
Tensorflow库函数
1.placeholder()
placeholder()函数是在神经网络构建graph的时候在模型中的占位,此时并没有把要输入的数据传入模型,
它只会分配必要的内存。等建立session,在会话中,运行模型的时候通过feed_dict()函数向占位符喂入数据。
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as np
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2) #此时只是分配了内存并未有实际的数据
with tf.Session() as sess: #在session中喂入数据feed_dict
print(sess.run(output, feed_dict = {input1:[3.], input2: [4.]})) #输出结果为12
WARNING:tensorflow:From C:\Users\24651\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\compat\v2_compat.py:65: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
[12.]