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.
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)
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.
My datasets have the respective shape: X_train: (668,10), X_test: (223,10), y_test: (223,), y_train: (668,)
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.