1
$\begingroup$

I'm starting with the regression models in Python, so I used the Titanic dataset from Kaggle. Problem is after I fit the training datasets and ran predict(), the accuracy returned as 100%, and the scores are returning the same.

Therefore I know something is wrong.

Preprocessing

I imported the dataset, filled the missing values, created some rules for the values like below:

df = pd.read_csv("test.csv")

df = df.drop(['PassengerId','Ticket','Fare','Cabin','Name'], axis=1)
df['Age'] = df['Age'].fillna(value=29)
df['Embarked'] = df.fillna('C')

y = df['Survived']
X = df.drop(['Survived'],axis=1)

X.loc[X['SibSp'] >= 2, 'SibSp'] = 2
X.loc[X['Parch'] >= 3, 'Parch'] = 3
X.loc[X['Age'] < 15, 'Age'] = 0
X.loc[(X['Age'] >= 15) & (X['Age'] < 60), 'Age'] = 1
X.loc[X['Age'] >= 60, 'Age'] = 2

X = pd.get_dummies(X,columns=['Embarked'])
X = pd.get_dummies(X,columns=['Pclass'])
X = pd.get_dummies(X,columns=['Sex'])

Then I used train_test_split() to create the training and testing datasets:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state=84)

Post-processing

model = LogisticRegression().fit(X_train,y_train)
y_pred = model.predict(X_test)

accuracy = metrics.accuracy_score(y_test, y_pred)
accuracy_percentage = 100 * accuracy
print(accuracy_percentage)

print(model.score(X_train,y_train))
print(model.score(X_test, y_pred))

Both scores returned 1.0, and the accuracy I ran also returned 100%.

I've compared to numerous tutorials on the internet I could find, so I'm really lost here. I have probably written wrong code somewhere, but I'm struggling to identify it. I checked other results on StackExchange but no good either.

Any help?

My datasets have the respective shape: X_train: (668,10), X_test: (223,10), y_test: (223,), y_train: (668,)

Solution

The problem was indeed with the code, specifically in this part:

df['Embarked'] = df.fillna('C')

What it should've been is:

df['Embarked'] = df['Embarked'].fillna('C')

The consequence was that my 'Embarked' columns became either 1s or 0s instead of the actual port of embark.

$\endgroup$
  • 1
    $\begingroup$ Do you have a RHS variable that is perfectly correlated with the dependent variable? $\endgroup$ – dlnB Apr 20 at 21:34
  • $\begingroup$ Yep, the "y" dataset is the dependent variable from the model. It's called "Survived", and it's a column with either 1 or 0, depending on whether the person died or not. $\endgroup$ – DuckRodgers Apr 20 at 21:39
  • $\begingroup$ I think you misunderstood my question. Is one of your "x's" perfectly correlated with your "y"? $\endgroup$ – dlnB Apr 20 at 21:45
  • $\begingroup$ The problem lies in your dummy variables, which make your data perfectly correlated. $\endgroup$ – vvvityaaa Apr 20 at 22:13
1
$\begingroup$

Embarked and Survived are perfectly correlated to be exact. Drop it.

To see this:

df = pd.read_csv("test.csv")

df = df.drop(['PassengerId','Ticket','Fare','Cabin','Name'], axis=1)
df['Age'] = df['Age'].fillna(value=29)
df['Embarked'] = df.fillna('C')

#y = df['Survived']
#X = df.drop(['Survived'],axis=1)
X = df.copy()

X.loc[X['SibSp'] >= 2, 'SibSp'] = 2
X.loc[X['Parch'] >= 3, 'Parch'] = 3
X.loc[X['Age'] < 15, 'Age'] = 0
X.loc[(X['Age'] >= 15) & (X['Age'] < 60), 'Age'] = 1
X.loc[X['Age'] >= 60, 'Age'] = 2

X = pd.get_dummies(X,columns=['Embarked'])
X = pd.get_dummies(X,columns=['Pclass'])
X = pd.get_dummies(X,columns=['Sex'])

print(X.corr())

Or another way to see this would be

df = pd.read_csv("test.csv")

df = df.drop(['PassengerId','Ticket','Fare','Cabin','Name'], axis=1)
df['Age'] = df['Age'].fillna(value=29)
df['Embarked'] = df.fillna('C')

df['S==E'] = df.Survived == df.Embarked 
print(df.loc[df['S==E'] == False])

| cite | improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ You're right! I ran X.corr() and 'Survived' was perfectly correlated to 'Embarked_0'. I feel this shouldn't be happening, was it my code or just coincidence? I'll keep trying. Thanks you! $\endgroup$ – DuckRodgers Apr 20 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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