简体   繁体   English

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

[英]How to predict based off one one value in sklearn python

Is it possible to train data and predict based on just an x value? 是否可以仅基于x值训练数据并进行预测?

On my chart, I have the point (35,20) in black. 在我的图表上,我的点(35,20)为黑色。 This value when predicted with 35, should return 0, but a point like 15 - with most data points above the black line - should return 1 用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)

Then here's my Linear Regression model: 这是我的线性回归模型:

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)

It looks like you're attempting a linear regression. 您似乎正在尝试线性回归。 The relevant documentation is here . 相关文档在这里

You can predict based on only the x values, but you need the y values to train (else how do you know what to predict?). 您可以仅基于x值进行预测,但是需要y值进行训练(否则您怎么知道要预测什么?)。

From sklearn : 从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]]))

A complete example 一个完整的例子

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)

Notice the reshape 注意重塑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM