在sklearn

时间:2018-11-15 23:48:29

标签: python scikit-learn regression non-linear-regression

我有一个多项式特征函数,我想给出一个(1/2)或0.5的度数,因为所使用的数据集是一个最大为(1/2)的度数的向下平稳段,但是产生了预测都相同,并且r平方均值是-23736.436220427375。当度数更改为2并向前时,随着X值超过中点,预测会增加,从而产生抛物线,因为数据不是抛物线。

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures


df = pd.read_csv('infantmortality.csv',sep=',')
x = df['Year']
y = df['Infant Mortality Rate']

x_Train = np.array(x[96:150]).reshape(-1, 1)
y_Train = y[96:150]
x_Test = np.array(x[150:193]).reshape(-1, 1)
y_Test = x[150:193]

poly = PolynomialFeatures(degree=int(2))
X_ = poly.fit_transform(x_Train)
x_test_ = poly.fit_transform(x_Test)

lg = LinearRegression(fit_intercept=False, normalize=True)
lg.fit(X_, y_Train)

score = lg.score(x_test_, y_Test)
val = [2005]
val_ = poly.fit_transform(np.array(val).reshape(-1, 1))
print lg.predict(val_)

0 个答案:

没有答案