import urllib.request
import matplotlib.pyplot as plot
from math import sqrt, cos, log
target_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 = True
for 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()
图片显示效果如下: