乳腺癌的數據準備函數
> par(ask=TRUE) #佈局能夠恢復 > loc <- "http://archive.ics.uci.edu/ml/machine-learning-databases/" > ds <- "breast-cancer-wisconsin/breast-cancer-wisconsin.data" > url <- paste(loc, ds, sep="") > breast <- read.table(url,sep = ",",header = F,na.strings = "?") #sep=","間隔,na,strings="?":「?」的都用NA表示 > names(breast) <- c("ID", "clumpThickness", "sizeUniformity", #names(brest)獲取breast數據框的變量名組成的字符向量 + "shapeUniformity", "maginalAdhesion", #將右邊的變量字符向 + "singleEpithelialCellSize", "bareNuclei", #量賦值給breast的變量 + "blandChromatin", "normalNucleoli", "mitosis", "class") > df <- breast[-1] #剔除第一個變量(實際數據框中第一個變量名爲ID,沒有用因此剔除) > df$class <- factor(breast$class,levels=c(2,4),labels=c("benign","malignant")) ##將class變量設置爲因子 #-------設置訓練集和驗證集 > set.seed(1234) #set.seed()讓隨機抽樣的結果可複製 > train <- sample(nrow(df),0.7*nrow(df)) #sample(x,size)從x總獲取size大小的隨機數,nrow(df)獲取df的函數,0.7*(nronw)df或70%的 > df.train <- df[train,] #獲取70%行的數據框數據做爲訓練集 > table(df.train$class) #使用N個類別型變量(因子)建立一個N維列聯表並進行簡單的頻數統計 benign malignant 329 160 > df.validate <- df[-train,] #剔除訓練集剩下的就是驗證集 > table(df.validate$class) benign malignant 129 81