第一、numpy.random.choice(a, size=None, replace=True, p=None)
概述:可以从一个int数字或1维array里随机选取内容,并将选取结果放入n维array中返回。12
说明:
numpy.random.choice(a, size=None, replace=True, p=None)
a : 1-D array-like or int If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a was np.arange(n) size : int or tuple of ints, optional replace : boolean, optional Whether the sample is with or without replacement p : 1-D array-like, optional The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.123456789101112
示例
>>> np.random.choice(5, 3) array([0, 3, 4]) >>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0]) array([3, 3, 0]) >>> np.random.choice(5, 3, replace=False) array([3,1,0]) >>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0]) array([2, 3, 0]) >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher'] >>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3]) array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'],
第二、K折交叉验证:sklearn.model_selection.KFold(n_splits=3, shuffle=False, random_state=None)
思路:将训练/测试数据集划分n_splits个互斥子集,每次用其中一个子集当作验证集,剩下的n_splits-1个作为训练集,进行n_splits次训练和测试,得到n_splits个结果
注意点:对于不能均等份的数据集,其前n_samples % n_splits子集拥有n_samples // n_splits + 1个样本,其余子集都只有n_samples // n_splits样本
参数说明:
n_splits:表示划分几等份
shuffle:在每次划分时,是否进行洗牌
①若为Falses时,其效果等同于random_state等于整数,每次划分的结果相同
②若为True时,每次划分的结果都不一样,表示经过洗牌,随机取样的
random_state:随机种子数
属性:
①get_n_splits(X=None, y=None, groups=None):获取参数n_splits的值
②split(X, y=None, groups=None):将数据集划分成训练集和测试集,返回索引生成器
通过一个不能均等划分的栗子,设置不同参数值,观察其结果
①设置shuffle=False,运行两次,发现两次结果相同
[python] view plain copy
In [1]: from sklearn.model_selection import KFold
...: import numpy as np
...: X = np.arange(24).reshape(12,2)
...: y = np.random.choice([1,2],12,p=[0.4,0.6])
...: kf = KFold(n_splits=5,shuffle=False)
...: for train_index , test_index in kf.split(X):
...: print('train_index:%s , test_index: %s ' %(train_index,test_index))
第三、
import pandas as pd
data = [[1,2,3],[4,5,6]]
index = [0,1]
columns=['a','b','c']
df = pd.DataFrame(data=data, index=index, columns=columns)
1. loc——通过行标签索引行数据
df.loc[1]
'''''''
a 4
b 5
c 6
'''
1.2 loc[‘d’]表示索引的是第’d’行(index 是字符)
import pandas as pd
data = [[1,2,3],[4,5,6]]
index = ['d','e']
columns=['a','b','c']
df = pd.DataFrame(data=data, index=index, columns=columns)
df.loc['d']
'''''''
a 1
b 2
c 3
'''
1.3 如果想索引列数据,像这样做会报错
print df.loc['a']
'''''''
KeyError: 'the label [a] is not in the [index]'
'''
1.4 loc可以获取多行数据
print df.loc['d':]
'''''''
a b c
d 1 2 3
e 4 5 6
'''
1.5 loc扩展——索引某行某列
print df.loc['d',['b','c']]
'''''''
b 2
c 3
'''
1.6 loc扩展——索引某列
print df.loc[:,['c']]
'''''''
c
d 3
e 6
'''
当然获取某列数据最直接的方式是df.[列标签],但是当列标签未知时可以通过这种方式获取列数据。
需要注意的是,dataframe的索引[1:3]是包含1,2,3的,与平时的不同。
2. iloc——通过行号获取行数据
2.1 想要获取哪一行就输入该行数字
df.iloc[1]
'''''''
a 4
b 5
c 6
'''
2.2 通过行标签索引会报错
print df.iloc['a']
'''''''
TypeError: cannot do label indexing on
'''
2.3 同样通过行号可以索引多行
df.iloc[0:]
'''''''
a b c
d 1 2 3
e 4 5 6
'''
2.4 iloc索引列数据
df.iloc[:,[1]]
'''''''
b
d 2
e 5
'''
3. ix——结合前两种的混合索引
3.1 通过行号索引
df.ix[1]
'''''''
a 4
b 5
c 6
'''
3.2 通过行标签索引
df.ix['e']
'''''''
a 4
b 5
c 6
页面更新:2024-03-27
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号