How can I use an SVM to seperate two classes in a 2D map?
I have 2d-numpy array of points defining a map with obstacles Image of the 2D Map of which I am trying to create an SVM with. The values of this 2d-array are either 0 for open space (grey) or 1,-1 for obstacles (black/white). The formatting of the map is shown below:
[[ 1. 1. 1. ... 1. 1. 1.]
[ 1. 0. 0. ... 0. 0. 1.]
[ 1. 0. 0. ... 0. 0. 1.]
...
[ 1. 0. 0. ... 0. 0. 1.]
[ 1. 0. 0. ... 0. 0. 1.]
[ 1. 1. -1. ... 1. 1. 1.]]
My goal is to use a Support Vector Machine (SVM) to create a line/decision boundary between the obstacles. Since the obstacles are two separate classes, it should be possible to generate an SVM separating them. The ideal SVM boundary would look something like this.
What I am unsure of us how to transform my data to be in the format shown in the sklearn SVM example. I need to be able to specify the features, X
and the target, y
, so I can train the SVM by calling something like svm.SVC(kernel='linear', C=C).fit(X, y)
as shown in the example.
In summary: how to convert my map (a 2-d array of either -1, 1, 0
) into a set of features and targets?
do you know?
how many words do you know
See also questions close to this topic
-
Python File Tagging System does not retrieve nested dictionaries in dictionary
I am building a file tagging system using Python. The idea is simple. Given a directory of files (and files within subdirectories), I want to filter them out using a filter input and tag those files with a word or a phrase.
If I got the following contents in my current directory:
data/ budget.xls world_building_budget.txt a.txt b.exe hello_world.dat world_builder.spec
and I execute the following command in the shell:
py -3 tag_tool.py -filter=world -tag="World-Building Tool"
My output will be:
These files were tagged with "World-Building Tool": data/ world_building_budget.txt hello_world.dat world_builder.spec
My current output isn't exactly like this but basically, I am converting all files and files within subdirectories into a single dictionary like this:
def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree
Right now, my dictionary looks like this:
key:''
.In the following function, I am turning the empty values
''
into empty lists (to hold my tags):def empty_str_to_list(d): for k,v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v)
When I run my entire code, this is my output:
hello_world.dat ['World-Building Tool'] world_builder.spec ['World-Building Tool']
But it does not see
data/world_building_budget.txt
. This is the full dictionary:{'data': {'world_building_budget.txt': []}, 'a.txt': [], 'hello_world.dat': [], 'b.exe': [], 'world_builder.spec': []}
This is my full code:
import os, argparse def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree def empty_str_to_list(d): for k, v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v) parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--filter", action="store", help="keyword to filter files") parser.add_argument("--tag", action="store", help="a tag phrase to attach to a file") parser.add_argument("--get_tagged", action="store", help="retrieve files matching an existing tag") args = parser.parse_args() filter = args.filter tag = args.tag get_tagged = args.get_tagged current_dir = os.getcwd() files_dict = fs_tree_to_dict(current_dir) empty_str_to_list(files_dict) for k, v in files_dict.items(): if filter in k: if v == []: v.append(tag) print(k, v) elif isinstance(v, dict): empty_str_to_list(v) if get_tagged in v: print(k, v)
-
Actaully i am working on a project and in it, it is showing no module name pip_internal plz help me for the same. I am using pycharm(conda interpreter
File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\Scripts\pip.exe\__main__.py", line 4, in <module> File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\__init__.py", line 4, in <module> from pip_internal.utils import _log
I am using pycharm with conda interpreter.
-
Looping the function if the input is not string
I'm new to python (first of all) I have a homework to do a function about checking if an item exists in a dictionary or not.
inventory = {"apple" : 50, "orange" : 50, "pineapple" : 70, "strawberry" : 30} def check_item(): x = input("Enter the fruit's name: ") if not x.isalpha(): print("Error! You need to type the name of the fruit") elif x in inventory: print("Fruit found:", x) print("Inventory available:", inventory[x],"KG") else: print("Fruit not found") check_item()
I want the function to loop again only if the input written is not string. I've tried to type return Under print("Error! You need to type the name of the fruit") but didn't work. Help
-
VBA: Creating a class property that is an array of dictionaries
In Microsoft Excel VBA I need to create a class that has two properties, "Name" and "Holdings". "Name" is just a string so the code for that is easy. But I need "Holdings" to be a variable length array containing dictionary objects. So this is what I wrote:
Private mName As String Private mHoldings() As Scripting.Dictionary Public Property Let Name(vName As String) mName = vName End Property Public Property Get Name() As String Name = mName End Property Public Property Set Holdings(vHoldings() As Scripting.Dictionary) Set mHoldings = vHoldings End Property Public Property Get Holdings() As Scripting.Dictionary Set Holdings = mHoldings End Property
When I try to compile it gives me this error: "Definitions of property procedures for the same property are inconsistent, or property procedure has an optional parameter, a ParamArray, or an invalide Set final parameter.
What I doing wrong?
-
Map for react-native ( No google map and apple map)
Is there a map that can be used in React-Native?
If so, please upload an example of how to use it.
But again! No Google map and Apple map.
-
Keyerror when processing pandas dataframe
For a pathway pi, the CNA data of associated genes were extracted from the CNV matrix (C), producing an intermediate matrix B∈Rn×ri, where ri is the number of genes involved in the pathway pi. That is, the matrix B consists of samples in rows and genes for a given pathway in columns. Using principal component analysis (PCA), the matrix B was decomposed into uncorrelated components, yielding Gpi∈Rn×q, where q is the number of principal components (PCs).
import pandas as pd import numpy as np from sklearn.decomposition import PCA from sklearn.preprocessing import LabelEncoder import csv def get_kegg_pathways(): kegg_pathways = [] with open(directory + "hsa.txt", newline="") as keggfile: kegg = pd.read_csv(keggfile, sep="\t") for row in kegg: #for row in kegg.itertuples(): kegg_pathways.append(row) return kegg_pathways def main(): # Pathway info kegg = get_kegg_pathways() # q : Number of Principal Components (PCs) # C : CNV matrix # G = mRNA expression matrix # M : DNA methylation matrix q = 5 C = [] G = [] M = [] # Process common data (denoted as matrix B) cna_sample_index = {} process_common = True if process_common: for i, p in enumerate(kegg): genes = {} first = True for s in p: if first: first = False else: if s!= "NA": genes[s] = 1 # Loop through each sample B = [] pathways = [] for s in ld: B.append([]) pathways.append(cna_sample_index[p]) Bi = 0 for index, row in cna.df.itertuples(): if row[0].upper() in genes: Bi2 = Bi for c in pathways: B[Bi2].append(cna.df.iloc[index, c]) Bi2 = Bi2 + 1 pca_cna = cna.fit() pca_cna.fit(B)
Traceback:
File "/home/melissachua/main.py", line 208, in <module> main() File "/home/melissachua/main.py", line 165, in main pathways.append(cna_sample_index[p]) KeyError: 'hsa00010_Glycolysis_/_Gluconeogenesis'
kegg
table:0 1 0 hsa00010_Glycolysis_/_Gluconeogenesis NaN 1 hsa00020_Citrate_cycle_(TCA_cycle) NaN 2 hsa00030_Pentose_phosphate_pathway NaN cna
table:Hugo_Symbol TCGA-02-0001-01 TCGA-02-0001-02 TCGA-02-0001-03 0 0.001 0.002 0.003 0.004 1 0.005 0.006 0.007 0.008 -
Is there a way to use mutual information as part of a pipeline in scikit learn?
I'm creating a model with scikit-learn. The pipeline that seems to be working best is:
- mutual_info_classif with a threshold
- PCA
- LogisticRegression
I'd like to do them all using sklearn's pipeline object, but I'm not sure how to get the mutual info classification in. For the second and third steps I do:
pca = PCA(random_state=100) lr = LogisticRegression(random_state=200) pipe = Pipeline( [ ('dim_red', pca), ('pred', lr) ] )
But I don't see a way to include the first step. I know I can create my own class to do this, and I will if I have to, but is there a way to do this within sklearn?
-
Slicing an image into color based layers with Sklearn
I have an image. I applied KMeans color clustering on this image so I need to present only purple clusters on image and orange clusters on different image. How do I need to do that?
-
Bagging with svm and neural network in r with caret
I am fairly new to the bagging technique and Caret's bagControl() as well as bag() and am currently trying to build an ensemble model using bagging with SVM and neural network (nnet) with the Caret package. I've looked at the documentation of bagControl() and bag() but am still confused as to what I should put in each parameters.
So far, what I've done is I created my training and testing sets as follows
set.seed(345) # shuffle the order of the samples rows <- sample(nrow(gene.expr.fcbf.z)) # use random vector to reorder the FCBF dataset gene.expr.fcbf.z <- gene.expr.fcbf.z[rows, ] # random sample 30% of data set into test and the rest 70% into train set gene.splits.2 <- createDataPartition(gene.expr.fcbf.z$Class, p= 0.70, list = F) # split --should have 208 observations in train and 88 in test genes.fcbf.training <- gene.expr.fcbf.z[gene.splits.2, ] genes.fcbf.testing <- gene.expr.fcbf.z[-gene.splits.2, ]
Next, I wish to train my bagged SVM model. Here's what I have
svm.bag <- train(Class ~., data = genes.fcbf.training, method = "bag", B = 100, bagControl = bagControl(fit = svmBag$fit, predict = svmBag$pred, aggregate = svmBag$aggregate))
However, it is throwing the following error for all Resamples starting from Resample01
Warning: model fit failed for Resample01: vars=25 Error in fitter(btSamples[[iter]], x = x, y = y, ctrl = bagControl, v = vars, : task 1 failed - "could not find function "lev""
Can anyone provide some help? Ultimately, I wish to train my model with bagging and generate predictions to create a confusion matrix for model evaluation with Caret package. If anyone can provide any guidance that would be awesome!
-
Can I generate the hinge loss of a multiclass svm test result with sklearn?
The dummy version of the code is:
feature = [] label = [] xtrain,ytrain,xtest,ytest = train_test_split(feature,label, test_size = 0.3) clf=svm.SVC(C=10,kernel='rbf')
I saw that there is a function hinge_loss() from the sklearn library, but it seems that it is only for linear svm? Can I calculate the hinge loss of the classifier in the sample code above with this hinge_loss() function? Or do I have to manually state out the hinge loss function for the multiclass svm with a rbf kernel like
loss_function=max(0,1 y_test*...) # the line above was just a dummy version, as I havent figured out the loss function for the classifier just yet.
I am assuming this is a programming related question so I ask in this forum (was reported by someone before saying my previous questions were not programming related).
-
Is there a way to create a nonlinear SVM who's decision bounday passes through two predefined points?
I am using Python to create nonlinear decision boundaries (DB) using nonlinear Support Vector Machines (SVMs).
My goal is to be able to specify a pair of coordinates and have the DB pass through these points. For example, in the below image, the two points where the DB is desired to pass through is shown by the orange and blue points. The ideal DB would look like the red line.
Currently, if I want the DB to pass through two points, I surround them in guide features, where the top-right are 1's and the bottom-left are zeros as is shown below:
This leads to a DB as is shown below.
My goal is to be able to force the DB to pass through two specific points without having to add the guide points. I am wondering if there is a way I can go about this?
-
how to run a config file for boundary by hashicorp
Tried this command boundary dev -config file name
encountered the following error: flag provided but not defined: -config
-
Unity Drag and Drop Canvas Boundary
What I want is simple, I already have the drag and drop mechanic done. All I want to know is how I can set boundaries so that I can't drag and drop any UI images out of the canvas.