python常用导入函数及其他操作备忘录
生活随笔
收集整理的这篇文章主要介绍了
python常用导入函数及其他操作备忘录
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
python常用导入函数及其他操作备忘录
- python常用导入函数
- 解压缩zip并读取csv文件
- 查看缺失值
- 权重系数取绝对值后排序(查看特征权重重要度)
python常用导入函数
from IPython.display import displayimport numpy as npimport 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) # 降序排列 《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读总结
以上是生活随笔为你收集整理的python常用导入函数及其他操作备忘录的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 张三丰安排刷题
- 下一篇: Python之pandas,series