小编Cox*_*Tox的帖子

在mutate中粘贴变量名(dplyr)

我尝试在mutate_()函数(dplyr)中使用paste()创建变量.

我尝试用这个答案来调整代码(dplyr - mutate:使用动态变量名称),但它不起作用......

注意:nameVarPeriod1是函数的参数.

nameVarPeriod1=A2
df <- df %>%
    group_by(segment) %>%
    mutate_((.dots=setNames(mean(paste0("Sum",nameVarPeriod1)), paste0("MeanSum",nameVarPeriod1))))
Run Code Online (Sandbox Code Playgroud)

这会返回一个警告:

Warning message:
In mean.default(paste0("Sum", nameVarPeriod1)) :
  argument is not numeric or logical: returning NA
Run Code Online (Sandbox Code Playgroud)

如何评估paste0中的字符串作为变量名?

当我用这个替换paste0时它工作正常:

df <- df %>%
    group_by(segment) %>%
    mutate(mean=mean(SumA2))
Run Code Online (Sandbox Code Playgroud)

数据:

structure(list(segment = structure(c(5L, 1L, 4L, 2L, 2L, 14L, 
11L, 6L, 14L, 1L), .Label = c("Seg1", "Seg2", "Seg3", "Seg4", 
"Seg5", "Seg6", "Seg7", "Seg8", "Seg9", "Seg10", "Seg11", "Seg12", 
"Seg13", "Seg14"), class = "factor"), SumA2 = c(107584.9, 127343.87, 
205809.54, 138453.4, 24603.46, …
Run Code Online (Sandbox Code Playgroud)

r dplyr

7
推荐指数
1
解决办法
3297
查看次数

AttributeError:'GridSearchCV'对象没有属性'cv_results_'

我尝试应用此代码:

pipe = make_pipeline(TfidfVectorizer(min_df=5), LogisticRegression())
param_grid = {'logisticregression__C': [ 0.001, 0.01, 0.1, 1, 10, 100],
              "tfidfvectorizer__ngram_range": [(1, 1),(1, 2),(1, 3)]} 

grid = GridSearchCV(pipe, param_grid, cv=5)
grid.fit(text_train, Y_train)

scores = grid.cv_results_['mean_test_score'].reshape(-1, 3).T
# visualize heat map
heatmap = mglearn.tools.heatmap(
scores, xlabel="C", ylabel="ngram_range", cmap="viridis", fmt="%.3f",
xticklabels=param_grid['logisticregression__C'],
yticklabels=param_grid['tfidfvectorizer__ngram_range'])
plt.colorbar(heatmap)
Run Code Online (Sandbox Code Playgroud)

但我有这个错误:

AttributeError: 'GridSearchCV' object has no attribute 'cv_results_'
Run Code Online (Sandbox Code Playgroud)

python machine-learning text-mining scikit-learn

6
推荐指数
3
解决办法
2万
查看次数

计算特征与目标变量之间的相关性

计算我的特征和目标变量之间的相关性的最佳解决方案是什么?我的数据框有1000行和40000列...

范例:

df = pd.DataFrame([[1, 2, 4 ,6], [1, 3, 4, 7], [4, 6, 8, 12], [5, 3, 2 ,10]], columns=['Feature1', 'Feature2','Feature3','Target'])
Run Code Online (Sandbox Code Playgroud)

这段代码可以正常工作,但是对我的数据帧来说太长了……我只需要关联矩阵的最后一列:与目标的关联(而不是成对特征关联)。

corr_matrix=df.corr()
corr_matrix["Target"].sort_values(ascending=False)
Run Code Online (Sandbox Code Playgroud)

np.corcoeff()函数的工作原理与数组,但我们可以排除配对功能相关?

python numpy correlation dataframe

2
推荐指数
2
解决办法
6239
查看次数