<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 如何使用 scikit-learn 進行預測 > 原文: [https://machinelearningmastery.com/make-predictions-scikit-learn/](https://machinelearningmastery.com/make-predictions-scikit-learn/) #### 如何使用 Python 中的 scikit-learn 模型預測分類或回歸結果 。 一旦您在 scikit-learn 中選擇并適合最終的機器學習模型,您就可以使用它來對新數據實例進行預測。 初學者對如何做到這一點有一些困惑。我經常看到以下問題: 如何在 scikit-learn 中使用我的模型進行預測? 在本教程中,您將了解如何使用 scikit-learn Python 庫中的最終機器學習模型進行分類和回歸預測。 完成本教程后,您將了解: * 如何最終確定模型以便為預測做好準備。 * 如何在 scikit-learn 中進行類和概率預測。 * 如何在 scikit-learn 中進行回歸預測。 讓我們開始吧。 ![Gentle Introduction to Vector Norms in Machine Learning](https://img.kancloud.cn/30/7d/307d80f80687403cb90b4b0a95c0d5cc_640x428.jpg) 機器學習中向量規范的溫和介紹 Cosimo 的照片,保留一些權利。 ## 教程概述 本教程分為 3 個部分;他們是: 1. 首先完成您的模型 2. 如何用分類模型預測 3. 如何用回歸模型預測 ## 1.首先完成您的模型 在進行預測之前,必須訓練最終模型。 您可能使用 k 折交叉驗證或訓練/測試分割數據來訓練模型。這樣做是為了讓您估計模型對樣本外數據的技能,例如:新數據。 這些模型已達到目的,現在可以丟棄。 您現在必須在所有可用數據上訓練最終模型。 您可以在此處了解有關如何訓練最終模型的更多信息: * [如何訓練最終機器學習模型](https://machinelearningmastery.com/train-final-machine-learning-model/) ## 2.如何用分類模型預測 分類問題是模型學習輸入要素和作為標簽的輸出要素之間的映射的問題,例如“_ 垃圾郵件 _”和“_ 不是垃圾郵件 _”。 下面是針對簡單二元分類問題的最終 [LogisticRegression](http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html) 模型的示例代碼。 雖然我們在本教程中使用 _LogisticRegression_ ,但在 scikit-learn 中幾乎所有的分類算法都可以使用相同的函數。 ``` # example of training a final classification model from sklearn.linear_model import LogisticRegression from sklearn.datasets.samples_generator import make_blobs # generate 2d classification dataset X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1) # fit final model model = LogisticRegression() model.fit(X, y) ``` 完成模型后,您可能希望將模型保存到文件,例如通過泡菜。保存后,您可以隨時加載模型并使用它進行預測。有關此示例,請參閱帖子: * [用 scikit-learn](https://machinelearningmastery.com/save-load-machine-learning-models-python-scikit-learn/) 在 Python 中保存和加載機器學習模型 為簡單起見,我們將跳過本教程中的示例。 我們可能希望使用最終模型進行兩種類型的分類預測;它們是階級預測和概率預測。 ### 階級預測 類預測是:給定最終模型和一個或多個數據實例,預測數據實例的類。 我們不知道新數據的結果類。這就是我們首先需要模型的原因。 我們可以使用 _predict()_ 函數在 scikit-learn 中使用我們最終的分類模型來預測新數據實例的類。 例如,我們在名為 _Xnew_ 的數組中有一個或多個數據實例。這可以傳遞給我們模型上的 _predict()_ 函數,以預測數組中每個實例的類值。 ``` Xnew = [[...], [...]] ynew = model.predict(Xnew) ``` ### 多類預測 讓我們通過一次預測多個數據實例的示例來具體化。 ``` # example of training a final classification model from sklearn.linear_model import LogisticRegression from sklearn.datasets.samples_generator import make_blobs # generate 2d classification dataset X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1) # fit final model model = LogisticRegression() model.fit(X, y) # new instances where we do not know the answer Xnew, _ = make_blobs(n_samples=3, centers=2, n_features=2, random_state=1) # make a prediction ynew = model.predict(Xnew) # show the inputs and predicted outputs for i in range(len(Xnew)): print("X=%s, Predicted=%s" % (Xnew[i], ynew[i])) ``` 運行該示例預測三個新數據實例的類,然后將數據和預測一起打印。 ``` X=[-0.79415228 2.10495117], Predicted=0 X=[-8.25290074 -4.71455545], Predicted=1 X=[-2.18773166 3.33352125], Predicted=0 ``` ### 單一類預測 如果您只有一個新數據實例,則可以將此數據包裝為 _predict()_ 函數;例如: ``` # example of making a single class prediction from sklearn.linear_model import LogisticRegression from sklearn.datasets.samples_generator import make_blobs # generate 2d classification dataset X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1) # fit final model model = LogisticRegression() model.fit(X, y) # define one new instance Xnew = [[-0.79415228, 2.10495117]] # make a prediction ynew = model.predict(Xnew) print("X=%s, Predicted=%s" % (Xnew[0], ynew[0])) ``` 運行該示例將打印單個實例和預測類。 ``` X=[-0.79415228, 2.10495117], Predicted=0 ``` ### 關于類標簽的注釋 準備好數據后,您將把域中的類值(例如字符串)映射到整數值。您可能使用過 [LabelEncoder](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html#sklearn.preprocessing.LabelEncoder) 。 此 _LabelEncoder_ 可用于通過 _inverse_transform()_ 函數將整數轉換回字符串值。 因此,您可能希望在擬合最終模型時保存(pickle)用于編碼 y 值的 _LabelEncoder_ 。 ### 概率預測 您可能希望進行的另一種類型的預測是數據實例屬于每個類的概率。 這被稱為概率預測,其中給定新實例,模型將每個結果類的概率返回為 0 和 1 之間的值。 您可以通過調用 _predict_proba()_ 函數在 scikit-learn 中進行這些類型的預測,例如: ``` Xnew = [[...], [...]] ynew = model.predict_proba(Xnew) ``` 此功能僅適用于能夠進行概率預測的分類模型,這是大多數但不是所有模型。 以下示例對數據實例的 _Xnew_ 數組中的每個示例進行概率預測。 ``` # example of making multiple probability predictions from sklearn.linear_model import LogisticRegression from sklearn.datasets.samples_generator import make_blobs # generate 2d classification dataset X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1) # fit final model model = LogisticRegression() model.fit(X, y) # new instances where we do not know the answer Xnew, _ = make_blobs(n_samples=3, centers=2, n_features=2, random_state=1) # make a prediction ynew = model.predict_proba(Xnew) # show the inputs and predicted probabilities for i in range(len(Xnew)): print("X=%s, Predicted=%s" % (Xnew[i], ynew[i])) ``` 運行實例進行概率預測,然后打印輸入數據實例以及每個實例屬于第一和第二類(0 和 1)的概率。 ``` X=[-0.79415228 2.10495117], Predicted=[0.94556472 0.05443528] X=[-8.25290074 -4.71455545], Predicted=[3.60980873e-04 9.99639019e-01] X=[-2.18773166 3.33352125], Predicted=[0.98437415 0.01562585] ``` 如果您想向用戶提供專家解釋的概率,這在您的應用程序中會有所幫助。 ## 3.如何用回歸模型預測 回歸是一種監督學習問題,在給定輸入示例的情況下,模型學習映射到合適的輸出量,例如“0.1”和“0.2”等。 下面是最終的 _LinearRegression_ 模型的示例。同樣,用于進行回歸預測的函數適用于 scikit-learn 中可用的所有回歸模型。 ``` # example of training a final regression model from sklearn.linear_model import LinearRegression from sklearn.datasets import make_regression # generate regression dataset X, y = make_regression(n_samples=100, n_features=2, noise=0.1, random_state=1) # fit final model model = LinearRegression() model.fit(X, y) ``` 我們可以通過在最終模型上調用 _predict()_ 函數來使用最終的回歸模型預測數量。 與分類一樣,predict()函數采用一個或多個數據實例的列表或數組。 ### 多元回歸預測 下面的示例演示了如何對具有未知預期結果的多個數據實例進行回歸預測。 ``` # example of training a final regression model from sklearn.linear_model import LinearRegression from sklearn.datasets import make_regression # generate regression dataset X, y = make_regression(n_samples=100, n_features=2, noise=0.1) # fit final model model = LinearRegression() model.fit(X, y) # new instances where we do not know the answer Xnew, _ = make_regression(n_samples=3, n_features=2, noise=0.1, random_state=1) # make a prediction ynew = model.predict(Xnew) # show the inputs and predicted outputs for i in range(len(Xnew)): print("X=%s, Predicted=%s" % (Xnew[i], ynew[i])) ``` 運行該示例會進行多次預測,然后并排打印輸入和預測以供審閱。 ``` X=[-1.07296862 -0.52817175], Predicted=-61.32459258381131 X=[-0.61175641 1.62434536], Predicted=-30.922508147981667 X=[-2.3015387 0.86540763], Predicted=-127.34448527071137 ``` ### 單回歸預測 可以使用相同的函數來對單個數據實例進行預測,只要它適當地包裝在周圍的列表或數組中即可。 例如: ``` # example of training a final regression model from sklearn.linear_model import LinearRegression from sklearn.datasets import make_regression # generate regression dataset X, y = make_regression(n_samples=100, n_features=2, noise=0.1) # fit final model model = LinearRegression() model.fit(X, y) # define one new data instance Xnew = [[-1.07296862, -0.52817175]] # make a prediction ynew = model.predict(Xnew) # show the inputs and predicted outputs print("X=%s, Predicted=%s" % (Xnew[0], ynew[0])) ``` 運行該示例進行單個預測并打印數據實例和預測以供審閱。 ``` X=[-1.07296862, -0.52817175], Predicted=-77.17947088762787 ``` ## 進一步閱讀 如果您希望深入了解,本節將提供有關該主題的更多資源。 * [如何訓練最終機器學習模型](https://machinelearningmastery.com/train-final-machine-learning-model/) * [用 scikit-learn](https://machinelearningmastery.com/save-load-machine-learning-models-python-scikit-learn/) 在 Python 中保存和加載機器學習模型 * [scikit-learn API 參考](http://scikit-learn.org/stable/modules/classes.html) ### 摘要 在本教程中,您了解了如何使用 scikit-learn Python 庫中的最終機器學習模型進行分類和回歸預測。 具體來說,你學到了: * 如何最終確定模型以便為預測做好準備。 * 如何在 scikit-learn 中進行類和概率預測。 * 如何在 scikit-learn 中進行回歸預測。 你有任何問題嗎? 在下面的評論中提出您的問題,我會盡力回答。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看