I am working through the Titanic competition. This is my code so far:
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
train = pd.read_csv("https://raw.githubusercontent.com/oo92/Titanic-Kaggle/master/train.csv")
test = pd.read_csv("https://raw.githubusercontent.com/oo92/Titanic-Kaggle/master/test.csv")
train['Sex'].replace(['female', 'male'], [0, 1])
train['Embarked'].replace(['C', 'Q', 'S'], [1, 2, 3])
# Fill missing values in Age feature with each sex’s median value of Age
train['Age'].fillna(train.groupby('Sex')['Age'].transform("median"), inplace=True)
linReg = LinearRegression()
data = train[['Pclass', 'Sex', 'Parch', 'Fare', 'Age']]
# implement train_test_split
x_train, x_test, y_train, y_test = train_test_split(data, train['Survived'], test_size=0.2, random_state=0)
# Training the machine learning algorithm
linReg.fit(x_train, y_train)
# Checking the accuracy score of the model
accuracy = linReg.score(x_test, y_test)
print(accuracy*100, '%')
This line previously looked like this: data = train[['Pclass', 'Parch', 'Fare', 'Age']]
, which ended up giving me an accuracy score of 19.5%. I realized that I didn't include sex so I went ahead and did this:
data = train[['Pclass', 'Sex', 'Parch', 'Fare', 'Age']]
Then, I got the following error:
ValueError: could not convert string to float: 'female'
Here I realized that the changes that I've done to my train['Sex']
and train['Age']
did not reflect on the training and the testing of the model, which seems to be the reason why my model performed at 19.5%. How do I come across this problem?
UPDATE
After the first answer, i tried to modify this line accordingly :
train['Age'].fillna(train.groupby('Sex')['Age'].transform("median"), inplace=True)
with :
train['Age'] = train['Age'].fillna(train.groupby('Sex')['Age'].transform("median"), inplace=True)
And i then decided to print the Age
column and it turns out that the values are corrupted:
0 None
1 None
2 None
3 None
4 None
5 None
6 None
7 None
8 None
9 None
10 None
11 None
12 None
13 None
14 None
15 None
16 None
17 None
18 None
19 None
20 None
21 None
22 None
23 None
24 None
25 None
26 None
27 None
28 None
29 None
...
861 None
862 None
863 None
864 None
865 None
866 None
867 None
868 None
869 None
870 None
871 None
872 None
873 None
874 None
875 None
876 None
877 None
878 None
879 None
880 None
881 None
882 None
883 None
884 None
885 None
886 None
887 None
888 None
889 None
890 None
Name: Age, Length: 891, dtype: object