You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

函数传递触发TypeError:'function'对象不可下标访问的解决方法

Fix: TypeError: 'function' object is not subscriptable

Let's break down what's going wrong here and how to fix it quickly:

Why This Error Happens

You're passing the function object analyze_playlist directly to classify_playlist, then trying to index it like func[['danceability', ...]]—but functions don't support subscript access! You need to first call the function to get the DataFrame it returns, then work with that DataFrame.

Solutions

There are a couple of straightforward ways to fix this, depending on how you want to handle parameters for analyze_playlist:

Option 1: Call the Function Inside classify_playlist

If analyze_playlist doesn't need extra parameters (or you can handle them inside classify_playlist), just call the function to get your DataFrame first:

def classify_playlist(func):
    spotify = pd.read_csv('../OUTPUT/spotify.csv')
    spotify.drop(columns=['Unnamed: 0'], inplace=True)
    y = spotify['intervals'].values
    X = spotify[['danceability', 'energy', 'loudness', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence']].values
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    
    forest = RandomForestClassifier(n_estimators=200)
    forest.fit(X_train, y_train)
    
    # Critical fix: Call the function to get the DataFrame first!
    playlist_df = func()
    tx = playlist_df[['danceability', 'energy', 'loudness', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence']].values
    
    testdata = forest.predict(tx)
    return testdata

Option 2: Pass Arguments to the Injected Function

Since your analyze_playlist uses *args to accept playlist parameters, modify classify_playlist to forward those arguments:

def classify_playlist(func, *func_args):
    spotify = pd.read_csv('../OUTPUT/spotify.csv')
    spotify.drop(columns=['Unnamed: 0'], inplace=True)
    y = spotify['intervals'].values
    X = spotify[['danceability', 'energy', 'loudness', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence']].values
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    
    forest = RandomForestClassifier(n_estimators=200)
    forest.fit(X_train, y_train)
    
    # Call the function with the provided arguments
    playlist_df = func(*func_args)
    tx = playlist_df[['danceability', 'energy', 'loudness', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence']].values
    
    testdata = forest.predict(tx)
    return testdata

Call it like this (replace with your actual playlist ID/arguments):

b = classify_playlist(analyze_playlist, "your_playlist_id_here")

Option 3: Pre-Call analyze_playlist Before Passing It

If you prefer not to modify classify_playlist, just generate the DataFrame first, then pass that instead of the function:

# First get your playlist DataFrame
playlist_data = analyze_playlist("your_playlist_id_here")
# Pass the DataFrame to classify_playlist
b = classify_playlist(playlist_data)

Update classify_playlist to expect a DataFrame instead of a function:

def classify_playlist(playlist_df):
    spotify = pd.read_csv('../OUTPUT/spotify.csv')
    spotify.drop(columns=['Unnamed: 0'], inplace=True)
    y = spotify['intervals'].values
    X = spotify[['danceability', 'energy', 'loudness', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence']].values
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    
    forest = RandomForestClassifier(n_estimators=200)
    forest.fit(X_train, y_train)
    
    tx = playlist_df[['danceability', 'energy', 'loudness', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence']].values
    
    testdata = forest.predict(tx)
    return testdata

Quick Recap

The core issue was treating the function itself as its return value. Just call the function to get the DataFrame first, then index into that DataFrame, and you'll be good to go. Pick the option that fits how you want to handle parameters for analyze_playlist!

内容的提问来源于stack exchange,提问作者Lor_lab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 16:27:48