![](/img/trans.png)
[英]Different output while using fit_transform vs fit and transform from sklearn
[英]The output from the .fit_transform method
我想对scikit Learn的PolynomialFeatures类中的.fit_transform()方法正在输出的内容有更深入的了解。
我知道该方法在做两件事:1)通过使数据适合回归算法来生成数据模型,以及2)根据1中找到的模型创建新数据。
但是我不理解的是输出。 这是我的代码:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
np.random.seed(0)
n = 15
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
X_train1 = X_train.reshape(11,1)
y_train1 = y_train.reshape(11,1)
def answer_one():
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
poly1 = PolynomialFeatures(degree=1)
X_poly1 = poly1.fit_transform(X_train1)
return X_poly1
answer_one()
我得到的输出是:
array([[ 1. , 10.08877265],
[ 1. , 3.23065446],
[ 1. , 1.62431903],
[ 1. , 9.31004929],
[ 1. , 7.17166586],
[ 1. , 4.96972856],
[ 1. , 8.14799756],
[ 1. , 2.59103578],
[ 1. , 0.35281047],
[ 1. , 3.375973 ],
[ 1. , 8.72363612]])
我假设每个迷你数组中的每个第二个数字都是模型计算出的值,但我不明白每个1是什么?
从PolynomialFeatures
文档中:
生成由度小于或等于指定度的特征的所有多项式组合组成的新特征矩阵。 例如,如果输入样本是二维且格式为[a,b],则2阶多项式特征为[1,a,b,a ^ 2,ab,b ^ 2]。
在您的情况下,输出是度数小于或等于1的列x
所有组合: [1, x]
。 在第一栏中,您有x**0
,在第二栏中,您有x**1
您稍微误解了PolynomialFeatures
。 这个想法根本不适合模型,而只是通过将现有特征相乘来创建新特征。 如果输入样本为二维且格式为[a, b]
,则2中的多项式特征为[1, a, b, a^2, ab, b^2]
。
因此,您在示例中看到的只是偏差和输入。 如果在模型中设置“ include_bias = False”,则模型将消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.