python(20190120)

2019-01-20  本文已影响0人  叫兽吃橙子

基本语法

1.loc

import pandas as pd
import numpy
titanic.loc[titanic["Embarked"] == "S", "Embarked"] = 0  #titanic中使用
# 导入数据
df = pd.read_csv(filepath_or_buffer="D://movie.csv")
df_new = df.set_index(["country"])
df_new.loc[list(["Canada"])] # 1
df_new.loc[df_new["duration"]>160] # 2
df_new.loc[((df_new["duration"] > 200) & (df_new["director_facebook_likes"] > 300 )),"flage"] =1 # 3
df_new.loc[df_new["duration"].isin([100])] # 4
df_new.query("duration > 100 & index == 'UK'") # 5

1:根据列中的元素,选取对应元素的数据集 
2:根据元素的选取条件来选取对应的数据集 
3:根据元素的选取条件来来选取对应的数据集,并在符合条件的数据行添加flage标签 
4:isin函数是series用来判断值是否在目标值是否在series 
5:query函数中用来判断条件符合的数据集并返回

2.fillna:填充;median:平均值;describe:数据总体描述;unique:去重

titanic["Age"] = titanic["Age"].fillna(titanic["Age"].median())
print (titanic.describe())
print (titanic["Sex"].unique())

3.shape

>>> c = array([[1,1],[1,2],[1,3],[1,4]])  
>>> c.shape  
(4, 2)  
>>> c.shape[0]  
4  
>>> c.shape[1]  
2  

4.KFold通过提供index来给你确定不同组的训练集以及测试的index,来构造交叉验证数据集。

参数(n, n_folds=3, shuffle=False, random_state=None)
n为总数
n_folds为分为多少个交叉验证集
shuffle为是否随机
random_state设置随机因子
https://blog.csdn.net/qq_16949707/article/details/79080432

上一篇 下一篇

猜你喜欢

热点阅读