未知标签类型:连续

Posted

技术标签:

【中文标题】未知标签类型:连续【英文标题】:Unknown label type: continuous 【发布时间】:2018-01-20 14:12:39 【问题描述】:
Avg.SessionLength TimeonApp TimeonWebsite LengthofMembership Yearly Amount Spent
0   34.497268   12.655651   39.577668   4.082621    587.951054
1   31.926272   11.109461   37.268959   2.664034    392.204933
2   33.000915   11.330278   37.110597   4.104543    487.547505
3   34.305557   13.717514   36.721283   3.120179    581.852344
4   33.330673   12.795189   37.536653   4.446308    599.406092
5   33.871038   12.026925   34.476878   5.493507    637.102448
6   32.021596   11.366348   36.683776   4.685017    521.572175

我要申请KNN:

X = df[['Avg. Session Length', 'Time on App','Time on Website', 'Length of Membership']] 
y = df['Yearly Amount Spent'] 
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) 
from sklearn.neighbors import KNeighborsClassifier 
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train,y_train)

ValueError:未知标签类型:'连续'

【问题讨论】:

***.com/questions/41925157/… 【参考方案1】:

您正在寻找 KNeighborsRegressor 而不是 KNeighborsClassifier 将代码更改为

X = df[['Avg. Session Length', 'Time on App','Time on Website', 'Length of Membership']] 
y = df['Yearly Amount Spent'] 
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) 
from sklearn.neighbors import KNeighborsRegressor
knn = KNeighborsRegressor(n_neighbors=1)
knn.fit(X_train,y_train)

【讨论】:

嘿,如果我想创建一个 n_neighbors=1 的 KNN 模型实例并将这个 KNN 模型拟合到训练数据中。在创建混淆矩阵和分类报告之后。知道我该怎么做,基本上这就是我想做的事情。干杯 @MabReyaz scikit-learn.org/stable/auto_examples/neighbors/…,查看sklearn的例子~和这个scikit-learn.org/stable/modules/generated/…【参考方案2】:
def type_of_target(y):
    """Determine the type of data indicated by the target.

    Note that this type is the most specific type that can be inferred.
    For example:

        * ``binary`` is more specific but compatible with ``multiclass``.
        * ``multiclass`` of integers is more specific but compatible with
          ``continuous``.
        * ``multilabel-indicator`` is more specific but compatible with
          ``multiclass-multioutput``.

    Parameters
    ----------
    y : array-like

    Returns
    -------
    target_type : string
        One of:

        * 'continuous': `y` is an array-like of floats that are not all
          integers, and is 1d or a column vector.

## from knn.fit(X_train,y_train)  change y_train as y_train.astype(int)

【讨论】:

以上是关于未知标签类型:连续的主要内容,如果未能解决你的问题,请参考以下文章

如何修复未知标签类型:“连续”?

如何更正值错误:未知标签类型“连续”

由于“未知标签类型'连续'”,逻辑回归不起作用?

ValueError:未知标签类型:DecisionTreeClassifier() 中的“连续”

当我将 IterativeImputer 与 KNeighborsClassifier 一起使用时出现错误“未知标签类型:'连续'”

Python ValueError:未知标签类型:“连续”