Re: Using SPMF from Python to mine patterns and rules?
Date: February 15, 2019 10:02AM
When calling from python it's easiest in my opinion to do something like this:
import subprocess
# -Xmx1024m specifies to use 1 gb of memory for the java vm.
jar_call = f'java -Xmx1024m -jar spmf.jar run {algorithm} {input}.txt {output_file} ' \
f'{param1} {param2}'
jar_call = jar_call.split()
#This calls the SPMF JAR file
subprocess.call(jar_call)
Then You can read back in the file with something like this
Im using FCHM Bond so for that example the code could look something like this.
import pandas as pd
df = pd.read_csv(output_file, header=None, sep='\n', names=['rules'])
#Convert to a dataframe
df['rules'] = df['rules'].apply(lambda x: x.replace(' #UTIL: ', ' ').replace('
#BOND: ', ' '))
df = df['rules'].str.split(expand=True, n=2)
df.columns = ['Basket', 'Utility', 'Bond']
df['Items'] = seq['Basket'].str.split(',')
I hope this helps! It may not be the fastest or smartest way to read this into python but I use something similar to this.