python机器学习4-5代码及运行结果

来源:互联网 发布:uu淘宝店 编辑:程序博客网 时间:2024/06/10 05:55
import urllib.requestimport matplotlib.pyplot as plotfrom math import sqrt, cos, logtarget_url = ("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv")data = urllib.request.urlopen(target_url)xList = []labels = []names = []firstLine = Truefor line in data:    if firstLine == True:        names = line.strip().split(";".encode(encoding='utf-8'))        firstLine = False    else:        row = line.strip().split(";".encode(encoding='utf-8'))        labels.append(float(row[-1]))        row.pop()        floatRow = [float(num) for num in row]        xList.append(floatRow)        xExtended = []alchCol = len(xList[1])for row in xList:    newRow = list(row)    alch = row[alchCol - 1]#获取每一行的最后一列数    newRow.append((alch -7) * (alch - 7) / 10)#第一个新属性是((alch - 7) * (alch - 7)) /10,基本上新属性取酒精值的平方    newRow.append(5 * log(alch - 7))    newRow.append(cos(alch))    xExtended.append(newRow)nrow = len(xList)v1 = [xExtended[j][alchCol -1] for j in range(nrow)]for i in range(4):#range与索引相一致,i为0,1,2,3    v2 = [xExtended[j][alchCol - 1 + i] for j in range(nrow)]    plot.scatter(v1, v2)plot.xlabel("Alcohol")plot.ylabel(("Extension Functions of Alcohol"))plot.show()

图片显示效果如下:


阅读全文
0 0
原创粉丝点击