首页 > 解决方案 > 使用旧客户数据定​​位新客户

问题描述

我有两张桌子。

  1. 老客户数据,包括交易数据。
  2. 没有交易数据的新客户数据。我需要对数据进行建模以推荐新客户表中的哪些客户作为目标。

我遵循的过程。

  1. RFM对老客户数据进行细分,将客户分为11类。
  2. 由于大多数数据是字符串类型,因此通过 LabelEncoder() 转换为数字。
  3. 旧客户数据分为 X train(3492,12) 和 X test(3492,)
  4. 新的客户数据只是 Ytrain(983,12)。括号中的值是它的形状。
  5. 执行 KNN 算法。

请建议该过程是否正确我也遇到以下错误。

train_cols = ['address', 'state', 'gender', 'job_title', 'job_industry_category', 'wealth_segment', 'owns_car', 'Title']
from sklearn.preprocessing import LabelEncoder
enc = LabelEncoder()
for col in train_cols:
  Training[col] = Training[col].astype('str')
  Training[col] = enc.fit_transform(Training[col])
//Training is the old customer data

test_cols = ['address', 'state', 'gender', 'job_title', 'job_industry_category', 'wealth_segment', 'owns_car']
from sklearn.preprocessing import LabelEncoder
enc = LabelEncoder()
for col in test_cols:
  Test[col] = Test[col].astype('str')
  Test[col] = enc.fit_transform(Test[col])
//Test is the new customer data

Xtrain = Xtrain.transpose(); Ytrain = Ytrain.transpose()
//shape - Xtrain = (12,3492), Ytrain = (12,983)

from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=5)
classifier.fit(Xtrain, Ytrain)

y_pred = classifier.predict(Xtest)

错误:

//ValueError                                Traceback (most recent call last)
<ipython-input-211-8ae3ac010601> in <module>()
----> 1 y_pred = classifier.predict(Xtest)

1 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    554                     "Reshape your data either using array.reshape(-1, 1) if "
    555                     "your data has a single feature or array.reshape(1, -1) "
--> 556                     "if it contains a single sample.".format(array))
    557 
    558         # in the future np.flexible dtypes will be handled like object dtypes

ValueError: Expected 2D array, got 1D array instead:
array=[10  2  3 ...  4  0  3].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

该模型经过训练但无法预测。我无法重塑它。请帮我。

标签: pythonmachine-learningscikit-learnclassificationknn

解决方案


我改变了数组的形状,问题就解决了:

Xtest = Xtest.reshape(1,-1)

我不确定为什么 (-1,1) 不起作用。


推荐阅读