【读书笔记】scikit-learn Cookbook

本文介绍了数据预处理的重要步骤,包括缺失值处理、噪声数据平滑、数据转换、数据集成和数据归约的方法。此外,还详细讲解了几种常用的数据规范化技术,如最小-最大规范化、z分数规范化和小数定标规范化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


  2.2.1:中心趋势度量
   中位数(median)、均值(mean)、众数(出现频率最高的数)、中列数(数据集最大值和最小值的平均)
   众数:单峰的、双峰、多峰的。。
   对于适度倾斜的单峰数值,有下面经验关系:
   mean-mode=3*(mean-median)
   2.2.2: 度量数据散布
   极差(max()-min())、四分位数四分位数极差(IQR=Q3-Q1)、方差和标准差

   3.1.2 数据预处理的主要任务
    3.2 数据清理:
     3.2.1 缺失值
      1)忽略元组 2)人工填写缺失值 3)使用一全局常量填充 4)使用属性的中心度量(均值或中位数) 6)使用最有可能的值填充(用回归、决策树确定)
      3.2.2 噪声数据    
      分箱:通过考察数据的“近邻”来光滑有序数据值。
            用箱中值、箱中位数、箱边界光滑。
      回归
      3.2.3 偏差检测和数据转换
      字段过载:开发者将新属性的定义挤进已定义的属性的未使用的位部分(例如,使用了一个属性未使用的位,该属性取值已使用了32位中的31位
    3.3 数据集成:
       标称数据的卡方相关检验。
       数值数据的协方差。 E(A)=A(均值),E(B)=B(均值)。cov(A,B)=E(A*B)-A(均值)B(均值)
    数据归约:得到的数据简化表示,它小得多,但能得到同样的分析结果。(归约策略包括维归约数值归约
        维归约 小波变换主成分分析(PCA)---把原数据投影到较小的空间
        数量归约 :用替代的较小的数据表示形式替换原数据。
        数据压缩:有损和无损。
       聚类:簇的“质量”可以用直径表示,直径是簇中两个对象的最大距离。
             形心距离,定义簇中每个对象到簇形心的平均距离。

       数据变换:数据规范化、离散化、概念分层
 2、2016/5/19
    3.5.2 数据规范化:
         最小-最大规范化
         z分数规范化
         小数定标规范化
 
Numpy基础
7.9 金融函数
NumPy 中有很多金融函数,如下所示。
fv 函数计算所谓的终值( future value ),即基于一些假设给出的某个金融资产在未来某一时间点的价值。
     ---终值决定于 4 个参数——利率、期数、每期支付金额以及现值   print "Future value", np.fv(0.03/4, 5 * 4, -10, -1000)
pv 函数计算现值( present value ),即金融资产当前的价值。
npv 函数返回的是净现值( net present value ),即按折现率计算的净现金流之和。
pmt 函数根据本金和利率计算每期需支付的金额。   print "Payment", np.pmt(0.10/12, 12 * 30, 1000000)   Payment -8775.71570089
irr 函数计算内部收益率( internal rate of return )。内部收益率是净现值为 0 时的有效利率,不考虑通胀因素。
mirr 函数计算修正后内部收益率( modified internal rate of return ),是内部收益率的改进版本。
nper 函数计算定期付款的期数。
rate 函数计算利率( rate of interest )。  

scilearn
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/datasets/base.py .
In [13]: datasets.get_data_home()
Out[13]: 'C:\\Users\\kfzx-yingw\\scikit_learn_data'
reg_data = d.make_regression() 默认创建了100*100的数据集
complex_reg_data = d.make_regression(1000, 10, 5, 2, 1.0)   generate a 1000 x 10 matrix with five features responsible for the target creation, an underlying bias factor of 1.0, and 2 targets,

Classification datasets
>>> classification_set = d.make_classification(weights=[0.1])
>>> np.bincount(classification_set[1])
array([10, 90])

Clusters will also be covered
blobs = d.make_blobs()

mean、std、scale、StandardScaler、transform、MinMaxScaler、normalize、fit_transform
>>> new_target = preprocessing.binarize(boston.target, threshold=boston.target.mean())
>>> new_target[:5]
array([ 1., 0., 1., 1., 1.])

验证:>>> (boston.target[:5]> boston.target.mean()).astype(int)
array([1, 0, 1, 1, 1])

方差是用来衡量样本分散程度的。如果样本全部相等,那么方差为0。方差越小,表示样本越集中,反正则样本越分散。
协方差表示两个变量的总体的变化趋势。如果两个变量的变化趋势一致,也就是说如果其中一个大于自身的期望值,另外一个也大于自身的期望值,那么两个变量之间的协方差就是正值。 如果两个变量的变化趋势相反,即其中一个大于自身的期望值,另外一个却小于自身的期望值,那么两个变量之间的协方差就是负值。如果两个变量不相关,则协方差为0,变量线性无关不表示一定没有其他相关性。

scikit-learn提供了一些方法来使线性回归模型正则化。其中之一是岭回归(Ridge Regression,RR,也叫Tikhonov regularization),通过放弃最小二乘法的无偏性,以损失部分信息、降低精度为代价获得回归系数更为符合实际、更可靠的回归方法。岭回归增加L2范数项(相关系数向量平方和的平方根)来调整成本函数(残差平方和):

scikit-learn也提供了最小收缩和选择算子(Least absolute shrinkage and selection operator,LASSO),增加L1范数项(相关系数向量平方和的平方根)来调整成本函数(残差平方和):LASSO方法会让其中一个变量的相关系数会变成0,而岭回归是将两个系数同时缩小。

scikit-learn还提供了弹性网(elastic net)正则化方法,通过线性组合L1和L2兼具LASSO和岭回归的内容。可以认为这两种方法是弹性网正则化的特例。
About This Book ============================== Learn how to handle a variety of tasks with Scikit-Learn with interesting recipes that show you how the library really works Use Scikit-Learn to simplify the programming side data so you can focus on thinking Discover how to apply algorithms in a variety of situations Who This Book Is For ============================== If you're a data scientist already familiar with Python but not Scikit-Learn, or are familiar with other programming languages like R and want to take the plunge with the gold standard of Python machine learning libraries, then this is the book for you. In Detail ============================== Python is quickly becoming the go-to language for analysts and data scientists due to its simplicity and flexibility, and within the Python data space, scikit-learn is the unequivocal choice for machine learning. Its consistent API and plethora of features help solve any machine learning problem it comes across. The book starts by walking through different methods to prepare your data—be it a dataset with missing values or text columns that require the categories to be turned into indicator variables. After the data is ready, you'll learn different techniques aligned with different objectives—be it a dataset with known outcomes such as sales by state, or more complicated problems such as clustering similar customers. Finally, you'll learn how to polish your algorithm to ensure that it's both accurate and resilient to new datasets.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值