Problem implementing .iloc slicing in a custom function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Problem implementing .iloc slicing in a custom function (/thread-24724.html) |
Problem implementing .iloc slicing in a custom function - amjass12 - Mar-01-2020 Hello all, I am stuck with the implementation of slicing dataframes within a custom function. I have looked on the internet and cannot find anything that is helping me :( any help would be much appreciated! I am simply wrapping generating a umap and plotting it in seaborn into a wrapper function to avoid so much repeated code writing! the function is as follows: def activationUMAP(activations,dataIndex,ulMetric, ulVerbose, encodedMetadata, categoricalIndex,categoricalString, snsHue,snsPallete, snsLegend,ulNneighbour=40): #subset layerouput to specific neuron activations (organ age etc) layer_outputpd=pd.DataFrame(activations, index=dataIndex.index) layer_outputCategory=[b]layer_outputpd.iloc[:,categoricalIndex][/b] layer_outputCategory.shape ##umap algorithm:: umap-learn layout = umap.UMAP(verbose=ulVerbose,metric=ulMetric,n_neighbors=ulNneighbour).fit_transform(layer_outputCategory) layout = normalize_layout(layout) subsetxtrain=encodedMetadata[encodedMetadata.index.isin(dataIndex.index)] subsetxtrain=subsetxtrain.reindex(layer_outputCategory.index) ##make umap layout above pandas df layout1=pd.DataFrame(layout) layout1[categoricalString] = subsetxtrain[categoricalString].values ##plot with seaborn umapPlot = sns.scatterplot(x=layout1.iloc[:,0], y=layout1.iloc[:,1], data=layout1,hue=snsHue, palette=snsPallete, legend=snsLegend) umapPlot.legend() return(umapPlot)I have highlighted in bold where the problem lies. when carrying out normal slicing, i would simply add :,0:12 or :,12:19 etc... to selectively chose different columns that belong to different classes... I have passed however the "categoricalIndex" argument so that in the function arguments, 0:12, 12:19 can simply be inserted, however this consistently provides an error as follows: activationUMAP(activations=layer_output,dataIndex=X_train, encodedMetadata=metadataencode,categoricalString="Organ", [b]categoricalIndex=0:12[/b], ulMetric="euclidean", ulVerbose=True,snsHue="Organ",snsPallete="rainbow",snsLegend="brief")this produces the following error: File "", line 3 encodedMetadata=metadataencode,categoricalString="Organ", categoricalIndex=0:12, ^ SyntaxError: invalid syntax I have tried different variations including removing the square brackets from the function etc.. and this still produces the same error. Note: if i simply pass :,0:12 within the actual function and remove the categoricalIndex argument, all works fine as it should! I really need this slicing argument however! any help would be much appreciated! thank you! RE: Problem implementing .iloc slicing in a custom function - scidam - Mar-03-2020 Passing 0:12 as an argument value to a function definitely isn't valid Python syntax. Try categoricalIndex=slice(0, 12) instead.
RE: Problem implementing .iloc slicing in a custom function - amjass12 - Mar-03-2020 (Mar-03-2020, 01:11 PM)scidam Wrote: Passing this works PERFECTLY!! thank you so much! |