在 scikit-learn 中拟合数据与转换数据 [英] Fitting data vs. transforming data in scikit-learn

查看:21
本文介绍了在 scikit-learn 中拟合数据与转换数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scikit-learn 中,所有估算器都有一个 fit() 方法,并且根据它们是有监督的还是无监督的,它们还有一个 predict()transform() 方法.

In scikit-learn, all estimators have a fit() method, and depending on whether they are supervised or unsupervised, they also have a predict() or transform() method.

我正在编写转换器 对于无监督学习任务,想知道是否有经验法则可以放置哪种学习逻辑.官方文档在这方面不是很有帮助:

I am in the process of writing a transformer for an unsupervised learning task and was wondering if there is a rule of thumb where to put which kind of learning logic. The official documentation is not very helpful in this regard:

fit_transform(X, y=None, **fit_params)
适合数据,然后对其进行转换.

fit_transform(X, y=None, **fit_params)
Fit to data, then transform it.

在这种情况下,拟合数据转换数据是什么意思?

In this context, what is meant by both fitting data and transforming data?

推荐答案

Fitting 查找将用于转换数据的模型的内部参数.转换将参数应用于数据.您可以将模型拟合到一组数据,然后在完全不同的一组数据上进行转换.

Fitting finds the internal parameters of a model that will be used to transform data. Transforming applies the parameters to data. You may fit a model to one set of data, and then transform it on a completely different set.

例如,您将线性模型拟合到数据以获得斜率和截距.然后使用这些参数将 x 的新值或现有值转换(即映射)为 y.

For example, you fit a linear model to data to get a slope and intercept. Then you use those parameters to transform (i.e., map) new or existing values of x to y.

fit_transform 只是对相同的数据执行两个步骤.

fit_transform is just doing both steps to the same data.

一个 scikit 示例:您拟合数据以查找主成分.然后转换数据以查看它如何映射到这些组件上:

A scikit example: You fit data to find the principal components. Then you transform your data to see how it maps onto these components:

from sklearn.decomposition import PCA

pca = PCA(n_components=2)

X = [[1,2],[2,4],[1,3]]

pca.fit(X)

# This is the model to map data
pca.components_

array([[ 0.47185791,  0.88167459],
       [-0.88167459,  0.47185791]], dtype=float32)

# Now we actually map the data
pca.transform(X)

array([[-1.03896057, -0.17796634],
       [ 1.19624651, -0.11592512],
       [-0.15728599,  0.29389156]])

# Or we can do both "at once"
pca.fit_transform(X)

array([[-1.03896058, -0.1779664 ],
       [ 1.19624662, -0.11592512],
       [-0.15728603,  0.29389152]], dtype=float32)

这篇关于在 scikit-learn 中拟合数据与转换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆