1

I am using python 3 with jupyter notebook. I implemented simple widgets that take method name and parameter values as inputs from users. Parameters of the method depends on the method itself

For example, in a run event both of the functions below can be called according to methods and parameter values user chose

KMeans(n_clusters=2, random_state=0, n_init=10).fit(X)

AgglomerativeClustering(n_clusters=12, affinity='euclidean').fit(X)

I can form the parameters as strings but I could not pass it to functions. is there a way to execute those functions in string form?

rdas
  • 20,604
  • 6
  • 33
  • 46
Erdem
  • 43
  • 4
  • 2
    Does this answer your question? [How to call Python functions dynamically](https://stackoverflow.com/questions/4246000/how-to-call-python-functions-dynamically) – ChatterOne Feb 17 '20 at 10:52
  • Please check https://stackoverflow.com/questions/11291242/python-dynamically-create-function-at-runtime – Pitto Feb 17 '20 at 10:52
  • Does this answer your question? [Python: dynamically create function at runtime](https://stackoverflow.com/questions/11291242/python-dynamically-create-function-at-runtime) – Pitto Feb 17 '20 at 10:53
  • ``exec("Your_Code_as_String")`` – AlexNe Feb 17 '20 at 10:53
  • seems like it doesn't work aa = AgglomerativeClustering(n_clusters=12).fit(df_raw) bb = exec("AgglomerativeClustering(n_clusters=12).fit(df_raw)") display(type(aa)) display(type(bb)) sklearn.cluster._agglomerative.AgglomerativeClustering NoneType – Erdem Feb 17 '20 at 11:10

0 Answers0