使用旧客户数据瞄准新客户



我有两个表。

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

我遵循的过程。

  1. 对老客户数据进行RFM分割,将客户分为11类
  2. 由于大多数数据都是字符串类型,因此通过LabelEncode((转换为数字
  3. 旧客户数据分为X列(3492,12(和X测试(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.

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

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

Xtest = Xtest.reshape(1,-1)

我不知道为什么(-1,1(不起作用。

最新更新