Py学习  »  Python

基于数据更新的python预测

Taewoo.Lim • 5 年前 • 1541 次点击  

我目前正在做一个在线广告优化项目。 假设我唯一能改变的是cpc(每次点击的成本)。 我没有太多的数据,因为数据每天只更新一次。 我想得到CPC对净收入的预测,并希望让程序根据每天更新的数据,建议最佳CPC值,以最大化明天的净收入。

    cpc   margin
0   440 -95224.0
1   840 -81620.0
2   530 -57496.0
3   590 -47287.0
4   560 -45681.0
5   590 -52766.0
6   500 -60852.0
7   650 -59653.0
8   480 -48905.0
9   620 -56496.0
10  680 -53614.0
11  590 -44440.0
12  460 -34066.0
13  720 -31086.0
14  590 -23177.0
15  680 -12803.0
16  760 -10625.0
17  590 -20548.0
18  800 -15136.0
19  650 -12804.0
20  420 -63435.0
21  400  -7566.0
22  400  21136.0
23  400 -58585.0
24  400 -14166.0
25  420 -23065.0
26  400 -28533.0
27  380 -14454.0
28  400 -50819.0
29  380 -26356.0
30  400 -26322.0
31  380 -19107.0
32  400 -28270.0
33  380 -88439.0
34  360 -32207.0
35  340 -27632.0
36  340 -18050.0
37  340 -71574.0
38  340 -18050.0
39  320 -20735.0
40  300 -17984.0
41  290  -9426.0
42  280 -16555.0
43  290   2961.0

例如,上面的数据是 df .

我试着用 sklearn LogisticRegression 要得到预测:

import pandas as pd
from sklearn import datasets
from sklearn import metrics
from sklearn.linear_model import LogisticRegression

model = LinearRegression()
model.fit(df['cpc'], df['margin'])
prediction = model.predict([[300]])
print(prediction[0])

利润率是净收入,顺便说一下。

因此,通过这样做,我想我可以根据cpc为300时的数据得到预测,但是它返回了一个错误,说:

ValueError: Expected 2D array, got 1D array instead:
array=[440 840 530 590 560 590 500 650 480 620 680 590 460 720 590 680 760 590
 800 650 420 400 400 400 400 420 400 380 400 380 400 380 400 380 360 340
 340 340 340 320 300 290 280 290].
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.

我一直在寻找一些使用线性回归模型或物流回归模型的例子,但它们都使用二维数组作为输入,这不符合我的需要。我只有一个因素可以改变,结果就是净收入(或利润率)。

我如何在我的项目中使用sklearn?或者还有其他更好的方法来解决这个问题吗?

我对编程很陌生,不懂数学和统计学,这使得我更难理解或获取关键字来学习…请给我讲讲这个。

————————————————————————————————————————————————————--- 好吧,让我再给你一个df

    cpc    margin
0   440  -35224.0
1   340  -11574.0
2   380  -68439.0
3   420  -23435.0
4   840  -81620.0
5   400  -38585.0
6   530  -37496.0
7   590   -7287.0
8   560   -5681.0
9   590  -32766.0
10  500  -60852.0
11  400  -30819.0
12  650  -59653.0
13  480  -28905.0
14  620  -56496.0
15  680  -53614.0
16  590  -44440.0
17  460  -14066.0
18  420   16935.0
19  360  -12207.0
20  400   -8533.0
21  400   -6322.0
22  400   25834.0
23  720  -31086.0
24  400  121136.0
25  400  -28270.0
26  340    1950.0
27  340    1950.0
28  300    2016.0
29  340  -27632.0
30  400   32434.0
31  380  -26356.0
32  590  -23177.0
33  680    7197.0
34  320  -20735.0
35  760    9375.0
36  590  -20548.0
37  290   10574.0
38  380  -19107.0
39  290   42961.0
40  280  -16555.0
41  800  -15136.0
42  380  -14454.0
43  650  -12804.0

多亏你的回答,我可以更进一步如下。 在我可以毫无错误地运行代码之后,我想通过循环输入,我将能够获得最佳的cpc值。

import pandas as pd
from sklearn import datasets
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
df = pd.DataFrame(final_db)
model = LogisticRegression()
x = df[['cpc']]
model.fit(x, df['margin'])
previous_prediction = -99999999999999
df_prediction = []
for i in list(range(10, 1000, 10)):
    prediction = model.predict([[i]])
    df_prediction.append({'cpc':i, 'margin' : prediction})
    if prediction > previous_prediction:
        previous_prediction = prediction
        previous_i = i

结果如下 enter image description here

这不是很令人满意。根据我掌握的数据,有没有更好的模型可以使用?为了达到我的目标,还有其他的建议吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46089
 
1541 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Dev Khadka
Reply   •   1 楼
Dev Khadka    5 年前

我想是抱怨这条线
model.fit(df['cpc'], df['margin'])

其中第一个参数应该是二维数组。可以使用数据帧的数组索引
df[['cpc']]
以获取数据帧而不是将解决此问题的序列