LibSVM (svm-rfe)

Hello

  1. How one can get the coefficients of the attributes/features from the SVM learner? I’m using libsvm and SVM (of Knime) and i don’t see how it works.
  2. Any helo in SVM-Rfe (Recursive feature Elimination ) in Knime?

Malik

Hi @malik,

Unfortunately, there is currently no way to get the coefficients.

Not that I am aware of, although it looks interesting. There seem to be implementations for Python as well as R that you could use with either integration.

Best,
Stefan

1 Like

@stelfrich Thanks for your reply
How one can use sklearn in Python node in Knime ? Do i need to install the sklearn ? How to do that and import it from the Python node?

Best
Malik

Hi @malik

Take a look at the different Python nodes available in KNIME Nodepit.
Maybe you have to install the Python nodes first: File -> Install KNIME Extensions.

KNIME uses your local Python configuration. Via File -> Preferences -> Python you have to add the path to your Python executable.

The data in the Python node is treated as Pandas dataframe. From there you can coding Python e.g. scikit-learn or whatever you like.

gr. Hans

2 Likes

Hi
I already have the Python nodes in Knime. I have installed sklearn through PyCharm. Unfortunately i cant use sklearn through python nodes.

Ok- I have used sklearn in Python node. Now i want to have as the output the weights/cofeits of the features.
I want to have it as two columns- the first one is the feature name while the second one the coef of it.
Also i have a problem with the float values. I see that i get most of the value as 0 or -0.
How to have the double values and have two columns

import pandas as pd 
from numpy import array, ones, linalg
from sklearn import svm, feature_selection
from sklearn.pipeline import Pipeline
from sklearn.pipeline import make_pipeline
from sklearn.feature_selection import RFE, RFECV
from sklearn.svm import SVC, LinearSVC
# Only use numeric columns
data = input_table._get_numeric_data()
target_column = input_table[input_table.columns[0]]
X=data
y=target_column
classifier = LinearSVC()
classifier.fit(X,y)
t= classifier.coef_
output_table= pd.DataFrame(classifier.coef_)

Hi,

I think, something like this will do the job:
pd.DataFrame({'Variables': X.columns, 'Coefficients': classifier.coef_[0]})
(assuming you have a binary classification problem and your inputs have the target column in a form of string labels).

The output values are what they are. There can be many reasons for the behaviour that you observe: for example, coefficients might be smaller that the displayed precision in KNIME tables (you can improve by Right click on column name in the output table -> Available Renderers -> Full Precision) or many of variables are irrelevant for the target or the regularisation strength C is too high for your problem. Understanding those coefficients is a task specific to your particular use-case.

Cheers,
Misha

3 Likes

Hi Thanks for your reply.
I’m coding a workflow to implement svm-rfe with Knime.
Im using Recursive loop’ unfortunately i’m getting the following error.
ERROR Recursive Loop End 5:385 Execute failed: Loop End is not connected to matching/corresponding Recursive Loop Start node.

The data is shrinking by columns not rows.
image

Hi Malik,

for the recursive loop start with 2 ports you need the loop end with 3 input ports:
https://hub.knime.com/knime/extensions/org.knime.features.base/latest/org.knime.base.node.meta.looper.recursive.RecursiveLoopEnd2NodeFactory
This one will match your loop start and this error will be gone.

On a side note: do you have a python script inside of the svm-rfe metanode? If so, such loop will not be fast as you will loose time initialising python environment on each loop iteration. How complex is your svm-rfe node? Would it make sense to do whole RFE in a single python node? There is a dedicated class in sklearn RFE sklearn (or see RFECV for the version with integrated cross-validation loop). This way you will initialise python only once and will also avoid loops in KNIME.

Cheers,
Misha

2 Likes

Yes now i have used RFE sklearn.
Attached is the knime workflow.

svm_rfe_v2_usingREFofSklearn.knwf (1.6 MB)

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.