日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python葡萄酒数据集_利用python分析红葡萄酒数据

發布時間:2023/12/4 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python葡萄酒数据集_利用python分析红葡萄酒数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在本次分析中,我使用了隨機森林回歸,并涉及數據標準化和超參數調優。在這里,我使用隨機森林分類器,對好酒和不太好的酒進行二元分類。

首先導入數據包:

importnumpy as npimportpandas as pdimportmatplotlib.pyplot as pltimport seaborn as sns

導入數據:

data = pd.read_csv(‘winequality-red.csv‘)

data.head()

data.describe()

注釋:

fixed acidity:非揮發性酸

volatile acidity :?揮發性酸

citric acid:檸檬酸

residual sugar :剩余糖分

chlorides:氯化物

free sulfur dioxide :游離二氧化硫

total sulfur dioxide:總二氧化硫

density:密度

pH:pH

sulphates:硫酸鹽

alcohol:酒精

quality:質量

所有數據的數值為1599,所以沒有缺失值。讓我們看看是否有重復值:

extra =data[data.duplicated()]

extra.shape

有240個重復值,但先不刪除它,因為葡萄酒的質量等級是由不同的品酒師給出的。

數據可視化

sns.set()

data.hist(figsize=(10,10), color=‘red‘)

plt.show()

只有質量是離散型變量,主要集中在5和6中,下面分析下變量的相關性:

colormap =plt.cm.viridis

plt.figure(figsize=(12,12))

plt.title(‘Correlation of Features‘, y=1.05, size=15)

sns.heatmap(data.astype(float).corr(),linewidths=0.1,vmax=1.0, square=True,

linecolor=‘white‘, annot=True)

觀察:

酒精與葡萄酒質量的相關性最高,其次是各種酸度、硫酸鹽、密度和氯化物。

使用分類器:

將葡萄酒分成兩組;“優質”>5為“好酒”

y = data.quality #set ‘quality‘ as target

X = data.drop(‘quality‘, axis=1) #rest are features

print(y.shape, X.shape) #check correctness

#Create a new y1

y1 = (y > 5).astype(int)

y1.head()

# plot histogram

ax= y1.plot.hist(color=‘green‘)

ax.set_title(‘Wine quality distribution‘, fontsize=14)

ax.set_xlabel(‘aggregated target value‘)

利用隨機森林分類器訓練預測模型

from sklearn.model_selection importtrain_test_split, cross_val_scorefrom sklearn.ensemble importRandomForestClassifierfrom sklearn.metrics importaccuracy_score, log_lossfrom sklearn.metrics import confusion_matrix

將數據分割為訓練和測試數據集

seed = 8 #set seed for reproducibility

X_train, X_test, y_train, y_test = train_test_split(X, y1, test_size=0.2,

random_state=seed)

print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)

對隨機森林分類器進行交叉驗證訓練和評價

#Instantiate the Random Forest Classifier

RF_clf = RandomForestClassifier(random_state=seed)

RF_clf

#在訓練數據集上計算k-fold交叉驗證,并查看平均精度得分

cv_scores = cross_val_score(RF_clf,X_train, y_train, cv=10, scoring=‘accuracy‘)print(‘The accuracy scores for the iterations are {}‘.format(cv_scores))print(‘The mean accuracy score is {}‘.format(cv_scores.mean()))

執行預測

RF_clf.fit(X_train, y_train)

pred_RF= RF_clf.predict(X_test)

#Print 5 results to see

for i in range(0,5):print(‘Actual wine quality is‘, y_test.iloc[i], ‘and predicted is‘, pred_RF[i])

在前五名中,有一個錯誤。讓我們看看指標。

print(accuracy_score(y_test, pred_LR))print(log_loss(y_test, pred_LR))

print(confusion_matrix(y_test, pred_LR))

總共有81個分類錯誤。

與Logistic回歸分類器相比,隨機森林分類器更優。

讓我們調優隨機森林分類器的超參數

from sklearn.model_selection importGridSearchCV

grid_values= {‘n_estimators‘:[50,100,200],‘max_depth‘:[None,30,15,5],‘max_features‘:[‘auto‘,‘sqrt‘,‘log2‘],‘min_samples_leaf‘:[1,20,50,100]}

grid_RF= GridSearchCV(RF_clf,param_grid=grid_values,scoring=‘accuracy‘)

grid_RF.fit(X_train, y_train)

grid_RF.best_params_

除了估計數之外,其他推薦值是默認值。

RF_clf = RandomForestClassifier(n_estimators=100,random_state=seed)

RF_clf.fit(X_train,y_train)

pred_RF=RF_clf.predict(X_test)print(accuracy_score(y_test,pred_RF))print(log_loss(y_test,pred_RF))

print(confusion_matrix(y_test,pred_RF))

通過超參數調諧,射頻分類器的準確度已提高到82.5%,日志損失值也相應降低。分類錯誤的數量也減少到56個。

將隨機森林分類器作為基本推薦器,將紅酒分為“推薦”(6級以上)或“不推薦”(5級以下),預測準確率為82.5%似乎是合理的。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python葡萄酒数据集_利用python分析红葡萄酒数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。