TypeError: predict() missing 1 required positional argument: 'X'

Hello to everyone!

I am new to Python and Iam getting this error when running XGBoost Ranker about a Ranking problem:

Traceback (most recent call last): File "C:/Users/USER/pythonProject/main.py", line 88, in <module> test_pred = XGBRanker.predict(X_test)  
TypeError: predict() missing 1 required positional argument: 'X'

Μy dataset consists of 6 columns: [“label”,“id”,“comments”,“likes”,“product 1 frequency”,“product 2 frequency”] and 1222 rows.

Here is my code:

# xgboost Ranker
from numpy import asarray
from numpy import mean
from numpy import std
from xgboost import XGBRanker
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import train_test_split
from matplotlib import pyplot
import pandas as pd
from pandas import read_csv

# Load data
names = ["label","id","comments","likes","product 1 frequency","product 2 frequency"]
dataset = pd.read_csv("Product Ranking.csv", names=names, encoding="utf-8", error_bad_lines=True,
					   skip_blank_lines=True, sep=",", delimiter=None, doublequote=True, keep_default_na=True,
					   nrows=1223, header=6, engine="python")

# Shape
print(dataset.shape)

# Max labels
max_label = dataset.label.nunique()
print(max_label)


# Core Model
model = XGBRanker()


# Split the data
array = dataset.values
X = array [:,0:4]
y = array [:,5]
X = X.astype('int64')



# Split the Data in training and testing set
X_train, X_test, y_train, y_test = train_test_split(X,y,
                                                    train_size=0.75, test_size=0.25,random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)




# evaluate the model
model = XGBRanker(tree_method='hist',gpu_id=-1,
    booster='gbtree',
    objective='rank:ndcg',
    random_state=42,
    learning_rate=0.1,
    colsample_bytree=0.9,
    eta=0.05,
    max_depth=6,
    n_estimators=110,
    subsample=0.75)



# Fit the model
model = XGBRanker()
model.fit(X,y, group=[10, 24, 34, 14, 20, 22, 20, 19, 10, 23, 22,
				 10, 27, 14, 22, 11, 14, 10, 10, 26, 10, 10,
				 13, 10, 10, 21, 17, 12, 21, 10, 10, 10, 27,
				 12, 21, 10, 10, 11, 13, 10, 15, 17, 10, 9,
				 10, 10, 10, 10, 10, 10, 14, 23, 10, 10, 13,
				 10, 10, 10, 18, 16, 16, 11, 9, 10, 10, 10,
				 10, 11, 12, 10, 10, 18, 10, 16, 12, 10, 9,
				 14, 11, 10, 11, 11, 10, 10, 12, 9, 12, 11, 8, 17])

# Prediction
test_pred = XGBRanker.predict(X_test)
X_test["predicted_ranking"] = test_pred
predictions = (dataset.groupby("likes")
               .apply(lambda x: predicted_ranking(model, X)))

When I replace the Prediction code with the one below, it displays exactly the same error:

Prediction:

test_pred = XGBRanker.predict(X_test)
X_test["predicted_ranking"] = test_pred
X_test.sort_values("predicted_ranking", ascending=False)

Can anyone help me? I will appreciate your help!

Thank you in advance!
Sofia

You are calling the predict function on the imported class.

XGBRanker.predict(X_test)

You should instead be calling it on your fit model.

model.predict(X_test)

Once that is fixed, it should generate the prediction. I am mot sure what you are doing with the lambda function in the apply method though, that might also error once you fix the prediction code.

1 Like