Orange: Python Script

From OnnoWiki
Revision as of 07:35, 30 January 2020 by Onnowpurbo (talk | contribs)
Jump to navigation Jump to search

Sumber: https://docs.biolab.si//3/visual-programming/widgets/data/pythonscript.html

Memperluas fungsi melalui script Python.

Input

Data (Orange.data.Table): input dataset bound to in_data variable
Learner (Orange.classification.Learner): input learner bound to in_learner variable
Classifier (Orange.classification.Learner): input classifier bound to in_classifier variable
Object: input Python object bound to in_object variable

Output

Data (Orange.data.Table): dataset retrieved from out_data variable
Learner (Orange.classification.Learner): learner retrieved from out_learner variable
Classifier (Orange.classification.Learner): classifier retrieved from out_classifier variable
Object: Python object retrieved from out_object variable

Widget Python Script dapat digunakan untuk menjalankan skrip python di input, ketika fungsionalitas yang sesuai tidak diterapkan dalam widget yang ada. Script memiliki variabel in_data, in_distance, in_learner, in_classifier dan in_object (dari sinyal input) di namespace lokalnya. Jika sinyal tidak terhubung atau belum menerima data apa pun, variabel tersebut akan berisi None.

Setelah script dijalankan, variabel dari namespace lokal script diekstraksi dan digunakan sebagai output dari widget. Widget dapat lebih lanjut dihubungkan ke widget lain untuk memvisualisasikan output.

Misalnya script berikut hanya akan meneruskan semua sinyal yang diterimanya:

out_data = in_data
out_distance = in_distance
out_learner = in_learner
out_classifier = in_classifier
out_object = in_object

Catatan: Anda tidak boleh memodifikasi objek input di tempat.

PythonScript-stamped.png
  • Info box contains names of basic operators for Orange Python script.
  • The Library control can be used to manage multiple scripts. Pressing “+” will add a new entry and open it in the Python script editor. When the script is modified, its entry in the Library will change to indicate it has unsaved changes. Pressing Update will save the script (keyboard shortcut “Ctrl+S”). A script can be removed by selecting it and pressing the “-“ button.
  • Pressing Execute in the Run box executes the script (keyboard shortcut “Ctrl+R”). Any script output (from print) is captured and displayed in the Console below the script.
  • The Python script editor on the left can be used to edit a script (it supports some rudimentary syntax highlighting).
  • Console displays the output of the script.

Contoh

Python Script widget is intended to extend functionalities for advanced users. Classes from Orange library are described in the documentation. To find further information about orange Table class see Table, Domain, and Variable documentation.

One can, for example, do batch filtering by attributes. We used zoo.tab for the example and we filtered out all the attributes that have more than 5 discrete values. This in our case removed only ‘leg’ attribute, but imagine an example where one would have many such attributes.

from Orange.data import Domain, Table
domain = Domain([attr for attr in in_data.domain.attributes
                 if attr.is_continuous or len(attr.values) <= 5],
                in_data.domain.class_vars)
out_data = Table(domain, in_data)
PythonScript-filtering.png

The second example shows how to round all the values in a few lines of code. This time we used wine.tab and rounded all the values to whole numbers.

import numpy as np
out_data = in_data.copy()
#copy, otherwise input data will be overwritten
np.round(out_data.X, 0, out_data.X)
PythonScript-round.png


The third example introduces some Gaussian noise to the data. Again we make a copy of the input data, then walk through all the values with a double for loop and add random noise.

import random
from Orange.data import Domain, Table
new_data = in_data.copy()
for inst in new_data:
  for f in inst.domain.attributes:
    inst[f] += random.gauss(0, 0.02)
out_data = new_data
PythonScript-gauss.png


The final example uses Orange3-Text add-on. Python Script is very useful for custom preprocessing in text mining, extracting new features from strings, or utilizing advanced nltk or gensim functions. Below, we simply tokenized our input data from deerwester.tab by splitting them by whitespace.

print('Running Preprocessing ...')
tokens = [doc.split(' ') for doc in in_data.documents]
print('Tokens:', tokens)
out_object = in_data
out_object.store_tokens(tokens)

You can add a lot of other preprocessing steps to further adjust the output. The output of Python Script can be used with any widget that accepts the type of output your script produces. In this case, connection is green, which signalizes the right type of input for Word Cloud widget.

PythonScript-Example3.png


Referensi

Pranala Menarik