一、 row.pop()函数
去掉列表最后一列
labels.append(row.pop()) #pop已经发挥作用
二、范数
http://blog.csdn.net/shijing_0214/article/details/51757564(参考范数)
np.linalg.norm((yTestwineQModel.predict(xTest)),2)/sqrt(len(yTest)) norm范数
矩阵的各种范数
(参考 http://blog.csdn.net/compression/article/details/46349633)
1. n = norm(A,1) %求A的列范数,等于A的列向量的1-范数的最大值。
2. n = norm(A,2) %求A的欧几里德范数,和norm(A)相同。
3. n = norm(A,inf) %求行范数,等于A的行向量的1-范数的最大值即:max(sum(abs(A’)))。
4. n = norm(A, ‘fro’ ) %求矩阵A的Frobenius范数,矩阵元p阶范数估计需要自己编程求,
1. 1-范数:,列和范数,即所有矩阵列向量绝对值之和的最大值,matlab调用函数norm(A,1)。
2. 2-范数:,谱范数,即A’A矩阵的最大特征值的开平方。matlab调用函数norm(x,2)。
3. ∞-范数:,行和范数,即所有矩阵行向量绝对值之和的最大值,matlab调用函数norm(A,inf)。
4. F-范数:,Frobenius范数,即矩阵元素绝对值的平方和再开平方,matlab调用函数norm(A,’fro‘)。
例子
(1) 1范数 norm(a,1)=max(sum(abs(a)))
a=[1 2 3;4 5 6;7 8 9]
a =
1 2 3
4 5 6
7 8 9
norm(a,1)
ans =
18
>> sum(abs(a))
ans =
12 15 18
>> max(ans)
ans =
18
(2)2范数
>> norm(a)
ans =
16.8481
>> norm(a,2)
ans =
16.8481
[V S]=eig(a'*a)
V =
-0.4082 -0.7767 0.4797
0.8165 -0.0757 0.5724
-0.4082 0.6253 0.6651
S =
0.0000 0 0
0 1.1414 0
0 0 283.8586
>> sqrt(S)
ans =
0.0000 0 0
0 1.0684 0
0 0 16.8481
(3)∞-范数 norm(a,inf)=max(sum(abs(a),2))
norm(a,inf)
ans =
24
sum(abs(a),2)
ans =
6
15
24
(4) F-范数 norm(a,’fro’)=sqrt(sum(sum(abs(a).^2)))
norm(a,'fro')
ans =
16.8819
>> sum(sum(abs(a).^2))
ans =
285
>> sqrt(ans)
ans =
16.8819
三、数组的拼接方法
思路:numpy提供了numpy.concatenate((a1,a2,…), axis=0)函数。能够一次完成多个数组的拼接。其中a1,a2,…是数组类型的参数
1.
>>> a=np.array([1,2,3])
>>> b=np.array([11,22,33])
>>> c=np.array([44,55,66])
>>> np.concatenate((a,b,c),axis=0) # 默认情况下,axis=0可以不写
array([ 1, 2, 3, 11, 22, 33, 44, 55, 66])
#对于一维数组拼接,axis的值不影响最后的结果
2.
>>> a=np.array([[1,2,3],[4,5,6]])
>>> b=np.array([[11,21,31],[7,8,9]])
>>> np.concatenate((a,b),axis=0)
array([[ 1, 2, 3],
[ 4, 5, 6],
[11, 21, 31],
[ 7, 8, 9]])
3.
>>> np.concatenate((a,b),axis=1)
#axis=1表示对应行的数组进行拼接
array([[ 1, 2, 3, 11, 21, 31],
[ 4, 5, 6, 7, 8, 9]])
四、绝对值
abs()是一个内置函数,而fabs()在math模块中定义。
fabs()函数只适用于float和integer类型,而abs()也适用于复数。
五、数组索引
np.argsort 返回索引数组
list中可以 list.index
六、np.append 来创建数组
xTrain = np.append(xTrain,xTrainTemp,axis=0)
七、逆向切片
idxTemp = np.argsort(featureImportance)[::-1]
#逆向 ,就是把这个反过来
print('idxTemp',idxTemp)