使用深度學習預測葡萄酒型別
近年來,深度學習因其分析複雜資料集和進行準確預測的能力而受到廣泛關注,一個有趣的應用是根據各種化學屬性預測葡萄酒型別。透過利用深度學習演算法的強大功能,研究人員已經能夠開發出能夠高精度分類葡萄酒的模型。
本文探討了使用深度學習技術(例如神經網路)根據酒精含量、酸度和酚類化合物等屬性預測葡萄酒型別。透過利用深度學習的潛力,葡萄酒生產商和愛好者可以提高他們的決策過程和葡萄酒質量評估,最終帶來更好的客戶滿意度和行業進步。
使用深度學習預測葡萄酒型別
以下是我們將遵循的步驟,以使用深度學習預測葡萄酒型別:
步驟 1:匯入所需的庫
Pandas 用於資料操作,NumPy 用於數值運算,Matplotlib 用於資料視覺化,scikit-learn 用於資料預處理,Keras 用於構建深度學習模型。
步驟 2:載入資料集
它使用 Pandas 中的 pd.read_csv() 函式將資料集載入到 DataFrame 中。
步驟 3:資料預處理
在此步驟中,特徵(屬性)被提取到變數 X 中,而目標變數(葡萄酒型別)被提取到變數 y 中。
步驟 4:編碼葡萄酒型別
葡萄酒型別是分類變數,因此需要將其編碼為數值,以便模型能夠理解它們。scikit-learn 中的 LabelEncoder() 類用於將葡萄酒型別轉換為數值標籤。
步驟 5:資料分析
此步驟提供有關資料集的一些基本資訊。它計算資料集中的特徵(屬性)數量、類別(唯一葡萄酒型別)數量和樣本(行)數量。然後,它列印這些值以進行分析。
步驟 6:將資料集分割成訓練集和測試集
使用 scikit-learn 中的 train_test_split() 函式將資料集分割成訓練集和測試集。它將 80% 的資料分配給訓練集,將 20% 的資料分配給測試集。random_state 引數確保結果的可重複性。
步驟 7:標準化特徵
特徵縮放對於深度學習很重要。在這裡,scikit-learn 中的 StandardScaler() 類用於標準化特徵。訓練集使用 fit_transform() 擬合到縮放器,而測試集僅使用 transform() 進行轉換,以避免資料洩露。
步驟 8:建立模型
此步驟涉及使用 Keras 框架構建深度學習模型。該模型遵循順序結構,其中各層依次排列。密集層表示全連線層,其中每個神經元都連線到前一層和後一層中的每個神經元。
示例
以下是使用上述步驟的程式示例。
import pandas as pdd import numpy as npp from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense import matplotlib.pyplot as pltt from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelEncoder, StandardScaler # Load the dataset datasetd = pdd.read_csv('wine.csv') # Data preprocessing X = datasetd.iloc[:, 1:].values y = datasetd.iloc[:, 0].values # Encode the wine types label_encoder = LabelEncoder() y = label_encoder.fit_transform(y) num_features = X.shape[1] num_classes = len(np.unique(y)) num_samples = X.shape[0] print(f"Number of samples: {num_samples}") print(f"Number of features: {num_features}") print(f"Number of classes: {num_classes}") # Split the dataset into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Standardize the features scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) # Create the model model1 = Sequential() model1.add(Dense(64, activation='relu', input_shape=(num_features,))) model1.add(Dense(64, activation='relu')) model1.add(Dense(num_classes, activation='softmax')) model1.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Train the model history = model1.fit(X_train, y_train, batch_size=32, epochs=50, validation_split=0.2, verbose=1) # Plot the training history pltt.plot(history.history['accuracy']) pltt.plot(history.history['val_accuracy']) pltt.title('Model Accuracy') pltt.xlabel('Epoch') pltt.ylabel('Accuracy') pltt.legend(['Train', 'Validation'], loc='upper left') pltt.show() # Evaluate the model loss, accuracy = model1.evaluate(X_test, y_test, verbose=0) print(f'Test Loss: {loss:.4f}') print(f'Test Accuracy: {accuracy:.4f}') # Predict on new data new_data = [[13.24, 2.59, 2.87, 21.0, 118, 2.8, 2.69, 0.39, 1.82, 4.32, 1.04, 2.93, 735]] new_data_scaled = scaler.transform(new_data) predictions = model1.predict(new_data_scaled) predicted_class = npp.argmax(predictions) # Map predicted class to wine type predicted_wine_type = label_encoder.inverse_transform([predicted_class])[0] print(f'Predicted Wine Type: {predicted_wine_type}')
輸出
Number of samples: 178 Number of features: 13 Number of classes: 3 Epoch 1/50 4/4 [==============================] - 1s 81ms/step - loss: 1.0252 - accuracy: 0.3805 - val_loss: 0.8866 - val_accuracy: 0.5862 …………………………………………………………………………………………………………………………………………….. Epoch 50/50 4/4 [==============================] - 0s 12ms/step - loss: 0.0109 - accuracy: 1.0000 - val_loss: 0.0520 - val_accuracy: 0.9655
Test Loss: 0.0074 Test Accuracy: 1.0000 1/1 [==============================] - 0s 88ms/step Predicted Wine Type: 1
結論
總而言之,使用深度學習預測葡萄酒型別顯示出巨大的潛力。透過利用關於葡萄酒屬性的大量資料,深度學習演算法已成功地根據其化學成分精確地對葡萄酒進行分類。這一進步有可能給葡萄酒行業帶來重大變革。
它使生產商能夠在葡萄種植、發酵技術和風味特徵方面做出明智的選擇。最終,這項技術是徹底改變葡萄酒生產和改進生產商決策過程的關鍵,從而提高質量併為葡萄酒愛好者帶來更令人滿意的體驗。