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 异步编程 asyncio简介及基本用法

《Python异步编程asyncio简介及基本用法》asyncio是Python的一个库,用于编写并发代码,使用协程、任务和Futures来处理I/O密集型和高延迟操作,本文给大家介绍Python... 目录1、asyncio是什么IO密集型任务特征2、怎么用1、基本用法2、关键字 async1、async

Python实现剪贴板历史管理器

《Python实现剪贴板历史管理器》在日常工作和编程中,剪贴板是我们使用最频繁的功能之一,本文将介绍如何使用Python和PyQt5开发一个功能强大的剪贴板历史管理器,感兴趣的可以了解下... 目录一、概述:为什么需要剪贴板历史管理二、功能特性全解析2.1 核心功能2.2 增强功能三、效果展示3.1 主界面

Python与Java交互出现乱码的问题解决

《Python与Java交互出现乱码的问题解决》在现代软件开发中,跨语言系统的集成已经成为日常工作的一部分,特别是当Python和Java之间进行交互时,编码问题往往会成为导致数据传输错误、乱码以及难... 目录背景:为什么会出现乱码问题产生的场景解决方案:确保统一的UTF-8编码完整代码示例总结在现代软件

Linux区分SSD和机械硬盘的方法总结

《Linux区分SSD和机械硬盘的方法总结》在Linux系统管理中,了解存储设备的类型和特性是至关重要的,不同的存储介质(如固态硬盘SSD和机械硬盘HDD)在性能、可靠性和适用场景上有着显著差异,本文... 目录一、lsblk 命令简介基本用法二、识别磁盘类型的关键参数:ROTA查询 ROTA 参数ROTA

Python+Tkinter实现Windows Hosts文件编辑管理工具

《Python+Tkinter实现WindowsHosts文件编辑管理工具》在日常开发和网络调试或科学上网场景中,Hosts文件修改是每个开发者都绕不开的必修课,本文将完整解析一个基于Python... 目录一、前言:为什么我们需要专业的Hosts管理工具二、工具核心功能全景图2.1 基础功能模块2.2 进

Python多重继承慎用的地方

《Python多重继承慎用的地方》多重继承也可能导致一些问题,本文主要介绍了Python多重继承慎用的地方,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录前言多重继承要慎用Mixin模式最后前言在python中,多重继承是一种强大的功能,它允许一个

python+OpenCV反投影图像的实现示例详解

《python+OpenCV反投影图像的实现示例详解》:本文主要介绍python+OpenCV反投影图像的实现示例详解,本文通过实例代码图文并茂的形式给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前言二、什么是反投影图像三、反投影图像的概念四、反向投影的工作原理一、利用反向投影backproj

Python中edge-tts实现便捷语音合成

《Python中edge-tts实现便捷语音合成》edge-tts是一个功能强大的Python库,支持多种语言和声音选项,本文主要介绍了Python中edge-tts实现便捷语音合成,具有一定的参考价... 目录安装与环境设置文本转语音查找音色更改语音参数生成音频与字幕总结edge-tts 是一个功能强大的

使用Python和PaddleOCR实现图文识别的代码和步骤

《使用Python和PaddleOCR实现图文识别的代码和步骤》在当今数字化时代,图文识别技术的应用越来越广泛,如文档数字化、信息提取等,PaddleOCR是百度开源的一款强大的OCR工具包,它集成了... 目录一、引言二、环境准备2.1 安装 python2.2 安装 PaddlePaddle2.3 安装

Python+PyQt5开发一个Windows电脑启动项管理神器

《Python+PyQt5开发一个Windows电脑启动项管理神器》:本文主要介绍如何使用PyQt5开发一款颜值与功能并存的Windows启动项管理工具,不仅能查看/删除现有启动项,还能智能添加新... 目录开篇:为什么我们需要启动项管理工具功能全景图核心技术解析1. Windows注册表操作2. 启动文件