I am new to Machine learning and started course on Simple Linear Regression model recently.
I have a dataset where except for a column id (integer type), all the columns are of String datatype.
And I have loaded it into a pandas dataframe and selected indexes out of it as below.
The pandas dataframe has total 32 columns and the 33rd column is the dependent variable column that just says YES or NO.
Using all the independent variables (columns 0 to 31), I am trying to find if I can predict the values in column 32 which is my dependent variable.
data = psyco.read_into_pandas()
X = data.iloc[:, 1:33].values
Y = data.iloc[:, 32].values
# Add missing values
imputer = SimpleImputer(missing_values=np.nan, strategy='most_frequent', add_indicator=True)
# Fit the rows and columns into the imputer
imputer.fit(X[:, 1:33])
# Transform the data.
X[:, 1:33] = imputer.transform(X[:, 1:33])
# One hot encoding
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])], remainder='passthrough')
X = np.array(ct.fit_transform(X))
# Label Encoder
le = LabelEncoder()
Y = le.fit_transform(Y)
# Split data into train and test data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
Before sending the values of X_train and Y_train, I just printed the values of Y_train and I can see that it contains an array of integers which could be seen in the image below.
But when I send the data of X_train and Y_train to my LinearRegression() I am facing an error that says:
ValueError: could not convert string to float: 'yes'
Full error:
Traceback (most recent call last):
File "/Some/Path/mltask.py", line 52, in task_2
lr.fit(X_train, Y_train)
File "/Some/Path/venv/lib/python3.9/site-packages/sklearn/linear_model/_base.py", line 684, in fit
X, y = self._validate_data(
File "/Some/Path/venv/lib/python3.9/site-packages/sklearn/base.py", line 596, in _validate_data
X, y = check_X_y(X, y, **check_params)
File "/Some/Path/venv/lib/python3.9/site-packages/sklearn/utils/validation.py", line 1074, in check_X_y
X = check_array(
File "/Some/Path/venv/lib/python3.9/site-packages/sklearn/utils/validation.py", line 856, in check_array
array = np.asarray(array, order=order, dtype=dtype)
ValueError: could not convert string to float: 'yes'
What I don't understand is when I print Y_train I see integers in the array but the regression says it can't convert String to float.
Could anyone let me know if I missed any step in between and how can I correct my mistake ? Any help is massively appreciated.
