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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通俗地讲清楚fit_transform()和transform()的区别

發布時間:2023/12/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通俗地讲清楚fit_transform()和transform()的区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

網上抄來抄去都是一個意思,

fit_transform是fit和transform的組合。

?

?我們知道fit(x,y)在新手入門的例子中比較多,但是這里的fit_transform(x)的括號中只有一個參數,這是為什么呢?

fit(x,y)傳兩個參數的是有監督學習的算法,fit(x)傳一個參數的是無監督學習的算法,比如降維、特征提取、標準化

?

然后解釋為什么出來fit_transform()這個東西,下面是重點:

fit和transform沒有任何關系,僅僅是數據處理的兩個不同環節,之所以出來這么個函數名,僅僅是為了寫代碼方便,

所以會發現transform()和fit_transform()的運行結果是一樣的。

?

注意:運行結果一模一樣不代表這兩個函數可以互相替換,絕對不可以!!!

transform函數是一定可以替換為fit_transform函數的

fit_transform函數不能替換為transform函數!!!理由解釋如下:

?sklearn里的封裝好的各種算法都要fit、然后調用各種API方法,transform只是其中一個API方法,所以當你調用除transform之外的方法,必須要先fit,為了通用的寫代碼,還是分開寫比較好?

也就是說,這個fit相對于transform而言是沒有任何意義的,但是相對于整個代碼而言,fit是為后續的API函數服務的,所以fit_transform不能改寫為transform。

?

?

下面的代碼用來舉例示范,數據集是代碼自動從網上下載的,如果把下面的乳腺癌相關的機器學習代碼中的fit_transform改為transform,編譯器就會報錯。(下面給出的是無錯誤的代碼)

?

# coding: utf-8 # 導入pandas與numpy工具包。 import pandas as pd import numpy as np# 創建特征列表。 column_names = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape', 'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli', 'Mitoses', 'Class'] # 使用pandas.read_csv函數從互聯網讀取指定數據。 data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data', names = column_names )# 將?替換為標準缺失值表示。 data = data.replace(to_replace='?', value=np.nan) # 丟棄帶有缺失值的數據(只要有一個維度有缺失)。 data = data.dropna(how='any')# 輸出data的數據量和維度。 data.shape# In[2]:# 使用sklearn.cross_valiation里的train_test_split模塊用于分割數據。 from sklearn.cross_validation import train_test_split# 隨機采樣25%的數據用于測試,剩下的75%用于構建訓練集合。 X_train, X_test, y_train, y_test = train_test_split(data[column_names[1:10]], data[column_names[10]], test_size=0.25, random_state=33) # print "data[column_names[10]]",data[column_names[10]]# 查驗訓練樣本的數量和類別分布。 y_train=pd.Series(y_train) y_train.value_counts()# 查驗測試樣本的數量和類別分布。 y_test=pd.Series(y_test) y_test.value_counts()# 從sklearn.preprocessing里導入StandardScaler。 from sklearn.preprocessing import StandardScaler # 從sklearn.linear_model里導入LogisticRegression與SGDClassifier。 from sklearn.linear_model import LogisticRegression from sklearn.linear_model import SGDClassifier#標準化數據,保證每個維度的特征數據方差為1,均值為0。使得預測結果不會被某些維度過大的特征值而主導。 ss = StandardScaler() X_train = ss.fit_transform(X_train) X_test = ss.transform(X_test)## 初始化LogisticRegression與SGDClassifier。 lr = LogisticRegression() sgdc = SGDClassifier()# 調用LogisticRegression中的fit函數/模塊用來訓練模型參數。 lr.fit(X_train, y_train) # 使用訓練好的模型lr對X_test進行預測,結果儲存在變量lr_y_predict中。 lr_y_predict = lr.predict(X_test)# 調用SGDClassifier中的fit函數/模塊用來訓練模型參數。 sgdc.fit(X_train, y_train) # 使用訓練好的模型sgdc對X_test進行預測,結果儲存在變量sgdc_y_predict中。 sgdc_y_predict = sgdc.predict(X_test)# 從sklearn.metrics里導入classification_report模塊。 from sklearn.metrics import classification_report# 使用邏輯斯蒂回歸模型自帶的評分函數score獲得模型在測試集上的準確性結果。 print ("Accuracy of LR Classifier:", lr.score(X_test, y_test)) # 利用classification_report模塊獲得LogisticRegression其他三個指標的結果。 print (classification_report(y_test, lr_y_predict, target_names=['Benign', 'Malignant']))# 使用隨機梯度下降模型自帶的評分函數score獲得模型在測試集上的準確性結果。 print 'Accuarcy of SGD Classifier:', sgdc.score(X_test, y_test) # 利用classification_report模塊獲得SGDClassifier其他三個指標的結果。 print classification_report(y_test, sgdc_y_predict, target_names=['Benign', 'Malignant'])


會得到報錯信息

?

AttributeError: 'StandardScaler' object has no attribute 'mean_'

有的版本報錯更加直接:

sklearn.exceptions.NotFittedError: This StandardScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

原因就是因為代碼中的fit_transform函數被改為了transform函數。

所以總結:

fit_transform與transform運行結果一致,但是fit與transform無關,只是數據處理的兩個環節,fit是為了程序的后續函數transform的調用而服務的,是個前提條件。

以上。

?

總結

以上是生活随笔為你收集整理的通俗地讲清楚fit_transform()和transform()的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

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