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

本文通过抓取红酒质量数据集并使用Python进行数据分析及可视化展示。利用urllib请求数据,matplotlib绘制图表,并通过数学变换增加特征变量,探索酒精含量与酒质之间的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()

图片显示效果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值