下面是我的代码:
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
X_arr = []
Y_arr = []
with open('input.txt') as fp:
for line in fp:
b = line.split("|")
x,y = b
X_arr.append(int(x))
Y_arr.append(int(y))
X=np.array([X_arr]).T
print(X)
y=np.array(Y_arr)
print(y)
model = make_pipeline(PolynomialFeatures(degree=2),
LinearRegression(fit_intercept = False))
model.fit(X,y)
X_predict = np.array([[3]])
print(model.predict(X_predict))请告诉我,我有一个问题:
model = make_pipeline(PolynomialFeatures(degree=2), 请问如何选择此值(2、3、4等)?有没有动态设置这个值的方法?
例如,我有这个测试文件:
1 1
2 4
4 16
5 75对于前三行,模型是
y=a*x*x+b*x + c (b=c=0)对于最后一行,模型是:
y=a*x*x*x+b*x+c (b=c=0) 发布于 2018-08-08 00:18:06
这绝对不是解决问题的简单方法,但我想我明白你想要什么,也许:
import math
epsilon = 1e-2
# Do your error checking on size of array
...
# Warning: This only works for positive x, negative logarithm is not proper.
# If you really want to, do an `abs(X_arr[0])` and check the degree is even.
deg = math.log(Y_arr[0], X_arr[0])
assert deg % 1 < epsilon
for x, y in zip(X_arr[1:], Y_arr[1:]):
if x == y == 1: continue # All x^n fits this and will cause divide by 0
assert abs(math.log(y, x) - deg) < epsilon
...
PolynomialFeature(degree=int(deg))这将检查度数是否为整数值,以及所有其他数据点是否符合相同的多项式。
,如果你有一堆数据点为(1,1),你就无法决定实际的学位是多少。在没有对数据进行任何假设的情况下,无法确定多项式x^n的次数。
这只是你如何实现这样一个启发式的例子,请不要在生产中使用它。
https://stackoverflow.com/questions/51730559
复制相似问题