Python Forum
Problem implementing .iloc slicing in a custom function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem implementing .iloc slicing in a custom function
#1
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!
Reply
#2
Passing 0:12 as an argument value to a function definitely isn't valid Python syntax. Try categoricalIndex=slice(0, 12) instead.
Reply
#3
(Mar-03-2020, 01:11 PM)scidam Wrote: Passing 0:12 as an argument value to a function definitely isn't valid Python syntax. Try categoricalIndex=slice(0, 12) instead.

this works PERFECTLY!! thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Implementing a Lowpass Filter Madridista 5 2,170 Nov-08-2022, 06:41 PM
Last Post: Madridista
  I get "N/A" labels when implementing object detection hobbyist 0 964 Aug-17-2022, 12:35 PM
Last Post: hobbyist
  iloc d8a988 7 2,979 Apr-27-2020, 10:15 AM
Last Post: d8a988
  pandas dataframe iloc mystery edvvardbrian 2 2,165 Oct-29-2019, 02:55 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020