使用R中的Lime包,我想解释一下我的RF模型.
我在github上看到了一个例子(向下滚动到"示例"部分):( https://github.com/thomasp85/lime)
我尝试运行完全相同的代码,但使用我自己的数据(我的数据添加在下面).但是,每次我收到同样的错误.有人可以帮我从这里出去吗?
我理解我的代码与示例中的代码完全相同,包括我的数据的str().出了什么问题?我怎样才能做到这一点?
请注意,我的目标变量是第一列(称为目标),并且我的所有数据都是二进制数据.
library(caret)
library(lime)
total <- data # the data set is posted below
# Split up the data set
iris_test <- total[2:6,] # I just used the iris name (as in the example)
iris_train <- total[-(2:6),]
iris_lab <- total[[1]][-(2:6)]
iris_lab <- as.factor(iris_lab)
# Create Random Forest model on iris data
model <- train(iris_train, iris_lab, method = 'rf')
Run Code Online (Sandbox Code Playgroud)
第一个警告:警告消息:1:不建议在tibble上设置行名.
# Create an explainer object
explainer <- lime(iris_train, model)
Run Code Online (Sandbox Code Playgroud)
第二个警告:警告消息:数据包含方差为零的数字列
# Explain new observation
explanation …Run Code Online (Sandbox Code Playgroud) 我是计算机科学的新手(希望我使用正确的单词并提供正确的信息来回答这个问题),希望你们能为我提供帮助。我想在两个.NET解决方案之间自动传输(JSON)数据。
我在.NET中有两个解决方案:
Bot Framework SDK-在.NET Core 2.2中构建(https://docs.microsoft.com/zh-cn/azure/bot-service/dotnet/bot-builder-dotnet-sdk-quickstart?view=azure-bot-服务4.0)
Qlik Sense .Net SDK。-在.NET Framework 4.5.2中构建(https://github.com/kolsrud/qlik-dot-net-sdk-hypercube-usage)
我想让这两种解决方案相互交流。
一方面: 在QlikSense.NET SDK中,我们将所有数据存储到一个变量中(假设变量'Sales',保存JSON数据),如果此变量已更新,我想将其传输到Bot框架(并存储此处的数据)。
另一方面:Bot Framework中有一个变量(假设变量'Query',保存JSON数据),如果该变量已更新,我想将其传递给Qliksense SDK。
总结:我想在Qliksense SDK和Bot框架SDK之间自动传输(JSON)数据。我怎样才能做到这一点?
再次,抱歉,如果不清楚。预先感谢您的帮助!感激!
编辑1:
我尝试了什么?正如Selvin所问:
我唯一能想到的就是将JSON变量写入文本文件,然后从其他脚本读取该文本文件。但是,我认为这不是适当的“ 解决方案 ”。
然而,我正在挣扎。我有一列(包含字符串值),例如:
Sex
Male
Female
# This is just as example, in real it is having much more unique values
Run Code Online (Sandbox Code Playgroud)
现在问题来了。我收到了一个新的(唯一的)值,现在我无法再进行预测(例如'Neutral'添加了)。
由于我正在将'Sex'列转换为 Dummies,我确实遇到了我的模型不再接受输入的问题,
模型的特征数量必须与输入匹配。模型 n_features 为 2,输入 n_features 为 3
因此我的问题是:有没有办法让我的模型健壮,而忽略这个类?但是在没有具体信息的情况下进行预测?
我尝试过的:
df = pd.read_csv('dataset_that_i_want_to_predict.csv')
model = pickle.load(open("model_trained.sav", 'rb'))
# I have an 'example_df' containing just 1 row of training data (this is exactly what the model needs)
example_df = pd.read_csv('reading_one_row_of_trainings_data.csv')
# Checking for missing columns, and adding that to …Run Code Online (Sandbox Code Playgroud) 我是新手,对 Python 了解不多。有人知道如何在 while 循环中编写阶乘吗?
我可以在 if / elif else 语句中实现:
num = ...
factorial = 1
if num < 0:
print("must be positive")
elif num == 0:
print("factorial = 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print(num, factorial)
Run Code Online (Sandbox Code Playgroud)
但我想用一个 while 循环(无功能)来做到这一点。
我目前正在编写一个文本挖掘文档,我想从文本中提取相关关键字(请注意,我有很多很多文本文档)。
我正在使用 udpipe 包。一个很棒的 Vignette 在线 ( http://bnosac.be/index.php/blog/77-an-overview-of-keyword-extraction-techniques )。一切正常,但是当我运行代码时,该部分
x <- udpipe_annotate(ud_model, x = comments$feedback)
Run Code Online (Sandbox Code Playgroud)
真的非常慢(特别是当你有很多文本时)。有人知道如何更快地获得这部分吗?解决方法当然没问题。
library(udpipe)
library(textrank)
## First step: Take the Spanish udpipe model and annotate the text. Note: this takes about 3 minutes
data(brussels_reviews)
comments <- subset(brussels_reviews, language %in% "es")
ud_model <- udpipe_download_model(language = "spanish")
ud_model <- udpipe_load_model(ud_model$file_model)
x <- udpipe_annotate(ud_model, x = comments$feedback) # This part is really, really slow
x <- as.data.frame(x)
Run Code Online (Sandbox Code Playgroud)
提前谢谢了!
目前,我正在开发一个大数据集.我在此任务中唯一要做的就是预处理数据.
当我运行我的代码时,我看到我的计算机内存增加得非常快:
binary <- ifelse(subset_variables1 == "0", 0, 1)
Run Code Online (Sandbox Code Playgroud)
该行唯一应该做的是将所有值都设为二进制.这可以更快地完成吗?或者这是一种很好的方式(我必须处理内存问题).
我有一个思考问题(不确定堆栈上是否已有解决方案,但我不知道如何解决这个问题).
我有一个这样的数据框:
ID Visits Time X Y Z
1 2 2016-05-15 06:38:40 1 1 0
1 4 2016-05-15 07:38:40 0 0 1
1 2 2016-05-15 08:38:40 0 1 0
2 3 2016-05-15 09:38:40 1 0 2
3 2 2016-05-15 10:38:40 0 1 0
3 1 2016-05-15 11:38:40 1 0 1
Run Code Online (Sandbox Code Playgroud)
我想制作一个新的数据框,包括:
所以结果应该是这样的:
ID Visits Time X Y Z
1 8 2016-05-15 06:38:40 1 2 1
2 3 2016-05-15 09:38:40 1 0 2
3 3 2016-05-15 10:38:40 1 …Run Code Online (Sandbox Code Playgroud) 我是新手,但有一个问题:
我有一个包含15列和33,000行的数据集(csv文件)。
当我在Excel中查看数据时看起来不错,但是当我尝试将数据加载到R-studio时出现问题:
我使用了代码:
x <- read.csv(file = "1energy.csv", head = TRUE, sep="")
View(x)
Run Code Online (Sandbox Code Playgroud)
结果是列名很好,但是数据(第2行及以后的数据)都在我的第一列中。
在第一列中,数据用分隔。。但是当我尝试代码时:
x1 <- read.csv(file = "1energy.csv", head = TRUE, sep=";")
Run Code Online (Sandbox Code Playgroud)
下一个问题是:read.table中的错误(文件=文件,标头=标头,sep = sep,引用=引号,:不允许重复的“ row.names”
所以我做了代码:
x1 <- read.csv(file = "1energy.csv", head = TRUE, sep=";", row.names = NULL)
Run Code Online (Sandbox Code Playgroud)
看起来好像可以正常工作...。但是现在数据位于错误的列中(例如,“名称”列现在包含“时间”值,而“时间”列包含“成本”值)。
有人知道如何解决此问题吗?我可以重命名列,但我认为这不是最好的方法。
我想通过选择列来制作我的数据的子集,如下所示:
select(df, col1, col2, col3, col4)
Run Code Online (Sandbox Code Playgroud)
但有时我的数据集略有不同,只有col1,col2和col4.
如何使用select(),如果列不存在,它会继续而不会出错?
因此它会给出一个带col1,col2和col4的数据集(并跳过col3).如果我只运行上面的select()行,我会收到此错误:
Error in overscope_eval_next(overscope, expr) : object 'col3' not found
Run Code Online (Sandbox Code Playgroud) 我有一个数据框(称为all_data),如下所示:
Title Text
Title_1 Very interesting word_1 and also keyword_2
Title_2 hello keyword_1, and keyword_3.
Run Code Online (Sandbox Code Playgroud)
我还有第二个数据框(称为keywords),如下所示:
keywords
word_1
word_2
word_3
word_4a word_4b word_4c
Run Code Online (Sandbox Code Playgroud)
我想在 all_data 数据框中创建一个额外的列。在本列中,如果某个关键字(来自关键字数据帧)出现在 all_data$Text 或 all_data$Title 列中,我想打印相关关键字。例如:
Title Text Keywords
Title_1 Very interesting word_1 and also word_2, word_1. word_1, word_2
Title_2 hello word_1, and word_3. word_1, word_3
Title_3 difficult! word_4b, and word_4a also word_4c word_4a word_4b word_4c
Run Code Online (Sandbox Code Playgroud)
!只需在 all_data$Words 列中打印一次单词,而不是多次。对我来说,更难的部分是打印一个“关键字”,例如:“keyword_A Keyword_A1 Keyword_A3”,只有当关键字的所有部分都出现在相关文本中时才会出现。
这个问题在这里得到了回答(识别列中的模式,并将它们添加到数据框中的列),我在其中使用了 DJack 他的解决方案:
ls <- strsplit(tolower(paste(all_data$Title, …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的数据框:
Product_ID Quantity Year Quarter
1 100 2021 1
1 100 2021 2
1 50 2021 3
1 100 2021 4
1 100 2022 1
2 100 2021 1
2 100 2021 2
3 100 2021 1
3 100 2021 2
Run Code Online (Sandbox Code Playgroud)
我想获取每个 Product_ID 的过去三个月(不包括当月)的总和。
因此我尝试了这个:
df['Qty_Sum_3qrts'] = (df.groupby('Product_ID'['Quantity'].shift(1,fill_value=0)
.rolling(3).sum().reset_index(0,drop=True)
)
# Shifting 1, because I want to exclude the current row.
# Rolling 3, because I want to have the 3 'rows' before
# Grouping by, because I want to …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据条件“加入”两个 DataFrame。
健康)状况
if df1.Year == df2.Year &
df1.Date >= df2.BeginDate or df1.Date <= df2.EndDate &
df1.ID == df2.ID
#if the condition is True, I would love to add an extra column (binary) to df1, something like
#df1.condition = Yes or No.
Run Code Online (Sandbox Code Playgroud)
我的数据如下所示:
df1:
Year Week ID Date
2020 1 123 2020-01-01 00:00:00
2020 1 345 2020-01-01 00:00:00
2020 2 123 2020-01-07 00:00:00
2020 1 123 2020-01-01 00:00:00
df2:
Year BeginDate EndDate ID
2020 2020-01-01 00:00:00 2020-01-02 00:00:00 123
2020 …Run Code Online (Sandbox Code Playgroud)