用Plotily处理数据的基本操作
import pandas as pd # 导入数据.scv df = pd.read_csv(" .csv") # 查看前五行数据 df.head() # 查看一下数据描述 df.descirbe() # 查看一下数据的形状 df.shape # 查看一下数据集中都包含哪些列 df.columns # 对数据进行可视化 import matplotlib.pyplot as plt import seaborn as sns # 使用Jupyter Notebook # import warnings # warnings.filterwarnings("ignore") # %matplotlib inline # 创建自定义图像 figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True) # 标题 plt.title(" ") # 画出数据分布图 sns.displot(df[" "]) # !pip install plotly # 安装Plotily库 # 导入绘图工具库 import plotly.offline as offline import plotly.graph_objs as go import plotly.offline as py from plotly.offline import init_notebook_mode, iplot init_notebook_mode(connected=True) offline.init_notebook_mode() # 查看表格某列中有多少个不同值的快捷方法,并计算每个不同值有在该列中有多少重复值 temp = df[" "].value_counts() # 画出柱状图,查看不同值所占比重 trace = [go.Bar(x = temp.index, y = (temp / temp.sum()) * 100)] # 设置图的字体颜色等 layout = go.Layout( title=" ", xaxis=dict(title=' ', tickfont=dict(size= , color='rgb( , , )')), yaxis=dict(title=' ', titlefont=dict(size= , color='rgb( , , )'), tickfont=dict(size= , color='rgb( , , )')) ) # 显示图形 fig = go.Figure(data=trace, layout=layout) iplot(fig, filename=' ') # 画出饼状图 trace = [go.Pie(labels=temp.index, values=temp.values)] # 设置图题 layout = go.Layout( title=' ', ) # 显示图形 fig = go.Figure(data=trace, layout=layout) iplot(fig) # 画出饼状图,圆环型 trace = [go.Pie(labels=temp.index, values=temp.values, hole=0.6)] temp1 = df["FLAG_OWN_CAR"].value_counts() temp2 = df["FLAG_OWN_REALTY"].value_counts() # 画出两个饼状图 trace = [go.Pie(labels=temp1.index, values=temp1.values, domain={"x": [0, .48]}, hole=0.6), go.Pie(labels=temp2.index, values=temp2.values, domain={"x": [0.5, 1]}, hole=0.6)] # 设置图中的字体,图题等 layout = go.Layout( title=' ', annotations=[{"font": { "size": }, "showarrow": , "text": " ", "x": 0. , # 坐标 "y": 0. }, {"font": { "size": }, "showarrow": , "text": " ", "x": 0. , "y": 0. }]) # 显示图形 fig = go.Figure(data=trace, layout=layout) iplot(fig) # 计数方法 temp_y0 = [] temp_y1 = [] for val in temp.index: temp_y1.append(np.sum(df["TARGET"][df["TYPE"] == val] == 1)) temp_y0.append(np.sum(df["TARGET"][df["TYPE"] == val] == 0)) temp_y1 = np.array(temp_y1) temp_y0 = np.array(temp_y0) # 删除掉存在缺失值的特征列 df_drop = df.dropna(axis=1) df_drop.head() # 将其编码成为数值形式 from sklearn import preprocessing # 取出非数值的列 categorical_feats = [ f for f in df_drop.columns if df_drop[f].dtype == 'object' ] # 对非数值的列进行编码 for col in categorical_feats: lb = preprocessing.LabelEncoder() lb.fit(list(df_drop[col].values.astype('str'))) df_drop[col] = lb.transform(list(df_drop[col].values.astype('str'))) df_drop.head() # 划分数据 # 删除ID df_drop1 = df_drop.drop("ID", axis=1) # 提取训练特征数据和目标值 data_X = df_drop1.drop(" ", axis=1) data_y = df_drop1[' '] #划分数据集为训练数据集和测试数据集 from sklearn import model_selection train_x, test_x, train_y, test_y = model_selection.train_test_split(data_X.values, data_y.values, test_size=0.8, random_state=0) # 构建预测模型 from sklearn.ensemble import RandomForestClassifier model = RandomForestClassifier() # 构建模型 model.fit(train_x, train_y) # 训练模型 from sklearn import metrics y_pred = model.predict(test_x) # 预测测试集 metrics.accuracy_score(y_pred, test_y) # 评价预测结果 print(metrics.classification_report(y_pred, test_y)) features = data_X.columns.values # 取出数据集中的列名,即特征名 # 得到特征与其重要性 x, y = (list(x) for x in zip(*sorted(zip(model.feature_importances_, features), reverse=False))) # 画出柱状图 trace2 = go.Bar(x=x, y=y, marker=dict(color=x, colorscale='Viridis', reversescale=True), name=' ', orientation='h',) # 设置图题、字体等 layout = dict(title=' ', width=900, height=2000, yaxis=dict(showgrid=False, showline=False, showticklabels=True,), margin=dict(l=300,)) # 显示图形 fig1 = go.Figure(data=[trace2]) fig1['layout'].update(layout) iplot(fig1, filename='plots') from sklearn.tree import DecisionTreeClassifier from sklearn.neural_network import MLPClassifier from sklearn.ensemble import AdaBoostClassifier from sklearn.ensemble import BaggingClassifier from sklearn.ensemble import GradientBoostingClassifier from sklearn.linear_model import LogisticRegression # 构建 7 种算法 models = [LogisticRegression(solver='lbfgs'), # 逻辑回归 RandomForestClassifier(n_estimators=100), # 随机森林 DecisionTreeClassifier(), # 决策树 MLPClassifier(max_iter=100), # 多层感知机 AdaBoostClassifier(), # 自适应梯度提升 BaggingClassifier(), # 装袋算法 GradientBoostingClassifier()] # 梯度提升算法 model_name = ['LogisticRegression', 'RandomForestClassifier', "DecisionTreeClassifier", 'MLPClassifier', 'AdaBoostClassifier', 'BaggingClassifier', 'GradientBoostingClassifier'] acc = [] # 存放各算法的准确率 f1 = [] # 存放各算法的 f1 值 recall = [] # 存放各算法的召回率 for model in models: # 训练每个算法 model.fit(train_x, train_y) acc.append(model.score(test_x, test_y)) y_pred = model.predict(test_x) f1.append(metrics.f1_score(y_pred, test_y)) recall.append(metrics.recall_score(y_pred, test_y)) # 打印每种算法的评估结果 pd.DataFrame({"name": model_name, "acc": acc, "f1": f1, "recall": recall})
用Plotily处理数据的基本操作的更多相关文章
- MySQL:数据表基本操作
数据表基本操作 注意点: 1.数据表中已经有数据时,轻易修改数据类型,有可能因为不同的数据类型的数据在机器 中存储的方式及长度并不相同,修改数据类型可能会影响到数据表中已有的数据类型. 2. 数据表 ...
- MySQL之终端(Terminal)管理数据库、数据表、数据的基本操作(转)
MySQL有很多的可视化管理工具,比如“mysql-workbench”和“sequel-pro-”. 现在我写MySQL的终端命令操作的文章,是想强化一下自己对于MySQL的理解,总会比使用图形化的 ...
- MySQL系列:数据表基本操作(2)
1. 指定数据库 mysql> use portal; 2. 数据库表基本操作 2.1 查看数据表 mysql> show tables; +------------------+ | T ...
- pandas学习(创建数据,基本操作)
pandas学习(一) Pandas基本数据结构 Series类型数据 Dataframe类型 基本操作 Pandas基本数据结构 两种常用数据结构: Series 一维数组,与Numpy中的一维ar ...
- MySQL 数据库、数据表、数据的基本操作
1.数据库(database)管理 1.1 create 创建数据库 create database firstDB; 1.2 show 查看所有数据库 mysql> show database ...
- ASP.NET的一般处理程序对数据的基本操作
TableList.ashx: <%@ WebHandler Language="C#" Class="TableList" %> using Sy ...
- mysql数据的基本操作
本文内容: 插入数据: 查询数据 修改数据 删除数据 首发日期:2018-04-11 插入数据: 给所有字段插入数据: 插入单条记录:insert into 表名 values(值列表); 插入多条记 ...
- MySQL开发——【数据的基本操作】
增加数据 基本语法: insert into 数据表 [字段名称1,字段名称2..] values (数据1,数据2...); 特别注意:针对数据类型整型.浮点型数据可以不加单引或双引号,但是如果字段 ...
- MySQL 5.6学习笔记(数据表基本操作)
1. 创建数据表 1.1 最基本的语法 CREATE TABLE tbl_name (col_name column_definition,...) [table_options] -column_d ...
随机推荐
- 【Java Spring 进阶之路 】1.Spring 是什么?
- Python开源库的bug
scipy 在misc的pilutil.py中def fromimage(im, flatten=0)函数中, # workaround for crash in PIL, see #1613.im. ...
- Vue.js(16)之 directive自定义指令
推荐阅读:Vue.directive基础,在Vue模块开发中使用 全局指令 Vue.directive('全局自定义指令名称', { /* 自定义指令配置对象 */ }) 私有指令 <templ ...
- 第二届中国“AI+”创新创业大赛完美收官,京东云赛道硕果累累
聚焦南京产业发展核心诉求,京东云携手南京政府构建的"平台+生态+赋能"的产业体系,搭建产业创新云平台,以人工智能产业创新链要素补齐为核心,围绕"研.产.供.销.服&quo ...
- Android进阶——学习AccessibilityService实现微信抢红包插件
在你的手机更多设置或者高级设置中,我们会发现有个无障碍的功能,很多人不知道这个功能具体是干嘛的,其实这个功能是为了增强用户界面以帮助残障人士,或者可能暂时无法与设备充分交互的人们 它的具体实现是通过A ...
- 获取网站IP地址(Linux,C)
#include <netdb.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> ...
- JSON整理
1.什么是JSON JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 2.JSON基于两种结构: (1 )“名称/值“对的集合(A co ...
- 读取word模板,填充数据后导出
一.需求说明 定期生成word报告,报告中含有文本.表格.图表等元素,依次获取进行替换,保留原有样式,生成新的word文档 二.引入依赖 <dependency> <groupId& ...
- sed使用案例
简介: sed是一种流编辑器,它是文本处理中非常重要的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用 ...
- HTML5中的行级标签和块级标签
行级标签 1.行级标签又称为内联标签,行级标签不会单独占据一行,设置宽高无效. 2.行内内部可以容纳其他行内元素,但不可以容纳块元素.有span.strong.em.b.i.input.a.img.u ...