0

whenever I pass my first function to the other one I get this error,why is this happening and how can I solve it??

def analyze_playlist(*args):
        
        
        playlist_features_list = ["artist","track_name","track_id","danceability","energy","key","loudness","mode", "speechiness",'acousticness',"instrumentalness","liveness","valence","tempo", "duration_ms"]
        
        playlist_df = pd.DataFrame(columns = playlist_features_list)
        
       
        
        playlist_tracks = sp.playlist_tracks(*args)["items"]
        for track in playlist_tracks:
            
            playlist_features = {}
            
            playlist_features["artist"] = track["track"]["artists"][0]["name"]
            playlist_features["track_name"] = track["track"]["name"]
            playlist_features["track_id"] = track["track"]["id"]
            
        
            audio_features = sp.audio_features(playlist_features["track_id"])[0]
            for feature in playlist_features_list[3:]:
                playlist_features[feature] = audio_features[feature]
            
            
            track_df = pd.DataFrame(playlist_features, index = [0])
            playlist_df = pd.concat([playlist_df, track_df], ignore_index = True)
            
        return playlist_df
    
        analyze_playlist(a)
    
    def classify_playlist(func):
        spotify=pd.read_csv('../OUTPUT/spotify.csv')
        spotify.drop(columns=['Unnamed: 0'],inplace=True)
        spotify.columns
        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=func[['danceability', 'energy',
                'loudness',  'speechiness', 'acousticness',
               'instrumentalness', 'liveness', 'valence']].values
        testdata= forest.predict(tx)
        return testdata
Right now I am calling my second function passing it my second function and I get this error.
classify_playlist(analyze_playlist)

---> 14 tx=func[['danceability', 'energy', 15 'loudness', 'speechiness', 'acousticness', 16 'instrumentalness', 'liveness', 'valence']].values

TypeError: 'function' object is not subscriptable

Lor_lab
  • 11
  • 2
  • 2
    Have you tried? – GPhilo Jul 30 '20 at 08:32
  • Yes, you can pass a function to another function. – L3viathan Jul 30 '20 at 08:33
  • Yes, you can but don't call the function just pass the name, in python functions are variables that you can call. It would be `def func (f): f()` when making the function that takes function and when calling it `func(f)` you might have figured it out but you can pass the function itself like `func(func)`. – 12ksins Jul 30 '20 at 08:42
  • Look at e.g. the `key=` argument for `sorted` for a common example. – tripleee Jul 30 '20 at 08:44

1 Answers1

1

Yes, a function can be passed as argument.

def square(x):
    return x * x


def apply(func, val):
    return func(val)


apply(square, 2)
4
Jérôme
  • 13,328
  • 7
  • 56
  • 106