Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python预测正弦波_Python_Machine Learning_Regression_Trigonometry - Fatal编程技术网

用python预测正弦波

用python预测正弦波,python,machine-learning,regression,trigonometry,Python,Machine Learning,Regression,Trigonometry,我正试图用Python编写一个算法来预测正弦波的输出。例如,如果输入为90(以度为单位),则输出为1 当我尝试线性回归时,结果非常糟糕 [in] import pandas as pd from sklearn.linear_model import LinearRegression dic = [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360] dc = [0, 0.5, 0.866, 1, .866, 0.5, 0, -

我正试图用Python编写一个算法来预测正弦波的输出。例如,如果输入为90(以度为单位),则输出为1

当我尝试线性回归时,结果非常糟糕

[in]
import pandas as pd
from sklearn.linear_model import LinearRegression

dic = [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360]
dc = [0, 0.5, 0.866, 1, .866, 0.5, 0, -0.5, -0.866, -1, -0.866, -0.5, 0]
test = [1, 10, 100]

df = pd.DataFrame(dic)
dfy = pd.DataFrame(dc)
test = pd.DataFrame(test)

clf = LinearRegression()
clf.fit(df, dfy)

[out]
[[0.7340967 ]
[0.69718681]
[0.32808791]]

物流根本不适合,因为它是用于分类的。什么方法更适合这个问题?

这是一个使用数据和正弦函数的图形非线性拟合工具。numpy正弦函数使用弧度,因此此处使用的正弦函数会重新缩放输入。我通过查看数据的散点图来猜测初始参数估计值,从接近0.0的RMSE和接近1.0的R平方来看,数据似乎没有噪声成分


这是一个使用数据和正弦函数的图形非线性装配工。numpy正弦函数使用弧度,因此此处使用的正弦函数会重新缩放输入。我通过查看数据的散点图来猜测初始参数估计值,从接近0.0的RMSE和接近1.0的R平方来看,数据似乎没有噪声成分


sine
不是线性曲线,因此在其上拟合线性模型肯定会产生奇怪的结果。你试过非线性模型吗?你一定在寻找。
sine
不是线性曲线,因此在它上面拟合线性模型肯定会产生奇怪的结果。你试过非线性模型吗?你一定在找。哎哟!非线性!谢谢!我不知道为什么我没想到!你就是那个人!哎哟非线性!谢谢!我不知道为什么我没想到!你就是那个人!
import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit


dic = [0.0, 30.0, 60.0, 90.0, 120.0, 150.0, 180.0, 210.0, 240.0, 270.0, 300.0, 330.0, 360.0]
dc = [0.0, 0.5, 0.866, 1.0, 0.866, 0.5, 0.0, -0.5, -0.866, -1.0, -0.866, -0.5, 0.0]

# rename data to match previous example code
xData = dic
yData = dc


def func(x, amplitude, center, width):
    return amplitude * numpy.sin(numpy.pi * (x - center) / width)


# these are estimated from a scatterplot of the data
initialParameters = numpy.array([-1.0, 180.0, 180.0])

# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)

modelPredictions = func(xData, *fittedParameters) 

absError = modelPredictions - yData

SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))

print('Parameters:', fittedParameters)
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

print()


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    # first the raw data as a scatter plot
    axes.plot(xData, yData,  'D')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = func(xModel, *fittedParameters)

    # now the model as a line plot
    axes.plot(xModel, yModel)

    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)