机器学习笔记

文件读取

csv文件

#读取train.csv文件
import pandas as pd
import numpy as np
data =pd.read_csv("C:\\Users\\Administrator\\Desktop\\train.csv",index_col ='Loan_ID')
#指定索引列 index_col

excel文件

可以指定表单

data.to_excel('train.xlsx')#将文件写出为excel的格式
data2 = pd.read_excel('train.xlsx',sheet_name='Sheet1')

编码错误

encoding = ‘gbk’ 或者 encoding = ‘UTF-8’

pandas数据类型

dataframe

行 index
列 columns

数据选择

loc

data.loc[(data['Education']=='Not Graduate') & (data['Loan_Status']=='Y') & (data['Gender']=='Female'),['Gender','Education','Loan_Status'] ]

限制性别,是否毕业,贷款状态的列表

遍历dataframe

apply + 自定义函数

缺失值检查

# 使用apply对数据集应用自定义函数
def num_missing(x):
    return sum(x.isnull())
# 使用apply函数将num_missing函数用于统计数据集的每列缺失值数量
#  axis=0为列  axis=1为行
print(data.apply(num_missing,axis=0)) 

在这里插入图片描述

iterrows

iterrows() 是在数据框中的行进行迭代的一个生成器,它返回每行的索引及一个包含行本身的对象。

for index,row in data.iterrows():
	print(index)
	print(row['Gender'])

缺失值填充

对于Gender、Married、Self_Employed三个因子型变量,使用各自最常见的因子进行缺失值填充

data['Gender'].fillna(data['Gender'].mode().iloc[0],inplace=True)
data['Married'].fillna(data['Married'].mode().iloc[0],inplace=True)
data['Self_Employed'].fillna(data['Self_Employed'].mode().iloc[0],inplace=True)

对于LoanAmount变量进行缺失值填充处理
按照“Gender”、“Married”及“Self_Employed”的组合下的每个组群进行LoanAmount变量的均值统计
按照每组统计得到的平均值,对“LoanAmount”中缺失值进行填充

impute_grps = data.pivot_table(values=["LoanAmount"],index=["Gender","Married","Self_Employed"],aggfunc=np.mean)
for i,row in data.loc[data['LoanAmount'].isnull(),:].iterrows():
    ind = tuple([row['Gender'],row['Married'],row['Self_Employed']])
    data.loc[i,'LoanAmount']=impute_grps.loc[ind].values[0]

统计分析

min max mean

series,Dataframe

mode

series类型
iloc获取值

describe

corr(相关性矩阵)

分组统计/数据透视表

pivot_table

values

列表,元素为列名

index

列表,元素是列名

aggfunc

字典,元素“列名”:函数[函数1,函数2]

样例代码

impute_grps = data.pivot_table(values=["LoanAmount"."App icantincome"],
                               Index=["Gender","Maried","Self_employed"],
                               aggfunc={"LoanAmount":np.mean,"ApplicantIncome":[np.sum,np.mean]})
impute_grps                              

groupby

groupby(by=‘列名’)
返回一个group类型

groupby().agg(‘列1’:‘mean’,‘列2’:‘max’)
返回dataframe

groupby('company').agg('salary':'median','age':'mean')

groupby().apply()
传递给自定义函数的是dataframe

def func1(x):
	print(type(x))
	print(x)
groupby().apply(func1)

crosstab

列Series,可以用列表保存多列
margins 是否统计总数
nomalize 是否求比例

pd.crosstab(data("Credit_History"),[data['Gender'],data['Loan_Status'],margins=True,normalize=True)

合并数据集

merge

1.right=
2.join
3.left_on/right_on
列名是否要一致?否
列的内容是可以否定
4.
left_index/right_index
True/False

data_merged = data.merge(right=prop_rates,how='inner',left_on='Property_Area',right_index=True,sort=False)
data_merged.pivot_table(values='Credit_History',index=['Property_Area','rates'],aggfunc=len)

在这里插入图片描述

concat

1.默认index列进行合并
2axis=1/0
1=列合并
2=行合并
3.join

排序

sort_values

ascending=True/False

sort_index

1.时间排序
2.序号(学号,工号)

示例

将data数据集按照ApplicantIncome、CoapplicantIncome 两列变量值进行降序排列,并输出排序后数据集的前10行

data_sorted=data.sort_values(['ApplicantIncome','CoapplicantIncome'],ascending=False)
data_sorted[['ApplicantIncome','CoapplicantIncome']].head(10)

变量离散化、——pd.cut

1.列,Series
2.bins=[…] (划分区间)
3.labels
4.right
(9,90)不包括90
(9,90] 包括90

cut_points=[90,140,190]
break_points=[data['LoanAmount'].min()]+cut_points+[data['LoanAmount'].max()]
print(break_points)
labels=["low","medium","high","very_high"]
data['LoanAmount_Bin']=pd.cut(data['LoanAmount'],bins=break_points,right=False,labels=labels,include_lowest=True)
pd.value_counts(data['LoanAmount_Bin'],sort=False)

独热编码——get_dummies

1.列,Series
2.prefix=‘’

dummies=pd.get_dummies(data['LoanAmount_Bin'],prefix='LoanAmount')
data_onehot=pd.concat([data,dummies],axis=1)
data_onehot.head(10)

可视化

相关性热力图

1.corr()
2.seaborn。heatmap()
数据:相关性矩阵数据
取色区间设置:vmin和vmax
标注数据:annot = True
3.pandas plot bar
数据:dataframe
数值格式:
X:dataframe中的index
Y:dataframe中的一列
4.pandas plot hist
数据:一列数据,Series
数值格式
X:列数据的值的范围,按照bins设置进行区间划分
Y:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值