Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True)

加入好友
加入社群
Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

from sklearn.model_selection import train_test_split

xtrain,xtest,ytrain,ytest =\
train_test_split(x,y,test_size=0.3,
random_state=42,shuffle=True)

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

樣品分割:

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

參考解答:

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

import pandas as pd
import sys
fpath = r”C:\Python\P107\BostonHousing.csv”
dataset = pd.read_csv(fpath)
#.shape = (506, 14)
headerList = dataset.columns.tolist()
cols = dataset.columns.size
# dataset.columns.size = 14
x = dataset.drop(headerList[-1],axis=1).values
y = dataset[headerList[-1]].values

“””

#x=dataset.iloc[:,0:cols-1].values
#y=dataset.iloc[:,cols-1:].values

#用iloc切片的y 資料非常像,

#但變成2D,第二維長度僅有1

#y=dataset.iloc[:,cols-1:].values.ravel()

#用ravel() 把2D降維成1D

y剛好在最後一欄比較好切片

若沒在最後一欄,

原本的drop比較好用

“””

from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest =\
train_test_split(x,y,test_size=0.3,
random_state=42,shuffle=True)
print(“The shpae of training data(X axis):”,xtrain.shape)
print(“The shpae of training data(Y axis):”,ytrain.shape)
print(“The shpae of testing data(X axis):”,xtest.shape)
print(“The shpae of testing data(Y axis):”,ytest.shape)

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

506*0.3 = 152 (test_size=0.3)

506-152 = 354

 

改用iloc做切片

.ravel() 將2D降維成1D

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

實測random_state跟shuffle參數:

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

若真的需要都次取的都不一樣

random_state = None

shuffle = True

其他組合每次都會

取出一樣的samples

每次都一樣,

會比較容易debug

或判斷模型的優劣

random_state設了一樣的種子

shuffle = True也無效

每次都會取出一樣的資料

 

第四個組合

random_state = None

shuffle = False

沒種子,沒洗牌

為什麼每次都一樣?

from官網: 

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

在 train_test_split 函數中,random_state 和 shuffle 參數看似相似但實際功能不同。

shuffle 參數

shuffle 是一個布爾值參數,控制是否要在分割前對數據進行隨機打亂:

  • 類型:True/False
  • 默認值:True
  • 作用:決定是否打亂原始數據順序
  • 何時設為 False:處理時間序列數據時,希望保持數據的時間順序

random_state 參數

random_state 控制如何進行隨機打亂:

  • 類型:整數、None 或 RandomState 實例
  • 默認值:None
  • 作用:設定隨機種子,控制數據的打亂方式
  • 意義:確保實驗可重複性和結果穩定性

兩者關係

  • 當 shuffle=True 時,random_state 發揮作用:

    • random_state=42(或任何固定值):每次運行結果相同
    • random_state=None:每次運行結果不同
  • 當 shuffle=False 時,random_state 不起作用,因為不需要打亂數據

實例對比

# 情況1:打亂數據 + 固定結果
# 每次運行代碼,得到的訓練/測試集都相同
train_test_split(X, y, shuffle=True, random_state=42)

# 情況2:打亂數據 + 隨機結果
# 每次運行代碼,得到不同的訓練/測試集
train_test_split(X, y, shuffle=True, random_state=None)  # 或省略 random_state

# 情況3:保持順序切分
# 按原始數據順序,從前70%選訓練集,後30%為測試集
train_test_split(X, y, shuffle=False)  # random_state 在此無效
 
總結:shuffle 決定「是否打亂」,而 random_state 決定「以什麼方式打亂」。
在需要結果可重複的情況下,應固定 random_state 的值。

推薦hahow線上學習python: https://igrape.net/30afN

波士頓地區房價:

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

boston (整齊版):

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

 

boston(排版較亂): http://lib.stat.cmu.edu/datasets/boston

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

要如以下才能拼接回資料

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

import pandas as pd
import numpy as np
url = r”http://lib.stat.cmu.edu/datasets/boston”
dfBoston = pd.read_csv(url,sep= “\s+” ,skiprows=22,header=None)

#正則表示法, 一個以上的空白或Tab
x=np.hstack( [ dfBoston.values[::2,:],dfBoston.values[1::2,:2] ] )

y=dfBoston.values[1::2,2]

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

x:

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

部分y:

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

y.shape

Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

推薦hahow線上學習python: https://igrape.net/30afN

加入好友
加入社群
Python機器學習: train_test_split() 切割資料(波士頓地區房價)為訓練資料跟測試資料; from sklearn.model_selection import train_test_split ; xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=42, shuffle=True) - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *