Я пытаюсь пройти конкурс Титаника в Kaggle. Пытаясь применить модель линейной регрессии к моему коду и проверяя ее точность, я получаю следующую ошибку в Pycharm:
Traceback (most recent call last):
File "C:/Users/security/Downloads/AP/Titanic-Kaggle/TItanic-Kaggle.py", line 27, in <module>
accuracy = linReg.score(x_text, y_test)
File "C:\Users\security\Anaconda3\envs\TItanic-Kaggle.py\lib\site-packages\sklearn\base.py", line 330, in score
return r2_score(y, self.predict(X), sample_weight=sample_weight,
File "C:\Users\security\Anaconda3\envs\TItanic-Kaggle.py\lib\site-packages\sklearn\linear_model\base.py", line 213, in predict
return self._decision_function(X)
File "C:\Users\security\Anaconda3\envs\TItanic-Kaggle.py\lib\site-packages\sklearn\linear_model\base.py", line 196, in _decision_function
X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
File "C:\Users\security\Anaconda3\envs\TItanic-Kaggle.py\lib\site-packages\sklearn\utils\validation.py", line 582, in check_array
context))
ValueError: Found array with 0 sample(s) (shape=(0, 4)) while a minimum of 1 is required.
Это мой код:
import pandas as pd
from sklearn.linear_model import LinearRegression
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])
linReg = LinearRegression()
# 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)
data = train[['Pclass', 'SibSp', 'Parch', 'Fare', 'Age']]
# Splitting the dataset that contains the missing values and no missing values as test and train respectively.
x_train = data[data['Age'].notnull()].drop(columns='Age')
y_train = data[data['Age'].notnull()]['Age']
x_text = data[data['Age'].isnull()].drop(columns='Age')
y_test = data[data['Age'].isnull()]['Age']
# Training the machine learning algorithm
linReg.fit(x_train, y_train)
# Checking the accuracy score of the model
accuracy = linReg.score(x_text, y_test)
print(accuracy*100, '%')