How can i make this program predict strings as well? This is the error i keep getting: could not convert string to float: 'female'

Viewed 23

Can anyone help me, please? I've been stuck on this question for a while now. I've been told this can't be done with regression, it has to be classification. Is that true? This is the code:

import pandas as pd
import numpy as np
import sklearn
from sklearn import linear_model


data = pd.read_csv("InsuranceCostCalculator/Insurance.csv")
data = data[["age", "sex", "bmi", "children", "smoker", "region", "charges",]]


predict = ('charges')

X = np.array(data.drop([predict], 1)) #X is our training data
Y = np.array(data[predict])

x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X, Y, test_size=0.1)

Linear = linear_model.LinearRegression()

Linear.fit(x_train, y_train)

prediction = Linear.predict(x_test)

for x in range(len(prediction)):
    print(prediction[x], x_test[x], y_test[x])
1 Answers

This error occured because before dataset fit into the model you have to encode features to float numbers.For female/male you can use label encoder or any other encoder. Simmilary every categorical data must encode before fit to the model.Search about encoders such as onehot encoder.

On the otherhand if the prediction task is a clasification task you can use linear clasifier rather than using linear regressor.

Related