我有以下数据框,其中包含密码及其强度(范围为 0-4):
> head(passwords_data)
password score
1 kzde5577 2
2 kino3434 2
3 visi7k1yr 2
4 megzy123 2
5 lamborghin1 1
6 AVYq1lDE4MgAZfNt 4
我想使用 R 中的随机森林包构建密码强度分类模型。将输入(密码列)更改为数字类型的最佳方法是什么? 在 python 中,我看到使用了标记化和 TfidfVectorizer。 如何在 R 中处理这个问题以正确构建模型?
到目前为止我的代码:
split <- sample.split(passwords_data$password, SplitRatio = 0.8)
train <- subset(passwords_data, split == "TRUE")
test <- subset(passwords_data, split == "FALSE")
rf <- randomForest(password~., data = train, ntree = 500) # here I'm getting error "Error in y - ymean : non-numeric argument to binary operator..."
您将从文本挖掘和 NLP 包中找到标记化、特征矩阵和 tf-idf 方法(CRAN NLP 任务视图)。例如,使用
quanteda
,您可以链接 tokens() |> dfm() |> dfm_tfidf()
以获得适合 randomForest()
的特征矩阵。为了获得可比较的结果,您可能应该检查 sklearn
和 quanteda
方法的默认值,并可能考虑默认值如何适合此特定应用程序,例如默认情况下 dfm()
将特征转换为小写。
library(quanteda)
library(randomForest)
passwords_data <- tibble::tribble(
~password, ~score,
"kzde5577", 2,
"kino3434", 2,
"visi7k1yr", 2,
"megzy123", 2,
"lamborghin1", 1,
"AVYq1lDE4MgAZfNt", 4,
)
# tokenized to characters
pw_tokens <- tokens(passwords_data$password, "character")
pw_tokens |> as.list() |> str()
#> List of 6
#> $ text1: chr [1:8] "k" "z" "d" "e" ...
#> $ text2: chr [1:8] "k" "i" "n" "o" ...
#> $ text3: chr [1:9] "v" "i" "s" "i" ...
#> $ text4: chr [1:8] "m" "e" "g" "z" ...
#> $ text5: chr [1:11] "l" "a" "m" "b" ...
#> $ text6: chr [1:16] "A" "V" "Y" "q" ...
# document-feature matrix
pw_dfm <- dfm(pw_tokens)
print(pw_dfm, max_nfeat = 30)
#> Document-feature matrix of: 6 documents, 26 features (65.38% sparse) and 0 docvars.
#> features
#> docs k z d e 5 7 i n o 3 4 v s 1 y r m g 2 l a b h q f t
#> text1 1 1 1 1 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> text2 1 0 0 0 0 0 1 1 1 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> text3 1 0 0 0 0 1 2 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
#> text4 0 1 0 1 0 0 0 0 0 1 0 0 0 1 1 0 1 1 1 0 0 0 0 0 0 0
#> text5 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 1 1 0 1 1 1 1 0 0 0
#> text6 0 1 1 1 0 0 0 1 0 0 1 1 0 1 1 0 1 1 0 1 2 0 0 1 1 1
# tf-idf
pw_tfidf <- dfm_tfidf(pw_dfm)
# first 15 tokens
as.matrix(pw_tfidf)[,1:15] |> round(2)
#> features
#> docs k z d e 5 7 i n o 3 4 v s 1 y
#> text1 0.3 0.3 0.48 0.3 1.56 0.95 0.0 0.0 0.00 0.00 0.00 0.00 0.00 0.00 0.0
#> text2 0.3 0.0 0.00 0.0 0.00 0.00 0.3 0.3 0.48 0.95 0.95 0.00 0.00 0.00 0.0
#> text3 0.3 0.0 0.00 0.0 0.00 0.48 0.6 0.0 0.00 0.00 0.00 0.48 0.78 0.18 0.3
#> text4 0.0 0.3 0.00 0.3 0.00 0.00 0.0 0.0 0.00 0.48 0.00 0.00 0.00 0.18 0.3
#> text5 0.0 0.0 0.00 0.0 0.00 0.00 0.3 0.3 0.48 0.00 0.00 0.00 0.00 0.18 0.0
#> text6 0.0 0.3 0.48 0.3 0.00 0.00 0.0 0.3 0.00 0.00 0.48 0.48 0.00 0.18 0.3
# random forest for regression
pw_rf <- randomForest(x = as.matrix(pw_tfidf), y = passwords_data$score)
passwords_data$rf_predict <- apply(pw_tfidf, 1, \(x) predict(pw_rf, x))
passwords_data
#> # A tibble: 6 × 3
#> password score rf_predict
#> <chr> <dbl> <dbl>
#> 1 kzde5577 2 2.08
#> 2 kino3434 2 2.00
#> 3 visi7k1yr 2 1.95
#> 4 megzy123 2 2.00
#> 5 lamborghin1 1 1.76
#> 6 AVYq1lDE4MgAZfNt 4 3.20
创建于 2023-08-22,使用 reprex v2.0.2