pca

使用sklearn做特征工程

你离开我真会死。 提交于 2020-01-15 16:55:43
1 特征工程是什么?   有这么一句话在业界广泛流传:数据和特征决定了机器学习的上限,而模型和算法只是逼近这个上限而已。那特征工程到底是什么呢?顾名思义,其本质是一项工程活动,目的是最大限度地从原始数据中提取特征以供算法和模型使用。通过总结和归纳,人们认为特征工程包括以下方面:   特征处理是特征工程的核心部分,sklearn提供了较为完整的特征处理方法,包括数据预处理,特征选择,降维等。首次接触到sklearn,通常会被其丰富且方便的算法模型库吸引,但是这里介绍的特征处理库也十分强大!   本文中使用sklearn中的 IRIS(鸢尾花)数据集 来对特征处理功能进行说明。IRIS数据集由Fisher在1936年整理,包含4个特征(Sepal.Length(花萼长度)、Sepal.Width(花萼宽度)、Petal.Length(花瓣长度)、Petal.Width(花瓣宽度)),特征值都为正浮点数,单位为厘米。目标值为鸢尾花的分类(Iris Setosa(山鸢尾)、Iris Versicolour(杂色鸢尾),Iris Virginica(维吉尼亚鸢尾))。导入IRIS数据集的代码如下: 1 from sklearn.datasets import load_iris 2 3 #导入IRIS数据集 4 iris = load_iris() 5 6 #特征矩阵 7 iris.data

机器学习中的特征工程

自作多情 提交于 2020-01-15 14:59:59
作者:城东 链接: 特征工程到底是什么? - 城东的回答 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 目录 1 特征工程是什么? 2 数据预处理   2.1 无量纲化     2.1.1 标准化     2.1.2 区间缩放法     2.1.3 标准化与归一化的区别   2.2 对定量特征二值化   2.3 对定性特征哑编码   2.4 缺失值计算   2.5 数据变换 3 特征选择   3.1 Filter     3.1.1 方差选择法     3.1.2 相关系数法     3.1.3 卡方检验     3.1.4 互信息法   3.2 Wrapper     3.2.1 递归特征消除法   3.3 Embedded     3.3.1 基于惩罚项的特征选择法     3.3.2 基于树模型的特征选择法 4 降维   4.1 主成分分析法(PCA)   4.2 线性判别分析法(LDA) 5 总结 6 参考资料 1 特征工程是什么?   有这么一句话在业界广泛流传:数据和特征决定了机器学习的上限,而模型和算法只是逼近这个上限而已。那特征工程到底是什么呢?顾名思义,其本质是一项工程活动,目的是最大限度地从原始数据中提取特征以供算法和模型使用。通过总结和归纳,人们认为特征工程包括以下方面:   特征处理是特征工程的核心部分

FactoMineR PCA with factor variables

我是研究僧i 提交于 2020-01-15 09:23:28
问题 Here is a link to the data in R data format (so you can see that the factors are actually factors): Vaccination Data 2016 Here is the code: df %>% PCA(scale.unit = TRUE, quali.sup = 14, #the factor variables graph = T) Aside from telling me that it replaced missing values with means (which I was happy to see), the above produces this: argument is not numeric or logical: returning NA argument is not numeric or logical: returning NA argument is not numeric or logical: returning NA argument is

用scikit-learn学习主成分分析(PCA)

本秂侑毒 提交于 2020-01-15 07:44:00
    在 主成分分析(PCA)原理总结 中,我们对主成分分析(以下简称PCA)的原理做了总结,下面我们就总结下如何使用scikit-learn工具来进行PCA降维。 1. scikit-learn PCA类介绍     在scikit-learn中,与PCA相关的类都在sklearn.decomposition包中。最常用的PCA类就是sklearn.decomposition.PCA,我们下面主要也会讲解基于这个类的使用的方法。     除了PCA类以外,最常用的PCA相关类还有KernelPCA类,在原理篇我们也讲到了,它主要用于非线性数据的降维,需要用到核技巧。因此在使用的时候需要选择合适的核函数并对核函数的参数进行调参。     另外一个常用的PCA相关类是IncrementalPCA类,它主要是为了解决单机内存限制的。有时候我们的样本量可能是上百万+,维度可能也是上千,直接去拟合数据可能会让内存爆掉, 此时我们可以用IncrementalPCA类来解决这个问题。IncrementalPCA先将数据分成多个batch,然后对每个batch依次递增调用partial_fit函数,这样一步步的得到最终的样本最优降维。     此外还有SparsePCA和MiniBatchSparsePCA。他们和上面讲到的PCA类的区别主要是使用了L1的正则化

聚类:Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed ob

北战南征 提交于 2020-01-15 00:37:09
最近在用python做数据挖掘,在聚类的时候遇到了一个非常恶心的问题,搜遍全网都没有解决方案。话不多说,直接上代码: from sklearn . cluster import KMeans from sklearn . decomposition import PCA import matplotlib . pyplot as plt #kmeans算法 df1 = df23 kmeans = KMeans ( n_clusters = 5 , random_state = 10 ) . fit ( df1 ) #贴上每个样本对应的簇类别标签 df1 [ 'level' ] = kmeans . labels_ #df1.to_csv('new_df.csv') df2 = df1 . groupby ( 'level' , as_index = False ) [ 'level' ] . agg ( { 'num' : np . size } ) print ( df2 . head ( ) ) #将用于聚类的数据的特征的维度降至2维 pca = PCA ( n_components = 2 ) new_pca = pd . DataFrame ( pca . fit_transform ( df1 ) ) print ( new_pca . head ( ) ) #可视化 d

Feature Extraction for MNIST Images Using PCA

六月ゝ 毕业季﹏ 提交于 2020-01-14 05:53:47
问题 I use Matlab to read the MNIST database. Those images are, originally, 28x28 (=784) pixels. So, I have a 2D 784x1000 array (meaning, I have read 1000 images). Supposing my 2D array's name is IMGS, the Matlab expression: IMGS(:, 1), would give me the first image. In order to perform PCA, so to extract some of the features of the image (from the 784 of them): I transpose the array IMGS, putting the images to rows and features (dimensions) to columns, in an array called IMGS_T (IMGS_T(1, :)

Bug in Scikit-Learn PCA or in Numpy Eigen Decomposition?

梦想的初衷 提交于 2020-01-13 20:39:06
问题 I have a dataset with 400 features. What I did: # approach 1 d_cov = np.cov(d_train.transpose()) eigens, mypca = LA.eig(d_cov) # assume sort by eigen value also/ LA = numpy linear algebra # approach 2 pca = PCA(n_components=300) d_fit = pca.fit_transform(d_train) pc = pca.components_ Now, these two should be the same, right? as PCA is just the eigendecomposition of the covariance matrix. But these are very different in my case? How could that be, I am doing any mistake above? Comparing

Plotting RDA (vegan) in ggplot

寵の児 提交于 2020-01-13 19:18:11
问题 I'm still new to R, trying to learn how to use the library vegan, which I can easily plot in R with the normal plot function. The problem arises when I want to plot the data in ggplot. I know I have to extract the right data from the list I've created, but which and how? The dataset I've been practicing on can be downloaded here https://drive.google.com/file/d/0B1PQGov60aoudVR3dVZBX1VKaHc/view?usp=sharing The code I've been using to get the data transformed is this: library(vegan) library

Using PCA on linear regression

无人久伴 提交于 2020-01-13 14:06:29
问题 I want to use principal component analysis to reduce some noise before applying linear regression. I have 1000 samples and 200 features import numpy as np from sklearn.linear_model import LinearRegression from sklearn.decomposition import PCA X = np.random.rand(1000,200) y = np.random.rand(1000,1) With this data I can train my model: model.fit(X,y) But if I try the same after applying PCA pca = PCA(n_components=8) pca.fit(X) PCA(copy=True, iterated_power='auto', n_components=3, random_state

Using PCA on linear regression

喜夏-厌秋 提交于 2020-01-13 14:05:56
问题 I want to use principal component analysis to reduce some noise before applying linear regression. I have 1000 samples and 200 features import numpy as np from sklearn.linear_model import LinearRegression from sklearn.decomposition import PCA X = np.random.rand(1000,200) y = np.random.rand(1000,1) With this data I can train my model: model.fit(X,y) But if I try the same after applying PCA pca = PCA(n_components=8) pca.fit(X) PCA(copy=True, iterated_power='auto', n_components=3, random_state