机器学习中的python杂粮函数

机器学习中的python杂粮函数

第一、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

  1. In [1]: from sklearn.model_selection import KFold

  2. ...: import numpy as np

  3. ...: X = np.arange(24).reshape(12,2)

  4. ...: y = np.random.choice([1,2],12,p=[0.4,0.6])

  5. ...: kf = KFold(n_splits=5,shuffle=False)

  6. ...: for train_index , test_index in kf.split(X):

  7. ...: print('train_index:%s , test_index: %s ' %(train_index,test_index))

第三、

  1. import pandas as pd

  2. data = [[1,2,3],[4,5,6]]

  3. index = [0,1]

  4. columns=['a','b','c']

  5. df = pd.DataFrame(data=data, index=index, columns=columns)

  6. 1. loc——通过行标签索引行数据

  7. df.loc[1]

  8. '''''''

  9. a 4

  10. b 5

  11. c 6

  12. '''

  13. 1.2 loc[‘d’]表示索引的是第’d’行(index 是字符)

  14. import pandas as pd

  15. data = [[1,2,3],[4,5,6]]

  16. index = ['d','e']

  17. columns=['a','b','c']

  18. df = pd.DataFrame(data=data, index=index, columns=columns)

  19. df.loc['d']

  20. '''''''

  21. a 1

  22. b 2

  23. c 3

  24. '''

  25. 1.3 如果想索引列数据,像这样做会报错

  26. print df.loc['a']

  27. '''''''

  28. KeyError: 'the label [a] is not in the [index]'

  29. '''

  30. 1.4 loc可以获取多行数据

  31. print df.loc['d':]

  32. '''''''

  33. a b c

  34. d 1 2 3

  35. e 4 5 6

  36. '''

  37. 1.5 loc扩展——索引某行某列

  38. print df.loc['d',['b','c']]

  39. '''''''

  40. b 2

  41. c 3

  42. '''

  43. 1.6 loc扩展——索引某列

  44. print df.loc[:,['c']]

  45. '''''''

  46. c

  47. d 3

  48. e 6

  49. '''

  50. 当然获取某列数据最直接的方式是df.[列标签],但是当列标签未知时可以通过这种方式获取列数据。

  51. 需要注意的是,dataframe的索引[1:3]是包含1,2,3的,与平时的不同。

  52. 2. iloc——通过行号获取行数据

  53. 2.1 想要获取哪一行就输入该行数字

  54. df.iloc[1]

  55. '''''''

  56. a 4

  57. b 5

  58. c 6

  59. '''

  60. 2.2 通过行标签索引会报错

  61. print df.iloc['a']

  62. '''''''

  63. TypeError: cannot do label indexing on with these indexers [a] of

  64. '''

  65. 2.3 同样通过行号可以索引多行

  66. df.iloc[0:]

  67. '''''''

  68. a b c

  69. d 1 2 3

  70. e 4 5 6

  71. '''

  72. 2.4 iloc索引列数据

  73. df.iloc[:,[1]]

  74. '''''''

  75. b

  76. d 2

  77. e 5

  78. '''

  79. 3. ix——结合前两种的混合索引

  80. 3.1 通过行号索引

  81. df.ix[1]

  82. '''''''

  83. a 4

  84. b 5

  85. c 6

  86. '''

  87. 3.2 通过行标签索引

  88. df.ix['e']

  89. '''''''

  90. a 4

  91. b 5

  92. c 6

展开阅读全文

页面更新:2024-03-27

标签:行号   等份   子集   均等   杂粮   生成器   整数   示例   样本   函数   索引   机器   参数   标签   方式   数字   测试   数据   数码

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top