假设我有一些Python代码,如下所示:
input = open("input.txt")
x = (process_line(line) for line in input)
y = (process_item(item) for item in x)
z = (generate_output_line(item) + "\n" for item in y)
output = open("output.txt", "w")
output.writelines(z)
Run Code Online (Sandbox Code Playgroud)
此代码从输入文件中读取每一行,通过多个函数运行它,并将输出写入输出文件.现在我知道函数process_line,process_item并且generate_output_line永远不会相互干扰,让我们假设输入和输出文件位于不同的磁盘上,这样读写就不会相互干扰.
但Python可能不知道这些.我的理解是Python将读取一行,依次应用每个函数,并将结果写入输出,然后只有在将第一行发送到输出后才会读取第二行,这样第二行就不会进入管道直到第一个退出.我是否正确理解该程序将如何流动?如果这是它的工作方式,是否有任何简单的方法可以使多个行同时在管道中,以便程序并行读取,写入和处理每个步骤?
请对我放轻松。我正在将职业转向数据科学,并且没有 CS 或编程背景——所以我可能会做一些非常愚蠢的事情。我已经研究了几个小时没有成功。
目标:让 Pipeline 与 OrdinalEncoder 一起运行。
问题:代码无法在 OrdinalEncoder 调用下运行。它确实在没有 OrdinalEncoder 的情况下运行。作为最好的,我可以告诉我可以通过两个参数,即类和D型。都不帮忙。
我正在将公共糖尿病数据集传递给模型。这是问题吗?IOW,将高基数特征传递给 OrdinalEncoder 是否会在构建模型后导致训练/测试数据之间出现问题,即测试分割具有训练集没有的值?
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OrdinalEncoder
from sklearn.ensemble import RandomForestClassifier
pipe = Pipeline([
('imputer', SimpleImputer()),
('ordinal_encoder', OrdinalEncoder()),
('classifier', RandomForestClassifier(criterion='gini', n_estimators=100))])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Construct model
model = pipe.fit(X_train, y_train)
# Show results
print("Hold-out AUC score: %.3f" %roc_auc_score(model.predict_proba(X_test),y_test))
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
ValueError: Found unknown categories [17.0] in column 0 …Run Code Online (Sandbox Code Playgroud) 背景:我的解决方案中很少有项目依赖于 COM 库。因此,必须在构建实际解决方案之前注册这些 COM DLL。
在 Azure DevOps - Pipeline - Build - Task 中,我添加了一个“命令行”代理作业,使用以下命令,
场景一:
C:\windows\system32\regsvr32.exe /s [DLLFilePath]\[DLLName].dll
场景二:
CD [DLLFilePath]
C:\windows\system32\regsvr32.exe /s [DLLName].dll
但是这两种方案在构建时都返回相同的错误,
[error]Cmd.exe 以代码“3”退出。
注意:
在调用 regsvr32 之前,使用单独的代理作业将 DLL 复制到上述位置。
[DLLFilePath]\[DLLName].dll 是构建代理中的本地路径,比如 c:\..\someLibrary.dll
我的sklearn管道中有一个自定义 Transformer,我想知道如何将参数传递给我的 Transformer :
在下面的代码中,您可以看到我在 Transformer 中使用了字典“权重”。我不希望在我的 Transformer 中定义这个字典,而是从管道传递它,这样我就可以在网格搜索中包含这个字典。是否可以将字典作为参数传递给我的 Transformer ?
# My custom Transformer
class TextExtractor(BaseEstimator, TransformerMixin):
"""Concat the 'title', 'body' and 'code' from the results of
Stackoverflow query
Keys are 'title', 'body' and 'code'.
"""
def fit(self, x, y=None):
return self
def transform(self, x):
# here is the parameter I want to pass to my transformer
weight ={'title' : 10, 'body': 1, 'code' : 1}
x['text'] = weight['title']*x['Title'] +
weight['body']*x['Body'] +
weight['code']*x['Code']
return x['text']
param_grid = …Run Code Online (Sandbox Code Playgroud) 我正在尝试为 sklearn 管道创建一个自定义转换器,它将提取特定文本的平均字长,然后对其应用标准缩放器以标准化数据集。我正在将一系列文本传递给管道。
class AverageWordLengthExtractor(BaseEstimator, TransformerMixin):
def __init__(self):
pass
def average_word_length(self, text):
return np.mean([len(word) for word in text.split( )])
def fit(self, x, y=None):
return self
def transform(self, x , y=None):
return pd.DataFrame(pd.Series(x).apply(self.average_word_length))
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个这样的管道。
pipeline = Pipeline(['text_length', AverageWordLengthExtractor(),
'scale', StandardScaler()])
Run Code Online (Sandbox Code Playgroud)
当我在这条管道上执行 fit_transform 时,我收到错误消息,
File "custom_transformer.py", line 48, in <module>
main()
File "custom_transformer.py", line 43, in main
'scale', StandardScaler()])
File "/opt/conda/lib/python3.6/site-packages/sklearn/pipeline.py", line 114, in __init__
self._validate_steps()
File "/opt/conda/lib/python3.6/site-packages/sklearn/pipeline.py", line 146, in _validate_steps
names, estimators = zip(*self.steps)
TypeError: zip argument #2 must support …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Pipeline来自的类imblearn并GridSearchCV获得用于对不平衡数据集进行分类的最佳参数。由于每答案提到这里,我要离开了验证集的重采样,只有重新采样训练集,其中imblearn的Pipeline似乎是在做。但是,在实施已接受的解决方案时出现错误。请让我知道我做错了什么。下面是我的实现:
def imb_pipeline(clf, X, y, params):
model = Pipeline([
('sampling', SMOTE()),
('classification', clf)
])
score={'AUC':'roc_auc',
'RECALL':'recall',
'PRECISION':'precision',
'F1':'f1'}
gcv = GridSearchCV(estimator=model, param_grid=params, cv=5, scoring=score, n_jobs=12, refit='F1',
return_train_score=True)
gcv.fit(X, y)
return gcv
for param, classifier in zip(params, classifiers):
print("Working on {}...".format(classifier[0]))
clf = imb_pipeline(classifier[1], X_scaled, y, param)
print("Best parameter for {} is {}".format(classifier[0], clf.best_params_))
print("Best `F1` for {} is {}".format(classifier[0], clf.best_score_))
print('-'*50)
print('\n')
Run Code Online (Sandbox Code Playgroud)
参数:
[{'penalty': ('l1', 'l2'), 'C': (0.01, …Run Code Online (Sandbox Code Playgroud) 我使用质谱数据创建了许多数据处理管道,其中来自仪器的数据被清理、转换、缩放、检查和最终分析。我倾向于为此使用递归类型定义——这是一个非常简化的例子:
data Dataset = Initial { x::(Vector Double), y::(Vector Double) name::String}
| Cleaned { x::(Vector Double), y::(Vector Double) name::String}
| Transformed { x::(Vector Double), y::(Vector Double) name::String}
Run Code Online (Sandbox Code Playgroud)
那么一个典型的管道将只是一个以Dataset创建者开始的函数链,然后继续消耗类型的函数Dataset,并产生类型的东西Dataset:
createDataset :: Vector Double -> Vector Double -> String -> Dataset
createDataset x y name = Initial x y name
removeOutliers :: Dataset -> Dataset
removeOutliers (Initial x y n) = let
(new_x, new_y) = outlierRemovalFunction x y
in Cleaned new_x new_y (n ++"_outliersRemoved")
(Cleaned x …Run Code Online (Sandbox Code Playgroud) 我正在snakemake构建一个可以在 SLURM 集群上运行的变体调用管道。集群有登录节点和计算节点。srun任何真正的计算都应该以or作业的形式在计算节点上完成sbatch。作业的运行时间限制为 48 小时。我的问题是,处理许多样本,尤其是当队列繁忙时,将需要超过 48 小时来处理每个样本的所有规则。传统的集群执行使snakemake主线程保持运行,仅在所有规则的依赖项完成运行后才将规则提交到队列。我应该在计算节点上运行这个主程序,因此这将整个管道的运行时间限制为 48 小时。
我知道 SLURM 作业有依赖指令,告诉作业等待运行,直到其他作业完成。由于snakemake工作流是一个 DAG,是否可以一次提交所有作业,并且每个作业都具有由 DAG 中的规则依赖项定义的依赖项?提交所有作业后,主线程将完成,从而绕过 48 小时的限制。这是否可能snakemake,如果可以,它是如何工作的?我找到了--immediate-submit命令行选项,但我不确定这是否具有我正在寻找的行为以及如何使用该命令,因为我的集群在Submitted batch job [id]作业提交到队列后打印,而不仅仅是作业 ID。
我收到错误:“一个步骤不能同时具有uses和run键”,但我没有看到一个步骤同时具有uses和run。有人可以帮我弄清楚这有什么问题吗?
on:
pull_request:
branches:
- master
env:
IMAGE_NAME: api
jobs:
build:
name: Application build
runs-on: ubuntu-latest
steps:
- name: Checkout repository (#1)
uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.101
- name: Build API
run: dotnet build --configuration Release
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout repository (#2)
uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.101
- name: Run API Tests
run: dotnet test …Run Code Online (Sandbox Code Playgroud) https://tryfsharp.fsbolero.io/
printfn "Hello"
Run Code Online (Sandbox Code Playgroud)
但是,使用管道运算符可以按预期工作,没有错误
"Hello" |> printfn
Run Code Online (Sandbox Code Playgroud)
类型“string”与类型“Printf.TextWriterFormat”不兼容
我了解管道操作员的行为:
f(a)相当于a |> f
为什么后者会产生错误?谢谢。
pipeline ×10
scikit-learn ×4
python ×3
python-3.x ×3
azure ×1
azure-devops ×1
f# ×1
function ×1
github ×1
haskell ×1
imblearn ×1
iterator ×1
ordinal ×1
slurm ×1
snakemake ×1
type-systems ×1
valueerror ×1