The following documentation provides samples and instructions on how to get a Python script working with SPSS.
Scripts can be ran outside of the SPSS GUI through the command prompt by using Modeler Batch/Clemb. Standalone scripts can also be ran through the SPSS GUI using standalone script. Tools > Standalone Script
Modeler Batch or Clemb is included with Modeler Client. It is not a separate install. If you have Modeler client you can use the clemb.exe that is in the bin folder and run against the local server. You can find the executable here:
<path to spss program directory>\Modeler\18.0\bin\clemb.exe
Python scripts can be be executed through clemb. Clemb/Modeler Batch allows users to run SPSS Modeler from a command line, without the need of a graphical user interface.
Clemb has a variety of options and configurations which can be referenced in “IBM SPSS Modeler 18 Batch Users’s Guide”.
Here is a sample clemb command (with paramaters) that will execute the attached sample python script.
C:\"Program Files"\IBM\SPSS\Modeler\18.0\bin\clemb
-script C:\Users\Administrator\Desktop\DEVELOP\spss_scripting\ModelerScript.py
-Pcity=Austin
-execute
The following is sample script that runs a specific SPSS file (stream) and specific nodes for testing purposes. The script can be ran through SPSS GUI as a standalone script or using the command line using clemb.
The script takes in a session argument through clemb. This argument is optional.
-
http://www.sv-europe.com/blog/writing-standalone-python-script-modeler/
-
ftp://public.dhe.ibm.com/software/analytics/spss/documentation/modeler/16.0/en/modeler_jython_scripting_automation_book.pdf
-
ftp://public.dhe.ibm.com/software/analytics/spss/documentation/modeler/18.0/en/ModelerBatch.pdf
import pandas as pd
假設的賠率數據CSV檔案路徑
odds_csv_path = 'odds_data.csv'
假設的籃球比賽數據CSV檔案路徑
basketball_csv_path = 'basketball_data.csv'
讀取賠率數據
odds_data = pd.read_csv(odds_csv_path)
讀取籃球比賽數據
basketball_data = pd.read_csv(basketball_csv_path)
範例:找出特定比賽的最佳賠率
def find_best_odds(game_id):
game_odds = odds_data[odds_data['game_id'] == game_id]
best_odds = game_odds.loc[game_odds['odds'].idxmax()]
return best_odds
範例:基於過去表現預測比賽結果
def predict_game_outcome(team_id):
team_games = basketball_data[(basketball_data['home_team_id'] == team_id) |
(basketball_data['away_team_id'] == team_id)]
# 假設的預測邏輯
average_points = team_games['points'].mean()
return 'win' if average_points > 100 else 'lose'
使用範例
game_id = 12345
team_id = 678
print(find_best_odds(game_id))
print(predict_game_outcome(team_id))