python skikit-learn库总结

2024-06-23 09:48
文章标签 python 总结 learn skikit

本文主要是介绍python skikit-learn库总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、简介

scikit-learn是一个建立在Scipy基础上的用于机器学习的python模块,而在不同的领域中已经发展出为数众多的基于Scipy的工具包,它们被统一称为Scikits,而在所有的分支版本中,scikit-learn是最有名的。它是开源的,任何人都可以免费地使用它或者进行二次发行。

scikit-learn包含众多定级机器学习算法,它主要有6大类的基本功能,分别是分类,回归,聚类,数据降维,模型选择和数据预处理。

机器学习官方API链接

sklearn dataset 模块学习

2、重点函数讲解

sklearn.datasets.make_blobs(n_samples=100n_features=2centers=Nonecluster_std=1.0center_box=(-10.010.0)shuffle=Truerandom_state=None)[source]   此函数常用来生成测试数据集

Generate isotropic Gaussian blobs for clustering.

Read more in the User Guide.

Parameters:

n_samples : int or array-like, optional (default=100)

If int, it is the total number of points equally divided among clusters. If array-like, each element of the sequence indicates the number of samples per cluster.

n_features : int, optional (default=2)

The number of features for each sample.代表每个物体的特性数,可以决定输出X中的列数

centers : int or array of shape [n_centers, n_features], optional

(default=None) The number of centers to generate, or the fixed center locations. If n_samples is an int and centers is None, 3 centers are generated. If n_samples is array-like, centers must be either None or an array of length equal to the length of n_samples.表示生成数据在图上绘制出几个集合

cluster_std : float or sequence of floats, optional (default=1.0)

The standard deviation of the clusters.生成数据的标准差大小,标准差越大,则数据点越离散,否则则相反,默认给标准差大小为1,与默认给的center_box的比较合适,如果想调整这个大小,则与之相对应的center_box大小成成正比调整,到时绘制的点离散度比较合适,不然就会造成生成数据的点过于离散或者过于聚合

center_box : pair of floats (min, max), optional (default=(-10.0, 10.0))

The bounding box for each cluster center when centers are generated at random.调整生成数据的边界值

shuffle : boolean, optional (default=True)

Shuffle the samples.相当于打乱顺序

random_state : int, RandomState instance or None (default)

Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.设置生成数据的随机值,如果想控制每次产生的数据值是一样的,则使用这个参数传递一个合适的随机值,可以保证每次生成的数据值都一样,有利于重复试验;如果不传递随机值,则每次生成的数据则不一样;其余的函数传递的随机值含义也一样

Returns:

X : array of shape [n_samples, n_features]

The generated samples.生成数据的shape为(n_sample,centers)

y : array of shape [n_samples]

The integer labels for cluster membership of each sample.如果选择的特性数n,则生成数据值由0到n-1一维数组组成

# 使用示例
X, y = make_blobs(n_samples=100, n_features=2, centers=2, random_state=0, cluster_std=1.0)

 sklearn.model_selection.train_test_split(*arrays**options)[source]   交叉生成训练数据集和测试数据集的函数

Split arrays or matrices into random train and test subsets

Quick utility that wraps input validation and next(ShuffleSplit().split(X, y)) and application to input data into a single call for splitting (and optionally subsampling) data in a oneliner.

Read more in the User Guide.

Parameters:

*arrays : sequence of indexables with same length / shape[0]  

Allowed inputs are lists, numpy arrays, scipy-sparse matrices or pandas dataframes.  传入生成数据集,X,y

test_size : float, int or None, optional (default=0.25)  现在推荐使用test_size而不是train_size;指定划分数据集中测试数据集所占的比率

If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. If None, the value is set to the complement of the train size. By default, the value is set to 0.25. The default will change in version 0.21. It will remain 0.25 only if train_size is unspecified, otherwise it will complement the specified train_size.

train_size : float, int, or None, (default=None) 指定划分训练数据集的比率,与test_size可以同时使用,但是同时使用的比较少

If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If int, represents the absolute number of train samples. If None, the value is automatically set to the complement of the test size.

random_state : int, RandomState instance or None, optional (default=None)   指定随机划分时的随机种子,如果想要划分的数据集每次都一样的话,就指定一个随机值参数

If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.

shuffle : boolean, optional (default=True)  打乱数据集

Whether or not to shuffle the data before splitting. If shuffle=False then stratify must be None.

stratify : array-like or None (default=None) 一般传递y数组值,按照y中各类数据的比例分配给train和test

If not None, data is split in a stratified fashion, using this as the class labels.

Returns:

splitting : list, length=2 * len(arrays)

List containing train-test split of inputs.

New in version 0.16: If the input is sparse, the output will be a scipy.sparse.csr_matrix. Else, output type is the same as the input type.

#使用示例: 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=10, stratify=y)

sklearn.datasets.make_regression(n_samples=100n_features=100n_informative=10n_targets=1bias=0.0effective_rank=Nonetail_strength=0.5noise=0.0shuffle=Truecoef=Falserandom_state=None)[source]  生成回归预测数据集

Generate a random regression problem.

The input set can either be well conditioned (by default) or have a low rank-fat tail singular profile. See make_low_rank_matrix for more details.

The output is generated by applying a (potentially biased) random linear regression model with n_informative nonzero regressors to the previously generated input and some gaussian centered noise with some adjustable scale.

Read more in the User Guide.

Parameters:

n_samples : int, optional (default=100)  生成数据的个数

The number of samples.

n_features : int, optional (default=100)  生成数据的特性数

The number of features.

n_informative : int, optional (default=10)  生成数据参与建模的特性个数

The number of informative features, i.e., the number of features used to build the linear model used to generate the output.

n_targets : int, optional (default=1)  目标因变量的个数

The number of regression targets, i.e., the dimension of the y output vector associated with a sample. By default, the output is a scalar.

bias : float, optional (default=0.0)  偏差(截距)

The bias term in the underlying linear model.

effective_rank : int or None, optional (default=None)

if not None:

The approximate number of singular vectors required to explain most of the input data by linear combinations. Using this kind of singular spectrum in the input allows the generator to reproduce the correlations often observed in practice.

if None:

The input set is well conditioned, centered and gaussian with unit variance.

tail_strength : float between 0.0 and 1.0, optional (default=0.5)

The relative importance of the fat noisy tail of the singular values profile if effective_rank is not None.

noise : float, optional (default=0.0) 噪音值,也就是标准差

The standard deviation of the gaussian noise applied to the output.

shuffle : boolean, optional (default=True)

Shuffle the samples and the features.

coef : boolean, optional (default=False)  是否输出coef标识,默认不输出

If True, the coefficients of the underlying linear model are returned.

random_state : int, RandomState instance or None (default)

Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.

Returns:

X : array of shape [n_samples, n_features]

The input samples.

y : array of shape [n_samples] or [n_samples, n_targets]

The output values.

coef : array of shape [n_features] or [n_features, n_targets], optional

The coefficient of the underlying linear model. It is returned only if coef is True.

3、函数使用简要说明

sklearn相关函数
函数使用说明
sklearn.neighbors.KNeighborsClassifier(n_neighbors=5, weights='uniform', algorithm='auto')n_neighbors调节临近点的个数,一般调整预测值需要用这个参数;weights调整权重,uniform表示初始权重全部一样的;algorithm更换训练算法,auto表示尝试选择一个最佳的算法进行预测
x_train, x_test, y_train, y_test = sklearn.cross_validation.train_test_split(x, y, test_size = 0.2,random_state=0)将原始数据划分成训练数据集合测试数据集,根据test_size参数调整测试数据集合训练数据集的数据各占用总数据的比率
  
  
  
  
  
  
  
  
  
  
  
  

 

 

这篇关于python skikit-learn库总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1086813

相关文章

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧