[Python笔记] 开发中遇到的部分实用小问题

python 部分实用小问题

持续更新中……

1.读入列名csv转df后,如何转为可hash的list给别的df当索引用?

适用于如下场合:
特征工程中初步选取的top特征集存在csv里了,想大批pd.read_csv直接用,但是直接df.values.tolist()的话会报unhashable type ‘list’。这是因为转完的list长成[[‘a’],[‘b’],[‘c’]]这样,而不是我们所需要的可以直接df[cols]的[‘a’,‘b’,‘c’]。

cols = pd.read_csv('path')
cols = np.array(data)
cols = data.reshape(1, len(data)).tolist()
cols = data[0]
train = df[cols]

2.Jupyter Notebook的checkpoint是?

Jupyter creates a checkpoint file every single time you create an .ipynb file, and then updates the checkpoint file every single time you manually save your progress for the initial .ipynb. A manual save is what’s done when the file is saved by clicking the Save and Checkpoint button:
Auto-saving, on the other hand, updates only the initial .ipynb file, not the checkpoint file.
When you revert from the initial .ipynb file to a previously saved checkpoint by using the Revert to Checkpoint button, the checkpoint file is what gets accessed and opened inside Jupyter.
As a side note, the checkpoint file is located within a hidden folder named .ipynb_checkpoints. This folder is located within the same folder as the initial .ipynb file.

3.~的意义

~,用法只有一个那就是按位取反,需要注意的是:
~的按位取反,包括符号位。正数各位取反变为负数,显示时转化为其补码。
负数本身需要先转换为补码(符号位不变,各位取反再加 1),再对其补码进行各位取反。
进阶用法:去除缺失值的操作

df = df[~df['meme'].isnull()]

4.令可视化表格变得丰富的操作

在调库的时候设置好matplotlib。

%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
pd.set_option('display.max_columns', None)

5.对总体标签列表,每行进行拆分再组合为一个大列表,可进行标签按类别统计计数。

cnt = Counter(reduce(lambda x, y: x+y, [r.split(',') for r in df['tag'].values]))

其中,reduce() 函数会对参数序列中元素进行累积。将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。

6.pandas contact 之后,一定要记得用reset_index去处理index,不然容易出现莫名的逻辑错误。

result = result.reset_index(drop=True)来改变index就可以了。

7. lightGBM小笔记

  1. lightGBM设定的学习方法是GBDT即梯度提升树时,模型会做bagging,也就是说,样本顺序会影响最终结果。
  2. lgb每次会选择部分特征建模,加减特征重要性为0的特征,相当于修改随机种子,结果会在预计范围内波动。
  3. lgb对于缺失值nan的处理:
    通常来讲:lightGBM会在分支时无视缺失值,再最终将所有缺失值分入某一侧使损失函数最小。手动填充缺失值是可以提升模型表现的,就比如说给缺失值的位置都填上-100,lgb在划分的时候会将该样本计入考虑,只要特征自身是有效力的,填充其缺失的值便是可以带来一定效果的。

你可能感兴趣的:(Python笔记)