Sklearn:TypeError:单例数组

2024-10-01 00:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有列车数据帧,其中一部分是

date    city    brand   model   price   count
2016-02 abakan  audi    a6  200 тыс - 500 тыс   1
2016-02 abakan  bmw 5-series    200 тыс - 500 тыс   2
2016-02 abakan  bmw x5  200 тыс - 500 тыс   2
2016-02 abakan  chery   a15 200 тыс - 500 тыс   1
2016-02 abakan  chevrolet   cruze   200 тыс - 500 тыс   3
2016-02 abakan  chevrolet   cruze   500 тыс - 1 млн 10
2016-02 abakan  chevrolet   lacetti 200 тыс - 500 тыс   1
2016-02 abakan  gaz 69  до 200 тыс  2
2016-02 abakan  honda   element 200 тыс - 500 тыс   1

测试的一部分是

^{pr2}$

我需要预测count 我用密码

X = pd.read_excel('result_drom2.xlsx')
X_predict = pd.read_excel('test.xlsx')
y = X.count
del X['count']
label = LabelEncoder()
def cat_to_num(df, column):
    dicts = {}
    label.fit(df[column].drop_duplicates())
    dicts[column] = list(label.classes_)
    df[column] = label.transform(df[column])

cat_to_num(X, 'date')    
cat_to_num(X, 'city')
cat_to_num(X, 'brand')
cat_to_num(X, 'model')
cat_to_num(X, 'price')

cat_to_num(X_predict, 'date') 
cat_to_num(X_predict, 'city')
cat_to_num(X_predict, 'brand')
cat_to_num(X_predict, 'model')
cat_to_num(X_predict, 'price')

model = LinearRegression()
model.fit(X, y)
y_predict = model.predict(X_predict)
y_predict.to_excel('res.xlsx')

但是这个代码返回一个错误

TypeError: Singleton array array(<bound method DataFrame.count of , dtype=object) cannot be considered a valid collection.

我怎样才能解决这个问题?在


Tags: tocitydfdatemodelcountcolumnprice