Skip to content

Instantly share code, notes, and snippets.

@raarellano
Last active April 9, 2024 19:44
Show Gist options
  • Save raarellano/69e37c2fed371a8b331077743aa0e85b to your computer and use it in GitHub Desktop.
Save raarellano/69e37c2fed371a8b331077743aa0e85b to your computer and use it in GitHub Desktop.
Python-SPSS Scripting

Python-SPSS Scripting

The following documentation provides samples and instructions on how to get a Python script working with SPSS.

Standalone Scripts

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

Running a script using Clemb

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

Sample Python Script

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.

Resources

def initSession():
return modeler.script.session()
def initStream(session, streamFile):
taskrunner = session.getTaskRunner()
return openStream(taskrunner, streamFile)
def openStream(taskrunner, streamFile):
try:
stream = taskrunner.openStreamFromFile(streamFile, True)
except:
print "Could not open", streamFile
modeler.script.exit(1)
return stream
def getCity(session):
city = session.getParameterValue('city')
if city == None:
city = "Dallas"
print city
return city
def runAnalysis(city):
results = []
if city == "Dallas":
stream.findByID("id552TFW5PVMW").run(results)
elif city == "Austin":
stream.findByID("id4FYTFKPRGES").run(results)
printResults(results)
# return results
def runExportResults(city):
results = []
if city == "Dallas":
stream.findByID("id2JESUGKSLIB").run(results)
# Exports file Dallas Cl1NN
elif city == "Austin":
stream.findByID("id56YT3B2IWXX").run(results)
# Exports file Austin Cl1NN
print "Excel Exported"
def printResults(results):
print "Results"
print results[0]
# Define Stream File
streamFile = "C:\Users\Administrator\Desktop\DEVELOP\spss_scripting\example_stream.str"
# Start SPSS Session
session = initSession()
stream = initStream(session, streamFile)
# Get Params
city = getCity(session)
# Run Nodes
# runAnalysis(city)
runExportResults(city)
@PatLin777
Copy link

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment