用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 ...
随机推荐
- UVA - 10791 Minimum Sum LCM(最小公倍数的最小和)
题意:输入整数n(1<=n<231),求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小.输出最小的和. 分析: 1.将n分解为a1p1*a2p2……,每个aipi作为一个单独 ...
- CGridCtrl添加右键菜单
头文件下添加: afx_msg void OnMergeCell(); afx_msg void OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/); 添加 ...
- MFC 根据字符宽度居中
Gdiplus::Font font(_T("微软雅黑"), (Gdiplus::REAL)130); Gdiplus::RectF orgin(0.0f, 100.0f, nWi ...
- WTL 选择文件
CFileDialog dlg(true, 0, 0, OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_PATHMUSTEXIST, NULL, m_hWnd); dlg ...
- C++基础--函数模板
函数模板是通用的函数描述,其使用泛型来定义函数.其实就是有些操作,如果撇开具体的变量的数据类型,其操作是一样的如果我们将这些操作写成一个模板,在调用不同变量的时候就设定好变量类型就可了,后续的操作基本 ...
- cf 187B.AlgoRace
floyd...太神奇了(不会floyd(大雾)) 貌似floyd的外层k是保证最短路从起点逐渐向外扩展(而不是乱搞233) 所以在处理f[i][j]=min(f[i][j],f[i][k]+f[k] ...
- 吴裕雄--天生自然TensorFlow2教程:张量限幅
import tensorflow as tf a = tf.range(10) a # a中小于2的元素值为2 tf.maximum(a, 2) # a中大于8的元素值为8 tf.minimum(a ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于Annotation装配Bean
在 Spring 中,尽管使用 XML 配置文件可以实现 Bean 的装配工作,但如果应用中 Bean 的数量较多,会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难. Java 从 J ...
- 流程控制语句反汇编(1)(Debug版)
// 流程控制语句反汇编 //Author:乾卦 Date:2014-5-8 #include<stdio.h> int main() { ,b=; if(a>b) { a=b; } ...
- OpenStack(四)——使用Kolla部署OpenStack多节点云
(1).实验环境 主机名 IP地址 角色 内存 网卡 CPU 磁盘 OpenStack-con 192.168.128.110 controller(控制) 8G 桥接网卡ens32和ens33 4核 ...