python - 使用旧客户数据定位新客户
问题描述
我有两张桌子。
- 老客户数据,包括交易数据。
- 没有交易数据的新客户数据。我需要对数据进行建模以推荐新客户表中的哪些客户作为目标。
我遵循的过程。
- RFM对老客户数据进行细分,将客户分为11类。
- 由于大多数数据是字符串类型,因此通过 LabelEncoder() 转换为数字。
- 旧客户数据分为 X train(3492,12) 和 X test(3492,)
- 新的客户数据只是 Ytrain(983,12)。括号中的值是它的形状。
- 执行 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) 不起作用。
推荐阅读
- vue.js - vue js中两个文件输入字段之间的区别
- react-native - React Native 无法连接到 Android 中的 SSE
- r - 将 lapply 用于矩阵上的函数,其中列作为 R 中的元素
- reactjs - 模拟单击我的组件中的另一个组件不起作用
- system-verilog - 实例数组,如何知道数组中特定实例的索引是什么
- reactjs - 如何根据 id 渲染产品
- python - 在 Odoo 中修剪价值
- c# - ASP.NET Core 托管服务 ExecuteAsync() 方法未完成
- cordova - 在搜索路径中找不到包含的文件“Pods/Target Support Files/Pods-Sync/Pods-Sync.debug.xcconfig”
- flutter - 我想以编程方式调用我的 appdrawer 中的页面