当前位置: 首页 > news >正文

东莞专业网站设计制作公司百度秒收录软件

东莞专业网站设计制作公司,百度秒收录软件,承德做网站,普洱高端网站建设价格目录 一、用法精讲 52、pandas.from_dummies函数 52-1、语法 52-2、参数 52-3、功能 52-4、返回值 52-5、说明 52-6、用法 52-6-1、数据准备 52-6-2、代码示例 52-6-3、结果输出 53、pandas.factorize函数 53-1、语法 53-2、参数 53-3、功能 53-4、返回值 53-…

目录

一、用法精讲

52、pandas.from_dummies函数

52-1、语法

52-2、参数

52-3、功能

52-4、返回值

52-5、说明

52-6、用法

52-6-1、数据准备

52-6-2、代码示例

52-6-3、结果输出

53、pandas.factorize函数

53-1、语法

53-2、参数

53-3、功能

53-4、返回值

53-5、说明

53-6、用法

53-6-1、数据准备

53-6-2、代码示例

53-6-3、结果输出 

54、pandas.unique函数

54-1、语法

54-2、参数

54-3、功能

54-4、返回值

54-5、说明

54-6、用法

54-6-1、数据准备

54-6-2、代码示例

54-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

52、pandas.from_dummies函数
52-1、语法
# 52、pandas.from_dummies函数
pandas.from_dummies(data, sep=None, default_category=None)
Create a categorical DataFrame from a DataFrame of dummy variables.Inverts the operation performed by get_dummies().New in version 1.5.0.Parameters:
data
DataFrame
Data which contains dummy-coded variables in form of integer columns of 1’s and 0’s.sep
str, default None
Separator used in the column names of the dummy categories they are character indicating the separation of the categorical names from the prefixes. For example, if your column names are ‘prefix_A’ and ‘prefix_B’, you can strip the underscore by specifying sep=’_’.default_category
None, Hashable or dict of Hashables, default None
The default category is the implied category when a value has none of the listed categories specified with a one, i.e. if all dummies in a row are zero. Can be a single value for all variables or a dict directly mapping the default categories to a prefix of a variable.Returns:
DataFrame
Categorical data decoded from the dummy input-data.Raises:
ValueError
When the input DataFrame data contains NA values.When the input DataFrame data contains column names with separators that do not match the separator specified with sep.When a dict passed to default_category does not include an implied category for each prefix.When a value in data has more than one category assigned to it.When default_category=None and a value in data has no category assigned to it.TypeError
When the input data is not of type DataFrame.When the input DataFrame data contains non-dummy data.When the passed sep is of a wrong data type.When the passed default_category is of a wrong data type.
52-2、参数

52-2-1、data(必须)一个DataFrame对象,包含了哑变量(0和1)的列,每列通常表示一个类别的存在或缺失。

52-2-2、sep(可选,默认值为None)用于分隔哑变量列名中类别信息的分隔符。例如,如果列名是A_cat和A_dog,并且用下划线分隔,那么sep应该设置为 _

52-2-3、default_category(可选,默认值为None)在原始数据中,可能有一些类别在哑变量中缺失,这个参数允许指定一个默认类别,以便在缺失的情况下使用。

52-3、功能

        接受一个包含哑变量的DataFrame,并将其转换回表示原始类别的DataFrame,哑变量通常是通过对分类变量进行独热编码(one-hot encoding)生成的。

52-4、返回值

        返回值是一个DataFrame,其中包含了原始的分类数据,这些数据是根据哑变量的值重构的,即每行数据中值为1的哑变量列对应的列名(去掉分隔符和前缀)即为原始分类变量的值。

52-5、说明

        该函数非常有用,特别是在对数据进行独热编码之后希望恢复原始分类变量的情况下,它简化了数据预处理模型结果解释的过程。

52-6、用法
52-6-1、数据准备
52-6-2、代码示例
# 52、pandas.from_dummies函数
import pandas as pd
# 示例哑变量DataFrame
data = pd.DataFrame({'color_red': [1, 0, 0],'color_blue': [0, 1, 0],'color_green': [0, 0, 1]
})
# 使用pandas.from_dummies将哑变量转换回原始分类数据
original_data = pd.from_dummies(data, sep='_')
print(original_data)
52-6-3、结果输出
# 52、pandas.from_dummies函数
#    color
# 0    red
# 1   blue
# 2  green
53、pandas.factorize函数
53-1、语法
# 53、pandas.factorize函数
pandas.factorize(values, sort=False, use_na_sentinel=True, size_hint=None)
Encode the object as an enumerated type or categorical variable.This method is useful for obtaining a numeric representation of an array when all that matters is identifying distinct values. factorize is available as both a top-level function pandas.factorize(), and as a method Series.factorize() and Index.factorize().Parameters:
valuessequence
A 1-D sequence. Sequences that aren’t pandas objects are coerced to ndarrays before factorization.sortbool, default False
Sort uniques and shuffle codes to maintain the relationship.use_na_sentinelbool, default True
If True, the sentinel -1 will be used for NaN values. If False, NaN values will be encoded as non-negative integers and will not drop the NaN from the uniques of the values.New in version 1.5.0.size_hintint, optional
Hint to the hashtable sizer.Returns:
codesndarray
An integer ndarray that’s an indexer into uniques. uniques.take(codes) will have the same values as values.uniquesndarray, Index, or Categorical
The unique valid values. When values is Categorical, uniques is a Categorical. When values is some other pandas object, an Index is returned. Otherwise, a 1-D ndarray is returned.NoteEven if there’s a missing value in values, uniques will not contain an entry for it.
53-2、参数

53-2-1、values(必须)需要编码的数组或序列,可以是列表、NumPy数组、Pandas系列等。

53-2-2、sort(可选,默认值为False)是否对唯一值数组进行排序,如果为True,返回的唯一值数组将按字典顺序排序。

53-2-3、use_na_sentinel(可选,默认值为True)是否在输出整数数组中使用-1作为NaN的标记,如果为False,则将NaN也编码为一个唯一的整数。

53-2-4、size_hint(可选,默认值为None)一个整数提示,用于内部优化,指示预期的唯一值的数量,这可以提高大规模数据的处理性能。

53-3、功能

        将一组值(如列表、数组或序列)编码为整数索引。具体来说,它会为每个唯一值分配一个唯一的整数,并返回这些整数索引以及唯一值的数组。

53-4、返回值

        返回两个对象:

53-4-1、整数索引数组:一个与输入数组大小相同的整数数组,其中每个整数对应于输入数组中的一个值。

53-4-2、唯一值数组:一个包含输入数组中所有唯一值的数组,按它们首次出现的顺序排列,除非使用sort=True参数。

53-5、说明

        pandas.factorize的主要功能是:

53-5-1、将类别数据转换为整数索引:通过为每个唯一值分配一个整数,使得类别数据可以用于数值计算或进一步分析。

53-5-2、处理缺失值:可以选择是否将缺失值(NaN)编码为一个特定的整数(默认为-1)。

53-6、用法
53-6-1、数据准备
53-6-2、代码示例
# 53、pandas.factorize函数
# 53-1、基本用法
import pandas as pd
values = ['a', 'b', 'a', 'c', 'b']
factorized_values, unique_values = pd.factorize(values)
print(factorized_values)
print(unique_values)# 53-2、使用sort参数
import pandas as pd
values = ['a', 'b', 'a', 'c', 'b']
factorized_values, unique_values = pd.factorize(values, sort=True)
print(factorized_values)
print(unique_values)# 53-3、处理NaN
import pandas as pd
values_with_nan = ['a', 'b', None, 'a', 'c']
factorized_values, unique_values = pd.factorize(values_with_nan, use_na_sentinel=True)
print(factorized_values)
print(unique_values)# 53-4、使用size_hint参数
import pandas as pd
large_values = ['a'] * 100000 + ['b'] * 100000
factorized_values, unique_values = pd.factorize(large_values, size_hint=2)
print(factorized_values[:10])
print(unique_values)
53-6-3、结果输出 
# 53、pandas.factorize函数
# 53-1、基本用法
# [0 1 0 2 1]
# ['a' 'b' 'c']# 53-2、使用sort参数
# [0 1 0 2 1]
# ['a' 'b' 'c']# 53-3、处理NaN
# [ 0  1 -1  0  2]
# ['a' 'b' 'c']# 53-4、使用size_hint参数
# [0 0 0 0 0 0 0 0 0 0]
# ['a' 'b']
54、pandas.unique函数
54-1、语法
# 54、pandas.unique函数
pandas.unique(values)
Return unique values based on a hash table.Uniques are returned in order of appearance. This does NOT sort.Significantly faster than numpy.unique for long enough sequences. Includes NA values.Parameters:
values
1d array-like
Returns:
numpy.ndarray or ExtensionArray
The return can be:Index : when the input is an IndexCategorical : when the input is a Categorical dtypendarray : when the input is a Series/ndarrayReturn numpy.ndarray or ExtensionArray.
54-2、参数

54-2-1、values(必须):可以是以下几种类型的对象:

  • 一维的pandas.Series
  • 一维的pandas.Index
  • 一维的numpy.ndarray
  • 一维的列表或序列
54-3、功能

        返回输入数组中的唯一值,按它们在数组中首次出现的顺序排列。

54-4、返回值

        返回一个numpy.ndarray,其中包含输入数组中的唯一值。

54-5、说明

        pandas.unique是一个简单且高效的函数,用于从一维数组、Series或Index中提取唯一值,它只需要一个参数,即要处理的数组,并返回一个包含唯一值的numpy.ndarray,这种功能在数据预处理清理阶段非常有用,可以帮助识别数据集中的独特元素。

54-6、用法
54-6-1、数据准备
54-6-2、代码示例
# 54、pandas.unique函数
# 54-1、处理列表
import pandas as pd
values = [1, 2, 2, 3, 4, 4, 4, 5]
unique_values = pd.unique(values)
print(unique_values)# 54-2、处理pandas Series
import pandas as pd
series = pd.Series(['a', 'b', 'a', 'c', 'b', 'd'])
unique_values = pd.unique(series)
print(unique_values)# 54-3、处理numpy数组
import pandas as pd
import numpy as np
array = np.array([1, 2, 3, 1, 2, 3, 4])
unique_values = pd.unique(array)
print(unique_values)# 54-4、处理含有NaN的数据
import pandas as pd
values_with_nan = [1, 2, np.nan, 2, np.nan, 3]
unique_values = pd.unique(values_with_nan)
print(unique_values)
54-6-3、结果输出
# 54、pandas.unique函数
# 54-1、处理列表
# [1 2 3 4 5]# 54-2、处理pandas Series
# ['a' 'b' 'c' 'd']# 54-3、处理numpy数组
# [1 2 3 4]# 54-4、处理含有NaN的数据
# [ 1.  2. nan  3.]

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页
http://www.hkea.cn/news/663331/

相关文章:

  • poedit pro wordpress免费网站推广优化
  • 市场营销产品推广策划方案seo合作代理
  • 东莞专业网站建设推广搜索引擎网络排名
  • 服务器做网站用什么环境好销售营销方案100例
  • 如何做DJ网站英文seo外链
  • 网站统计源码下载百度推广的步骤
  • 本地网站建设seo推广的方法
  • 东莞好的网站建设效果seo和sem分别是什么
  • 最新版wordpress背景手机网络优化软件
  • 丛台企业做网站推广免费建一级域名网站
  • 集宁网站建设免费网站推广网站破解版
  • 网站建设域名的购买有域名和服务器怎么建网站
  • 深圳有什么网站长沙百度seo
  • 台州企业网站模板建站怎么在百度上做公司网页
  • 烟台网站建设联系企汇互联专业网站维护收费标准
  • 网络客户服务平台搜索优化推广公司
  • 建设网站技术方案线上教育培训机构十大排名
  • 沈阳人流seo优化师就业前景
  • 开发区网站制作公司seo关键词有话要多少钱
  • 网站被篡改处理app拉新平台
  • 在线房屋设计网站seo推广平台服务
  • 电子政务门户网站建设代码短链接生成网址
  • 崔各庄地区网站建设百度非企渠道开户
  • 怎么用自己的电脑做网站服务器产品推广平台排行榜
  • 中国做的比较好的电商网站有哪些哈市今日头条最新
  • 微信怎么做网站推广百度网站优化培训
  • 网站开发支持多个币种电子技术培训机构
  • 移动网站设计与制作怎么找关键词
  • 国内移动端网站做的最好的厦门人才网597人才网
  • 建网站收费吗aso关键词覆盖优化