How can I access each estimater in scikit-learn pipelines? [closed]

2024/11/20 14:23:07

How can I access "Log" in pipelines?

pipelines = {"Log": Pipeline([("scl", StandardScaler()), ("est", LogisticRegression(random_state=1))]),"Rf": Pipeline([("est", RandomForestClassifier(random_state=1))]),"Rf_Pipeline": Pipeline([("scl", StandardScaler()),("reduct", PCA(n_components=10, random_state=1)),("est", RandomForestClassifier(random_state=1)),]),
}Pipelines.item(Log)

Currently I get:

NameError: name 'Log' is not defined
Answer

The pipeline objects can be viewed as dictionaries. In your case, you have stored multiple pipelines into a dictionary. To access the different keys (pipelines) you can simply use dict['key'] or dict.get['key'].

  1. For the first level (sub pipelines), simply use dict['key']
  2. For the second level, (steps inside sub pipelines), again, you can fetch the dict with steps using named_steps and then refer to each step the same way.

Here is a the code -

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestClassifierpipelines = {"Log": Pipeline([("scl", StandardScaler()), ("est", LogisticRegression(random_state=1))]),"Rf": Pipeline([("est", RandomForestClassifier(random_state=1))]),"Rf_Pipeline": Pipeline([("scl", StandardScaler()),("reduct", PCA(n_components=10, random_state=1)),("est", RandomForestClassifier(random_state=1)),]),
}first_subpipeline = pipelines['Log']
second_step_first_subpipeline =  pipelines['Log'].named_steps['est']print(first_subpipeline)
print(second_step_first_subpipeline)
Pipeline(steps=[('scl', StandardScaler()),('est', LogisticRegression(random_state=1))])LogisticRegression(random_state=1)
https://en.xdnf.cn/q/118404.html

Related Q&A

How do I structure a repo with Cloud Run needing higher level code?

I have added code to a repo to build a Cloud Run service. The structure is like this:I want to run b.py in cr. Is there any way I can deploy cr without just copying b.py into the cr directory? (I dont…

Unable to build kivy image loaded .py file into exe using auto-py-to-exe

I have a simple kivy file in which i want to cover the entire canvas with an image bgi.jpg MainWidget: <MainWidget>:canvas.before:Rectangle:size:self.sizesource:bgi.jpgand the .py file code i…

Pandas Panel is deprecated,

This code snippet is from one of my script which works fine in current panda version (0.23) but Panel is deprecated and will be removed in a future version.panel = pd.Panel(dict(df1=dataframe1,df2=data…

Python - Why is this data being written to file incorrectly?

Only the first result is being written to a csv, with one letter of the url per row. This is instead of all urls being written, one per row.What am I not doing right in the last section of this code t…

How does Python interpreter look for types? [duplicate]

This question already has answers here:How does Python interpreter work in dynamic typing?(3 answers)Closed 10 months ago.If I write something like:>>> a = float()how does Python interpreter …

title() method in python writing functions when word like arent

using functiondef make_cap(sentence):return sentence.title()tryining outmake_cap("hello world") Hello World# it workd but when I have world like "arent" and isnt". how to write…

Creating a C++ Qt Gui for a Python logic

I was presented with a Python logic for which I need to create a GUI. I want to use Qt for that purpose and ideally I would like to program it in C++, without using the Qt Creator.What are recommended …

Pythons BaseHTTPServer returns junky responses

I use Pythons BaseHTTPServer and implement the following very simple BaseHTTPRequestHandler:class WorkerHandler(BaseHTTPRequestHandler):def do_GET(self):self.wfile.write({"status" : "rea…

Why is matplotlib failing on import matplotlib.pyplot as plt

I installed matplotlib using conda:conda install matplotlibThe following code failed:#!/usr/bin/env python import matplotlib import matplotlib.pyplot as pltWith this error message:"ImportError: N…

Setting cell color of matplotlib table and save as a figure?

Im following this code a link! to save a table as the image, and I have some feature like check value in a cell then set color for a cell, but I added some code stylemap, it doesnt workimport pandas a…