I often use supernodes to organize (make it easier to see) long streams in SPSS Modeler, but I don't know how to handle nodes in supernodes when changing node settings in Python scripts. Did. (If you expand the supernode, it will be solved, but at this time there are many supernodes, and I wanted to solve it with a script if possible.) I will leave a way to realize it as a memorandum.
In conclusion, the method is well documented and found quickly.
IBM SPSS Modeler 18.2 Python Scripting and Automation Guide (http://public.dhe.ibm.com/software/analytics/spss/documentation/modeler/18.2/en/ModelerScriptingAutomation.pdf)
P.383 Excerpt ** Encapsulation node property settings ** You can set the properties of a particular node encapsulated within a supernode by accessing the child diagram within the supernode. For example, suppose you have an input supernode with a variable length file encapsulated to read data. You can pass the name of the read file (specified using the full_filename property) by accessing the child diagram and searching for related nodes as follows:
childDiagram = source_supernode.getChildDiagram() varfilenode = childDiagram.findByType("variablefile", None) varfilenode.setPropertyValue("full_filename", "c:/mydata.txt")
## Practical edition
If you can understand it easily by looking at the above manual, you don't have to read it anymore.
### Example stream
(1) There is such a stream that RFM aggregates from purchase history data
<img width="300" alt="WS0057.JPG" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/361272/85ea0092-1ffd-9966-6a51-efdfaab41b8e.jpeg ">
② Suppose that the contents of the super node are like this.
<img width="900" alt="WS0058.JPG" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/361272/0cf2ef31-2c6b-8da5-9668-c8083723020c.jpeg ">
The contents of the field creation node that is "RECENCY" in the super node is now like this.
<img width="600" alt="WS0061.JPG" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/361272/b78affdd-74af-886d-6d15-906c24352bf3.jpeg ">
What we are doing is calculating how many days ago the latest purchase date is based on 2020/10/1.
(SDATE is the purchase date. Think of SDATE_Max as the latest purchase date for each customer in the pre-aggregate node.)
Here, suppose that you want to change the base date of 2020/10/1 according to the execution timing of the stream, for example, you want to aggregate based on the previous Sunday no matter when you execute it.
In that case, you can change the datetime_date (2020, 10, 1) part in the Python script to the appropriate date you want to use as the reference, but since the nodes in the supernode cannot be handled directly by findByXXXX (), the previous manual It will take some effort as seen in.
Below is an example of a Python script.
import datetime stream = modeler.script.stream()
#Base date setting RefDate = datetime.date(2020, 10, 10)
#Identify supernodes in the stream rfm_process_super_node = stream.findByType("process_super", u"RFM aggregation") #Instantiate a diagram (stream?) Expanded inside a supernode rfm_child_diagram = rfm_process_super_node.getChildDiagram() #Identify the field creation node in the supernode rfm_derive_node = rfm_child_diagram.findByType("derive", u"RECENCY") #Change base date for specified field creation node rfm_derive_node.setPropertyValue("formula_expr", u"date_days_difference(SDATE_Max,datetime_date(" + str(RefDate.year) + "," + str(RefDate.month) + "," + str(RefDate.day) + ")")
It will be a little easier to understand if you understand that "stream" is the stream of ① and "rfm_child_diagram" is the stream in the supernode of ②.
If you execute this script in the stream (Tools ⇒ Stream Properties ⇒ Execute), the base date will change to 2020/10/10. This time, I have specified the base date for simplicity, but please think about the logic of what to do with the base date according to your requirements.
Below is the setting of the node that was changed by running the script. It went well.
(I never rewrote it by hand!)
<img width="600" alt="WS0062.JPG" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/361272/1aef661a-afdc-cf5d-f68c-8b9f7b988528.jpeg ">
* Please refrain from rushing into such a short stream as "Isn't it necessary to make it a super node?"
Recommended Posts