Search code examples
pythonpandasmachine-learninglinear-regressionkaggle

Getting a value error for Linear Regression model


I am trying to work through Kaggle's Titanic competition. While trying to apply the Linear Regression model to my code and checking its accuracy score, I get the following error on 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.

This is my code so far:

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, '%')

Solution

  • Try this replacement and it will work:

    x_text = data[data['Age'] != None].drop(columns='Age')
    y_test = data[data['Age'] != None]['Age']
    

    This will help.