我适合一个Pipeline物体RandomizedSearchCV
pipe_sgd = Pipeline([('scl', StandardScaler()),
('clf', SGDClassifier(n_jobs=-1))])
param_dist_sgd = {'clf__loss': ['log'],
'clf__penalty': [None, 'l1', 'l2', 'elasticnet'],
'clf__alpha': np.linspace(0.15, 0.35),
'clf__n_iter': [3, 5, 7]}
sgd_randomized_pipe = RandomizedSearchCV(estimator = pipe_sgd,
param_distributions=param_dist_sgd,
cv=3, n_iter=30, n_jobs=-1)
sgd_randomized_pipe.fit(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)
我想访问该coef_属性,best_estimator_但我无法做到这一点.我尝试使用coef_下面的代码访问.
sgd_randomized_pipe.best_estimator_.coef_
但是我得到以下AttributeError ...
AttributeError:'Pipeline'对象没有属性'coef_'
scikit-learn文档说这coef_是属性SGDClassifier,属于我的类base_estimator_.
我究竟做错了什么?
我想遍历数据框中的列并根据分隔符将它们拆分为 。我正在使用tidyr::separate,它在我一次做一列时有效。
例如:
df<- data.frame(a = c("5312,2020,1212"), b = c("345,982,284"))
df <- separate(data = df, col = "a",
into = paste("a", c("col1", "col2", "col3"),
sep = "_"), sep = ",")
Run Code Online (Sandbox Code Playgroud)
返回:
a_col1 a_col2 a_col3 b
1 5312 2020 1212 345,982,284
Run Code Online (Sandbox Code Playgroud)
当我尝试对dfR 的每一列执行相同的操作时返回错误
例如我用这个 for 循环:
for(col in names(df)){
df <- separate(data = df, col = col,
into = paste(col, c("col1", "col2", "col3),
sep = "_"), sep = ",")
}
Run Code Online (Sandbox Code Playgroud)
我期待得到以下输出:
a_col1 a_col2 a_col3 b_col1 b_col2 b_col3 …Run Code Online (Sandbox Code Playgroud) 在python中我们可以做到这一点..
numbers = [1, 2, 3]
characters = ['foo', 'bar', 'baz']
for item in zip(numbers, characters):
print(item[0], item[1])
(1, 'foo')
(2, 'bar')
(3, 'baz')
Run Code Online (Sandbox Code Playgroud)
我们也可以解包元组而不是使用索引。
for num, char in zip(numbers, characters):
print(num, char)
(1, 'foo')
(2, 'bar')
(3, 'baz')
Run Code Online (Sandbox Code Playgroud)
我们如何使用基础 R 做同样的事情?
我希望在满足现有列中的条件时递增从1重新开始的计数.
例如,我有以下数据框:
df <- data.frame(x1 = c(10, 100, 200, 300, 87, 90, 45, 80),
x2 = c("start", "a", "b", "c", "start", "k", "l", "o"))
Run Code Online (Sandbox Code Playgroud)
我想创建x3每次从1开始计数x2 == "start".
生成的数据框应如下所示:
x1 x2 x3
1 10 start 1
2 100 a 2
3 200 b 3
4 300 c 4
5 87 start 1
6 90 k 2
7 45 l 3
8 80 o 4
Run Code Online (Sandbox Code Playgroud)
我猜测R中有现有的功能可以提供一般解决方案.谁能指出我正确的方向?
假设我有多个数据框,每个数据框都有相同的列名,尽管这些列的内容不一定相同。
我试图遍历每个数据框的列并按名称删除列,但这不起作用。如果我分别在每个数据帧上尝试相同的方法,它就会起作用
我有以下数据框:
import pandas as pd
df1 = pd.DataFrame({'user': ['John', 'Joe', 'Alice'],
'income': [40000, 50000, 42000],
'Unnamed: 0': [1, 2, 3]})
df2 = pd.DataFrame({'user': ['Luke', 'Paul', 'Jane'],
'income': [40000, 50000, 42000],
'Unnamed: 0': [1, 2, 3]})
df3 = pd.DataFrame({'user': ['Sue', 'Haley', 'Erica'],
'income': [40000, 50000, 42000],
'Unnamed: 0': [1, 2, 3]})
Run Code Online (Sandbox Code Playgroud)
我试过这个,没有成功:
dataframes = [df1, df2, df3]
for df in dataframes:
for column in df.columns:
if "Unnamed" in column:
df = df.drop(column, axis = 1)
Run Code Online (Sandbox Code Playgroud)
这并没有改变 中的任何项目dataframes。 …