白葡萄酒质量分析

白葡萄酒质量探索

数据来自于uci机器学习数据库中的白葡萄酒部分

library(ggplot2)
library(gridExtra)
library(GGally)
# 加载数据
df <- read.csv('wineQualityWhites.csv')

汇总统计

# 察看数据结构
str(df)

在这里插入图片描述
从这里我们可以发现一共有4898行观测值,以及13个变量。

# 察看汇总分析
summary(df)

在这里插入图片描述

固定酸

# 画出固定酸含量分布图
ggplot(aes(x = fixed.acidity), data = df) +
  geom_histogram(binwidth = 0.1) +
  scale_x_continuous(breaks = seq(3.5, 15.5, 3)) 
summary(df$fixed.acidity)

在这里插入图片描述
在这里插入图片描述
从结果中可以发现,固定酸的含量大致是一个正态分布。50%的数据分布在6.3-7.3(g/dm^3)的范围之内。中位数为6.800,平均值为 6.855。

挥发性酸

# 画出挥发酸含量分布图
ggplot(aes(x = volatile.acidity), data = df) +
  geom_histogram(binwidth = 0.01) +
  scale_x_continuous(breaks = seq(0.1, 1.2, 0.2))
summary(df$volatile.acidity)

在这里插入图片描述
在这里插入图片描述
从结果中,我们可以发现,挥发酸的含量大致是一个正态分布。50%的数据分布在0.21-0.32(g/dm^3)的范围之内。中位数为0.26,平均值为 0.2782。

柠檬酸

# 画出柠檬酸的分布图
ggplot(aes(x = citric.acid), data = df) + 
  geom_histogram(binwidth = 0.01) + 
  scale_x_continuous(breaks = seq(0, 1.8, 0.2))
summary(df$citric.acid)

在这里插入图片描述
在这里插入图片描述
从结果中,我们可以发现,柠檬酸的含量大致是一个正态分布(除去右边的大于0.6的异常值)。50%的数据分布在0.27-0.39(g/dm^3)的范围之内。中位数为0.32,平均值为0.3342。同时由于,上述提到的三个变量都是酸,因此把固定酸,挥发酸,柠檬酸三项合起来定义一个新的特征总酸量。

残留糖分

# 画出残留糖分分布图
ggplot(aes(x = residual.sugar), data = df) +
  geom_histogram(binwidth = 1)
summary(df$residual.sugar)

在这里插入图片描述
在这里插入图片描述
从图中可以看出,残留糖分不是正态分布也不像偏态分布,并且由于坐标之间差距很大,因此在下图使用Log10作伪标尺,再来确认一下分布。从数据上可以看出,50%的数据分布在1.7-9.9(g/dm^3)的范围内,中位数为5.2,平均数为6.391.

# 使用log10尺度来显示分布图
ggplot(aes(x = residual.sugar), data = df) +
  geom_histogram(binwidth = 0.05) +
  scale_x_log10() +
  xlab('residual.sugar by log10')

在这里插入图片描述

从图中可以发现,变成log10显示后,很明显残余糖分出现了一个双峰的形状。结合这个因素,我们新定义一个特征,类型。以5为分界线,定义干葡萄酒(dry)以及甜葡萄酒(sweet)。

# 建立新的变量type包含干与甜,并且画出分布图
df$type<-ifelse(df$residual.sugar >= 5, 'sweet', 'dry')

ggplot(aes(x = type), data = df) + 
  geom_bar(width = 0.3)

table(df$type)

在这里插入图片描述
可以看到干葡萄酒和甜葡萄酒数量分别是2367,2531。
在这里插入图片描述

氯化物

# 画出氯化物含量分布图
ggplot(aes(x = chlorides), data = df) +
  geom_histogram(binwidth = 0.005)
summary(df$chlorides)

在这里插入图片描述
在这里插入图片描述
从结果中可以发现氯化物成一个长尾分布,50%的数据分布在0.036-0.05(g/dm^3)的范围之内。中位数为0.043,平均数为0.04577。

游离二氧化硫

# 画出游离二氧化硫分布图
ggplot(aes(x = free.sulfur.dioxide), data = df) +
  geom_histogram(binwidth = 2)
summary(df$free.sulfur.dioxide)

在这里插入图片描述
在这里插入图片描述
从结果中可以发现整体是一个长尾分布,50%的数据分布在23.00-35.31(mg/dm^3)之间,中位数为34.00,平均数为35.31。

总二氧化硫


                
  • 10
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用Python中的scikit-learn库实现软支持向量机(SVM)对白葡萄酒质量数据进行分析并生成图像的示例代码: ```python # 导入所需的库 import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.svm import SVC from sklearn.metrics import classification_report, confusion_matrix # 读取数据集 data = pd.read_csv('winequality-white.csv', sep=';') # 数据预处理 X = data.drop('quality', axis=1) y = data['quality'] y = np.where(y<=5, 0, 1) # 将质量等级<=5的设为0,>5的设为1 # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # 训练SVM模型 svm = SVC(kernel='linear', C=0.1) svm.fit(X_train, y_train) # 在测试集上进行预测 y_pred = svm.predict(X_test) # 评估模型性能 print(confusion_matrix(y_test, y_pred)) print(classification_report(y_test, y_pred)) # 绘制决策边界(只绘制前两个特征) fig, ax = plt.subplots() ax.scatter(X.iloc[:, 0], X.iloc[:, 1], c=y) xlim = ax.get_xlim() ylim = ax.get_ylim() xx = np.linspace(xlim[0], xlim[1], 30) yy = np.linspace(ylim[0], ylim[1], 30) XX, YY = np.meshgrid(xx, yy) xy = np.vstack([XX.ravel(), YY.ravel()]).T Z = svm.decision_function(xy).reshape(XX.shape) ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--']) ax.scatter(svm.support_vectors_[:, 0], svm.support_vectors_[:, 1], s=100, linewidth=1, facecolors='none', edgecolors='k') plt.show() ``` 代码中使用线性核函数(kernel='linear')的SVM模型进行训练,C参数设为0.1。在测试集上进行预测,并使用混淆矩阵和分类报告来评估模型性能。最后,使用matplotlib库绘制决策边界图。 注意:为了简化代码,只绘制了前两个特征的决策边界。如果想要绘制所有特征的决策边界,可以在模型训练时将X_train和X_test的维度降至2维,如使用主成分分析(PCA)进行降维。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值