0
$\begingroup$

Summarize the problem: Received an error

 1 # predict the test dataset
----> 2 yhat = model.predict(X_test)

File c:\Users\nwm2\Anaconda3\lib\site-packages\sklearn\linear_model\_base.py:425, in LinearClassifierMixin.predict(self, X)
    411 def predict(self, X):
    412     """
    413     Predict class labels for samples in X.
    414 
   (...)
    423         Vector containing the class labels for each sample.
    424     """
--> 425     scores = self.decision_function(X)
    426     if len(scores.shape) == 1:
    427         indices = (scores > 0).astype(int)

File c:\Users\nwm2\Anaconda3\lib\site-packages\sklearn\linear_model\_base.py:407, in LinearClassifierMixin.decision_function(self, X)
    387 """
    388 Predict confidence scores for samples.
    389 
   (...)
    403     this class would be predicted.
    404 """
...
    118         )
    119 # for object dtype data, we only check for NaNs (GH-13254)
    120 elif X.dtype == np.dtype("object") and not allow_nan:
"ValueError: Input contains NaN, infinity or a value too large for dtype('float64')."

Provide details and any research: Converting breast cancer dataset - categorical data into numbers using feature-engine. Seems to work up to - yhat = model.predict(X_test) with above error.

I've tried checking for nans in my dataset but there's none.

Here's the code

# load libraries
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from feature_engine.encoding import OrdinalEncoder
import pandas as pd
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import r2_score

# load dataset
df = pd.read_csv('https://raw.githubusercontent.com/jbrownlee/Datasets/master/breast-cancer.csv', header=None)
df.head()

# assign labels to features
names = ['Age', 'Menopause', 'Tumor-Size', 'Inv-Nodes', 'Node-Caps', 'Deg-Malig', 'Breast', 'Breast-Quad', 'Irradiat', 'Class']
df.columns = names

# use a loop to determine which features are categorical and which are numerical
for name in names:
    if name != 'Class':
        if df[name].dtype == 'object':
            print(name, 'is categorical')
        else:
            print(name, 'is numerical')

# print out the number of categorical features
print('Number of categorical features:', len(df.select_dtypes(include=['object'])))

# print out the number of numerical features
print('Number of numerical features:', len(df.select_dtypes(include=['number'])))

# use train test split method from scikit-learn library to seperate dataset into 70% training and 30% test
X_train, X_test, y_train, y_test = train_test_split(df.drop(['Class'], axis=1), df['Class'], test_size=0.3, random_state=1)

# use feature_engine method Ordinal Encoder to convert categorical features to ordinal
encoder = OrdinalEncoder(encoding_method='arbitrary')

#fit the data to the model
encoder.fit(X_train)

# use transform to encode the categories to numbers
X_train = encoder.transform(X_train)
X_test = encoder.transform(X_test)

#check for nans in X_test
print(X_test.isnull().sum())

# Ordinal encode target variable y
label_encoder = LabelEncoder()
label_encoder.fit(y_train)
y_train = label_encoder.transform(y_train)
y_test = label_encoder.transform(y_test)

# check for any nans
print(df.isnull().sum())

# use logistic regression method from scikit-learn library to predict malignancy
model = LogisticRegression()

# fit the model to the training dataset
model.fit(X_train, y_train)

print(X_test.isnull().sum())

X_test = X_test.fillna(X_test.mean())
X_test.isnull().sum()

# predict the test dataset - error happens here!
yhat = model.predict(X_test)**

# print out accuracy score using accuracy_score method from scikit-learn library
accuracy_score = accuracy_score(y_test, yhat)
print('Accuracy: %.2f' % (accuracy_score * 100))
```
$\endgroup$
1
  • $\begingroup$ With changes above, accuracy_score = accuracy_score(y_test, yhat) -> 1 # print out accuracy score using accuracy_score method from scikit-learn library ----> 2 accuracy_score = accuracy_score(y_test, yhat) 3 print('Accuracy: %.2f' % (accuracy_score * 100)) 4 5 # use mean absolute error to determine variance from mean, print out the error 6 print('Mean absolute error: %.2f' % mean_absolute_error(y_test, yhat)) TypeError: 'numpy.float64' object is not callable $\endgroup$
    – George Ng
    Aug 13, 2022 at 12:57

1 Answer 1

1
$\begingroup$

You need to use dropna before train test split.

Xtest has na because it was made from df having na

$\endgroup$
3
  • $\begingroup$ Thanks for the tip. As for nans, seems it was generated during the encoding process 'X_test = encoder.transform(X_test) c:\Users\nwm2\Anaconda3\lib\site-packages\feature_engine\encoding\base_encoder.py:238: UserWarning: During the encoding, NaN values were introduced in the feature(s) Inv-Nodes. warnings.warn( $\endgroup$
    – George Ng
    Aug 13, 2022 at 12:22
  • $\begingroup$ #check for nans in X_test print(X_test.isnull().sum()) generates nans in Inv-Nodes not sure why $\endgroup$
    – George Ng
    Aug 13, 2022 at 12:25
  • $\begingroup$ Used X_test = X_test.fillna(X_test.mean()) to avoid an error msg $\endgroup$
    – George Ng
    Aug 13, 2022 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.