如何在sklearnpython中基于一个值进行预测

2024-10-04 07:33:51 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有可能仅仅基于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)


Tags: infordatalabelsifnprangesg
2条回答

一个完整的例子

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)

注意重塑

看起来你在尝试线性回归。相关文档是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]]))

相关问题 更多 >