无法将字符串转换为浮点数以及如何使用此数据集训练模型

我有一个数据集,包含以下列:年龄(浮点类型)、性别(字符串类型)、地区(字符串类型)和费用(浮点类型)。

我想使用年龄、性别和地区作为特征来预测费用,如何在scikit-learn中实现这一点?

我尝试了一些方法,但显示了 "ValueError: could not convert string to float: 'northwest' "

import pandas as pdimport numpy as npdf = pd.read_csv('Desktop/insurance.csv')X = df.loc[:,['age','sex','region']].valuesy = df.loc[:,['charges']].valuesfrom sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)from sklearn import svmclf = svm.SVC(C=1.0, cache_size=200,decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf')clf.fit(X_train, y_train)

回答:

region列包含字符串,这些字符串不能直接在SVM分类器中使用,因为它们不是向量。

因此,你需要将这一列转换为SVM可以使用的形式。这里有一个例子,通过将region转换为分类系列:

import pandas as pdfrom sklearn import svmfrom sklearn.model_selection import train_test_splitdf = pd.DataFrame({'age':[20,30,40,50],              'sex':['male','female','female','male'],              'region':['northwest','southwest','northeast','southeast'],              'charges':[1000,1000,2000,2000]})df.sex = (df.sex == 'female')df.region = pd.Categorical(df.region)df.region = df.region.cat.codesX = df.loc[:,['age','sex','region']]y = df.loc[:,['charges']]X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)clf = svm.SVC(C=1.0, cache_size=200,decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf')clf.fit(X_train, y_train)

解决这个问题另一种方法是使用独热编码:

import pandas as pdfrom sklearn import svmfrom sklearn.model_selection import train_test_splitdf = pd.DataFrame({'age':[20,30,40,50],              'sex':['male','female','female','male'],              'region':['northwest','southwest','northeast','southeast'],              'charges':[1000,1000,2000,2000]})df.sex = (df.sex == 'female')df = pd.concat([df,pd.get_dummies(df.region)],axis = 1).drop('region',1)X = df.drop('charges',1)y = df.chargesX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)clf = svm.SVC(C=1.0, cache_size=200,decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf')clf.fit(X_train, y_train)

还有一个方法是执行标签编码:

from sklearn.preprocessing import LabelEncoderle = LabelEncoder()df.region = le.fit_transform(df.region)

当然,这些方法并不全面,它们的表现根据你的问题不同而有所不同。

使用非数值数据是一个非平凡的问题,需要对现有技术有一定的了解(我建议你去kaggle的论坛上搜索,那里可以找到有价值的信息)。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注