python常用导入函数及其他操作备忘录

python常用导入函数

from IPython.display import display

import numpy as np

import pandas as pd
from pandas import Series,DataFrame

# 忽略警告
import warnings
warnings.filterwarnings('ignore')

from PIL import Image  

import matplotlib.pyplot as plt
%matplotlib inline  
# 图片大小的两种方式
plt.rcParams['figure.figsize'] = (14.0, 8.0)   
plt.figure(figsize = (12,6))

plt.rcParams['font.sans-serif']=['SimHei']     # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False       # 用来正常显示负号
# 将横、纵坐标轴标准化处理,确保饼图是一个正圆,否则为椭圆
plt.axes(aspect='equal')  

# 设置x轴日期的显示格式:月-日 
date_format = mpl.dates.DateFormatter("%m-%d")  
ax.xaxis.set_major_formatter(date_format)

# 设置x轴每个刻度的间隔个数,例:每7个数作为间隔
xlocator = mpl.ticker.MultipleLocator(7)
ax.xaxis.set_major_locator(xlocator)

# 为了避免x轴刻度标签的紧凑,将刻度标签旋转45度
plt.xticks(rotation=45)

# 设置绘图风格(不妨使用R语言中的ggplot2风格)
plt.style.use('ggplot') 

# 调整子图之间的水平间距和高度间距
plt.subplots_adjust(hspace=0.6, wspace=0.3)    
    
%config ZMQInteractiveShell.ast_node_interactivity='all'   # nootbook使用

from scipy import interp             # 线性插值

from selenium import webdriver
# 我的环境变量没有配置成功,每次都要调用路径的Chromedriver
path = "D:/box/chromedriver_win32/chromedriver"
browser = webdriver.Chrome(executable_path=path, options=webdriver.ChromeOptions())
browser.get('http://www.baidu.com')

from sklearn.tree import export_graphviz
# 需要在电脑中安装Graphviz
# https://graphviz.gitlab.io/_pages/Download/Download_windows.html
# 然后将解压文件中的bin设置到环境变量中
# 不过我手工在环境变量中添加了bin路径不行,运行下边这个语句
import os
os.environ["PATH"] += os.pathsep + 'D:/software/graphviz-2.38/release/bin/'  #注意修改你的路径

# 数据集拆分为训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

# 标准化数据,使每个维度的特征数据均值为0,方差为1
from sklearn.preprocessing import StandardScaler
s = StandardScaler()
x_train = s.fit_transform(X_train)
x_test = s.transform(X_test)   # 前面fit后,后面只需要transform即可

# Display TF info logs
tf.logging.set_verbosity(tf.logging.INFO)

解压缩zip并读取csv文件

import pandas as pd
pd.set_option('display.max_columns', 500)   # 显示最大列数,如果超出,省略号表示

import zipfile
with zipfile.ZipFile('KaggleCredit2.zip', 'r') as z:
    f = z.open('KaggleCredit2.csv')
    data = pd.read_csv(f, index_col=0)  # index_col=0表示不设置索引列,以默认数字0,1,2,3...
data.head()

查看缺失值

data.isnull()                  # 缺失值判断:是缺失值返回True,否则范围False
data.isnull().sum(axis=0)      # 缺失值计算:返回每列包含的缺失值的个数
data.dropna()                  # 缺失值删除:直接删除含有缺失值的行
data.dropna(inplace=True)      # 删除缺失值,并且用删除之后的数据替换掉原数据
data.dropna(axis = 1)          # 缺失值删除列:直接删除含有缺失值的列
data.dropna(how = 'all')       # 缺失值删除行:只删除全是缺失值的行
data.dropna(thresh = n)        # 缺失值删除判断:保留至少有n个缺失值的行
data.dropna(subset = ['C'])    # 缺失值删除列:删除含有缺失值的特定的列

权重系数取绝对值后排序(查看特征权重重要度)

# 各个特征的权重系数
pd.Series(lr.coef_[0],index=X.columns)   # cls.coef_[0]一维数组,否则会出错

# 取绝对值并排序
pd.Series(np.abs(lr.coef_[0]),index=X.columns).sort_values(ascending=False)  # 降序排列

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值