Package: rapidminer
RapidMiner Python packageThis Python package allows you to interact with RapidMiner Studio and AI Hub. You can collaborate using the RapidMiner repository and leverage the scalable RapidMiner AI Hub infrastructure to run processes. This document shows examples on how to use the package. Additional notebook files provide more advanced examples. There is an API document for each classes:Project,Studio,Server,Connections,Scoring. You can find thechangelog for the package here.Table of contentsRapidMiner Python packageTable of contentsRequirementsKnown current limitationsOverviewInstallationProject-Reading ExampleSet-Writing ExampleSet-Running a processConnectionsStudio-Reading ExampleSet-Writing ExampleSet-Running a processServerUsage of Server APIRunning a processGetting information about projects, queues and connectionsProjectsConnectionsQueuesScoringRequirementsRapidMiner Studio10.0.0for Studio classRapidMiner AI Hub10.0.0for Server classPython Scripting extension10.0.0or later installed for both Studio, download it from theMarketplaceKnown current limitationsPython version:Extensive tests were only carried out usingPython 3.7, but earlier versions are expected to work as well.Python 2 is not supported.RapidMiner Studio and AI Hub processes guarantee reproducibility. That means you should always get the same result after a version update. The same featurecannot be guaranteedwhen using this Python library (the library depends on other libraries that are not in our control).RapidMiner AI Hub withSAML authenticationis not supported.OverviewStudio class provides a read and a write method for reading / writing data and other objects, and both Studio and Server classes provide a run method to run processes. To work with versioned projects, use the Project class that provides read and write methods to the data file format used in them.Studio class requires a local Studio installation and is suitable in the following cases:Implementing certain data science steps in Python using your favorite IDE or notebook implementation. You may even use the resulting code afterwards in a RapidMiner process within anExecute Pythonoperator.You are using coding primarily, but you want to incorporate methods that are impemented in a RapidMiner process.Creating batch tasks that also interact with the repository and / or run processes.Server class connects directly to a RapidMiner AI Hub instance without the need of a Studio installation. It is suitable in the following cases:Collaborating with RapidMiner users, sharing data easily.Calling, running, scheduling processes on the RapidMiner AI Hub platform from a local script.Project class is required to work with the git-based versioned repositories called projects. Projects can be shared using RapidMiner AI Hub. The shared data format allows Python coders and RapidMiner users to easily work on the same data. To summarize, this class is suitable in the following cases:Using versioned projects to collaborate with RapidMiner users and share data easily.Connections class can be used to access connections defined in a project. This way, Python coders can use the same external connections that are used by RapidMiner users. The connection fields are accessible, you only need an appropriate Python package to use those values.Using and sharing connections easily and securely without entering or storing any information redundantly.InstallationThe library can be installed easily:install in one step:$ pip install rapidmineror clone the repository and install:$ git clone https://github.com/rapidminer/python-rapidminer.git
  $ cd python-rapidminer
  $ python setup.py installProjectProjects are a new feature of RapidMiner AI Hub 9.7.0 that allows you to have versioned repositories as the storage layer shared between RapidMiner users and Python coders. You can use any kind of git client, e.g. git commands, to clone, checkout a repository from RapidMiner AI Hub, and push your modifications there. Use the Project class to read and write the common data file format (HDF5).Let's say you have cloned your versioned project into the localmyprojectfolder using the git clone command. After that, point the Project class to this folder:importrapidminerproject=rapidminer.Project("myproject")Reading ExampleSetOnce you have a project instance, you can read a RapidMiner ExampleSet in Python by running the following line (let's assume your data set calledmydatais inside thedatafolder):df=project.read("data/mydata")The resultingdfis apandasDataFrameobject, which you can use in the conventional way.You can also directly read a file on an arbitrary local path by using a default Project class:df=rapidminer.Project().read("local/file/path.rmhdf5table")Writing ExampleSetYou can save anypandasDataFrameobject to a project with the following command:project.write(df,"data/mydata_modified")After writing the data set to the disk, you can use git commit and push to publish your changes to the remote project.For more examples with projects, see theProject examples notebook.Running a processUse Studio or Server classes to run a process from a project, see examples below.ConnectionsConnections in RapidMiner allow you to access external systems like databases, cloud services, social media, etc. With the Connections class, you can reuse connections defined in RapidMiner in an easy and secure way. Access all connections in a project, by pointing to a local project folder:importrapidminerconnections=rapidminer.Connections("myproject",server=rapidminer.Server("https://myserver.mycompany.com:8080",username="myrmuser"))Here, we already pointed to aServerinstance. That is only necessary if you have encrypted connection fields or use the AI Hub Vault to store certain sensitive values.It is also possible to use connections from an AI Hub repository. Assuming here that we have aServerinstance, you can retrieve the connections defined in its repository:connections=server.get_connections()You can read the values of the connection fields by either using the connection name or an index. Use these field values to establish a connection using an appropriate Python package. The following code shows several different ways to access these values. Encryption or value injection (e.g. from AI Hub Vault) is handled transparently:myconn=connections["my_db_connection"]mydb=myconn.values["database"]myuser=myconn.usermypass=connections[0].passwordmyhost=myconn.find_first("host")myport=connections[0].values["port"]Note when reading connections directly from an AI Hub project, encrypted values are not available (values are None). You are advised to use AI Hub Vault for these values, or clone the project with the connection in it.StudioYou need to have a locally installed RapidMiner Studio instance to use this class. The only thing you need to provide is your installation path. Once that is specified, you can read from and write data or other objects to any configured repository. You can also run processes from files or from the repository. In this section, we show you some examples on how to read and write repository data and run processes. For more advanced scenarios see the includedIPython notebookand thedocumentation of theStudioclass.Note that eachStudiomethod starts a Studio instance in the background and stops it when it is done. It is not recommended to run multiple instances in parallel, e.g. on different Notebook tabs. If you have several RapidMiner extensions installed, all of them will be loaded each time, that may lead to longer runtime. Provide multiple parameters to a read or write call, if possible, to avoid the startup overhead.First you need aConnectorobject to interact with Studio. Once you have that, you can read and write data or run a process with a single line. To create aStudioConnectorobject, run the following code:connector=rapidminer.Studio("/path/to/you/studio/installation")where you replace"/path/to/you/studio/installation"with the location of your Studio installation. In case of Windows, a typical path is"C:/Program Files/RapidMiner/RapidMiner Studio"- note that you should either use forward "/" as separators or put anrbefore the first quote character to indicate raw string
. In case of Mac, the path is usually"/Applications/RapidMiner Studio.app/Contents/Resources/RapidMiner-Studio". Alternatively you can define this location via theRAPIDMINER_HOMEenvironment variable.Reading ExampleSetOnce you have a connector instance, you can read a RapidMiner ExampleSet in Python by running the following line:df=connector.read_resource("//Samples/data/Iris")The resultingdfis apandasDataFrameobject, which you can use in the conventional way.Writing ExampleSetYou can save anypandasDataFrameobject to a RapidMiner repository (or file) with the following command:connector.write_resource(df,"//Local Repository/data/mydata")wheredfis theDataFrameobject you want to write to the repository, and"//Local Repository/data/mydata"is the location where you want to store it.Running a processTo run a process execute the following line:df=connector.run_process("//Samples/processes/02_Preprocessing/01_Normalization")You will get the results aspandasDataFrames. You can also define inputs, and many more. For more examples, see theStudio examples notebook.ServerWithServerclass, you can directly connect to a local or remote RapidMiner AI Hub instance without the need for any local RapidMiner (Studio) installation. You can execute processes using the scalable Job Agent architecture. In this section, we show you some examples on how to run processes. For more advanced scenarios see the includedIPython notebookand thedocumentation of theServerclass.Usage of Server APITo create aServerConnectorobject, run the following code:connector=rapidminer.Server("https://myserver.mycompany.com:8080")It will ask you for further input to be able to authenticate. It is also possible to configure it using the constructor arguments:connector=rapidminer.Server("https://myserver.mycompany.com:8080",username="myrmuser",password="myrmpassword",authentication_server="http:///myserver.mycompany.com:8081",realm="MyCompanyRealm",client_id="mycompany-rapidminer-server")where you replace"https://myserver.mycompany.com:8080"with the url of your RapidMiner AI Hub instance,"myrmuser"with your username,"myrmpassword"with your password,"http:///myserver.mycompany.com:8081"with the url of your KeyCloak server,"MyCompanyRealm"with your company Realm in the KeyCloak server,"mycompany-rapidminer-server"with the client id which has the rights to authenticate in.Running a processYou may want to run a process that resides in a versioned project. Note that inputs and outputs are not allowed, as the process can only directly read from the project and potentially write back using an automatic commit and push. To run the latest version of a process in project, use the following code:process=ProjectLocation('test-project','test-process.rmp')connector=rapidminer.Server("https://myserver.mycompany.com:8080",username="myrmuser",password="myrmpassword",authentication_server="http:///myserver.mycompany.com:8081",realm="MyCompanyRealm",client_id="mycompany-rapidminer-server")connector.run_process(path=process)You can add theprojectname andpathto the process to the run_process method too. You can also definemacrosand thequeue, like the following way:connector.run_process(project='test-project',path='test-process.rmp',queue="default",macros={"sample_size":100})Getting information about projects, queues and connectionsProjectsYou can also get the available projects in the Server the following way:connector.get_projects()This method returns a JSON array of objects representing each repository with its properties.ConnectionsYou can also get the available connections in a given project:connector.get_connections('project')It returns a Connections object listing connections from the AI Hub project.QueuesYou can also get the queues in a Server:connector.get_queues()It returns a JSON array of objects representing each queue with its properties.ScoringThis class allows you to easily use a deployedReal-Time Scoringservice. You only need to provide the RapidMiner AI Hub url and the particular scoring service endpoint to create a class instance. After that, you can use the predict method to do scoring on a pandas DataFrame and get the result in a pandas DataFrame as well. For instructions on how to deploy Real-Time Scoring on RapidMiner AI Hub, please refer to its documentation.sc=rapidminer.Scoring("http://myserver.mycompany.com:8090","score-sales/score1")prediction=sc.predict(df)where the scoring endpoint is at"score-sales/score1"that can be applied to the datasetdf, and the resultingpredictionis apandasDataFrameobject. You can find theScoringclassdocumentation here. Note that the scoring endpoint should not have a leading"/".It can work without any authentication. However, there are two different options for authentication. It depends on the RTS server configuration, but the two different method is the basic authentication and the other one is the OAuth2 authentication with Keycloak.If basic authentication is configured, it is needed to add three extra arguments to define theauthenticationmethod,usernameandpassword.
The value of the authentication parameter in this case is"basic".sc=rapidminer.Scoring("http://myserver.mycompany.com:8090","score-sales/score1",authentication='basic',username="your_user",password="your_password")prediction=sc.predict(df)If the oauth authentication is configured, it is needed to add three more extra arguments compared to the basic authentication to define theauthentication server, therealmand theclient-id.
The value of the authentication parameter in this case is"oauth".sc=rapidminer.Scoring("http://myserver.mycompany.com:8090","score-sales/score1",authentication='basic',username="your_user",password="your_password",authentication_server='http://auth-server.mycompany.com:8081',realm='MyCompanyRealm',client_id='real-time-scoring-client')prediction=sc.predict(df)