如何基于sklearn python中的一个值进行预测

时间:2019-02-07 02:48:15

标签: python scikit-learn

是否可以仅基于x值训练数据并进行预测?

在我的图表上,我的点(35,20)为黑色。用35预测时,该值应返回0,但是像15这样的点(大多数数据点都在黑线上方)应返回1

This is what my data looks like

def createFeatures(startTime, datapoints, function, *days):

    trueStrength = []
    functionData = []
    beginPrice = []
    endPrice = []
    deltaPrice = []

    for x in range(datapoints*5):

        #----Friday Data----
        if x%4 == 0 and x != 0:
            endPrice.append((sg.HighPrice[startTime+x]+sg.LowPrice[startTime+x]+sg.ClosePrice[startTime+x])/3)

        #----Monday Data----
        if x%5 == 0:
            functionData.append(function(trueStrength, startTime+x, *days))
            beginPrice.append((sg.HighPrice[startTime+x]+sg.LowPrice[startTime+x]+sg.ClosePrice[startTime+x])/3)

    for x in range(len(beginPrice)):
        deltaPrice.append(endPrice[x] - beginPrice[x])
    return functionData , deltaPrice

def createLabels(data, deltaPrice):
    labels = []
    for x in range(len(data)):
        if deltaPrice[x] > 0:
            labels.append(1.0)
        else:
            labels.append(0.0)
    return labels

x, y = createFeatures(20, 200, ti.SMA, 7)
z = createLabels(x,y)

这是我的线性回归模型:

labels = np.asarray(at.z)
x = np.asarray([at.x])
y = np.asarray([at.y])

testX=35.1
testY=20.1
test = np.array([[testX, testY]])

clf = LinearRegression().fit(x, y)
print clf.predict(4)

2 个答案:

答案 0 :(得分:0)

您似乎正在尝试线性回归。相关文档为here

您只能基于x值进行预测,但是您需要y值进行训练(否则您怎么知道要预测什么?)

来自sklearn:

import numpy as np
from sklearn.linear_model import LinearRegression

X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
#y = 1 * x_0 + 2 * x_1 + 3
y = np.dot(X, np.array([1, 2])) + 3
reg = LinearRegression().fit(X, y)
reg.score(X, y)

print(reg.coef_, reg.intercept_ )

reg.predict(np.array([[3, 5]]))

答案 1 :(得分:0)

完整的示例

import numpy as np
from sklearn.linear_model import LinearRegression

x = np.random.rand(100)
y = np.random.randint(0,2,size=100)
print (x.shape)

clf = LinearRegression()
clf.fit(x.reshape(-1,1),y)

注意重塑