用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 ...
随机推荐
- 51nod 1105:第K大的数
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
- zabbix监控linux 以及监控mysql
Zabbix监控Linux主机设置方法 linux客户端 :59.128 安装了mysql 配置zabbix的yum源 rpm -ivh http://repo.zabbix.com/zabbix/2 ...
- BGP联邦配置
BGP联盟建立: ①:启用BGP进程. ②:关闭同步与自动汇总. ③:router-id ④:公布自己所属联盟.——confederation identifier ID ⑤:表达自己的与其他对等(p ...
- java中的字符串String
一.String简介d 参考:https://www.cnblogs.com/zhangyinhua/p/7689974.html String类代表字符串. java.lang.String: Ja ...
- 使用Spring AOP实现MySql的读写分离
转自:http://blog.csdn.net/xlgen157387/article/details/53930382 一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解 ...
- React16 新特性
一.使用Error Boundary处理错误组件 React16之前:组件在运行期出错,会阻塞整个应用的渲染. React16之后:引入新的错误处理机制——Error Bounda ...
- C++ 模板练习1
//特定的模板友元关系 #include "stdafx.h" #include <iostream> using namespace std; template< ...
- STL库中的equal_range()
equal_range根据键值,返回一对迭代器的pair对象.如果该键值在容器中存在,则pair对象中的第一个迭代器指向该键关联的第一个实例,第二个迭代器指向该键关联的最后一个实例的下一位置.如果找不 ...
- BIOS与UEFI
BIOS BIOS是英文"Basic Input Output System"的缩略词,直译过来后中文名称就是"基本输入输出系统".在IBM PC兼容系统上,是 ...
- ssh到ubuntu没颜色
ssh远程到ubuntu系统, 没有颜色. 原因是 .bashrc 配置没生效. $ echo '. $HOME/.bashrc' > ~/.profile