目标变量的字符串和数值混合

我在测试下面的代码,并且在最后一行得到了一个错误。

dataset = df[['Rate', 'Weights', 'Change', 'Price', 'CategoryOne']].copy() # dataset.shapeX = dataset.iloc[:, :-1].valuesy = dataset.iloc[:, 4].valuesfrom sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)from sklearn.preprocessing import StandardScalerscaler = StandardScaler()scaler.fit(X_train)X_train = scaler.transform(X_train)X_test = scaler.transform(X_test)#Import knearest neighbors Classifier modelfrom sklearn.neighbors import KNeighborsClassifier#Create KNN Classifierknn = KNeighborsClassifier(n_neighbors=5)#Train the model using the training setsknn.fit(X_train, y_train)#Predict the response for test datasety_pred = knn.predict(X_test)#Import scikit-learn metrics module for accuracy calculationfrom sklearn import metrics# Model Accuracy, how often is the classifier correct?print("Accuracy:",metrics.accuracy_score(y_test, y_pred))#Import knearest neighbors Classifier modelfrom sklearn.neighbors import KNeighborsClassifier#Create KNN Classifierknn = KNeighborsClassifier(n_neighbors=7)#Train the model using the training setsknn.fit(X_train, y_train)

在最后一行,当我尝试拟合X_train和y_train时,我得到了这个错误:

TypeError: '<' not supported between instances of 'int' and 'str'

CategoryOne字段中的数据看起来像这样: '2a', '1', '2a'。这可能是问题所在吗?我知道目标变量不必是数值的。我只是想看看自变量和因变量(CategoryOne)之间的关系。

这是堆栈跟踪:

Traceback (most recent call last):  File "<ipython-input-108-36266936f0ca>", line 29, in <module>    knn.fit(X_train, y_train)  File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\neighbors\base.py", line 906, in fit    check_classification_targets(y)  File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\multiclass.py", line 166, in check_classification_targets    y_type = type_of_target(y)  File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\multiclass.py", line 287, in type_of_target    if (len(np.unique(y)) > 2) or (y.ndim >= 2 and len(y[0]) > 1):  File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\arraysetops.py", line 264, in unique    ret = _unique1d(ar, return_index, return_inverse, return_counts)  File "C:\Users\rs\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\arraysetops.py", line 312, in _unique1d    ar.sort()TypeError: '<' not supported between instances of 'int' and 'str'

回答:

您可以尝试通过修改dataset的构建方式,将CategoryOne列明确追加为字符串数据,如下所示:

dataset = df[['Rate', 'Weights', 'Change', 'Price', 'CategoryOne']].copy()dataset['CategoryOne'] = dataset['CategoryOne'].map(lambda x : str(x))

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中创建了一个多类分类项目。该项目可以对…

发表回复

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