常用python_ML in Action
吕宣
2023-12-01
1.
sorted:排序后影响自身
sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)
#按照key排序,从大到小
sort:不改变自身
argsort:返回数组从小到大的索引
2.
title(x,(datasetSize,1))
#Construct an array by repeating x the number of times given by max(reps).
3.
datasetSize = dataset.shape[0]#返回行
normDataSet = zeros(shape(dataSet))#等大的
4.
set(data)#从列表创建集合是python得到列表中唯一元素最快的方法
5.
transpose: 对1-D array没有用,1-D matrix可以
对2-D 转置
6.
def loadDataSet():
dataMat = []; labelMat = []
fr = open('testSet.txt')
for line in fr.readlines():
lineArr = line.strip().split()
#strip() 删除空白符号
dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
labelMat.append(int(lineArr[2]))
return dataMat,labelMat
7.
mat1 = dataSet[nonzero(dataSet[:,feature] <= value)[0],:][0]
#返回值不为0的下标,默认返回两维,横的竖的
############plot
1.
from numpy import *
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
#datingDataMat,datingLabels = file2matrix('datingTestSet.txt')
#ax.scatter(datingDataMat[:,1], datingDataMat[:,2])
ax.scatter(datingDataMat[:,1], datingDataMat[:,2], 15.0*array(datingLabels), 15.0*array(datingLabels))#为两个类进行颜色标注
ax.axis([-2,25,-0.2,2.0])
plt.xlabel('Percentage of Time Spent Playing Video Games')
plt.ylabel('Liters of Ice Cream Consumed Per Week')
plt.show()
2.
annotation:画图注解工具