栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python分析及可视化

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python分析及可视化

目录

一、开发环境

二、使用步骤

三、数据分析

1.numpy模块

2.matplotlib模块

3. pandas模块 

3.1 Series类 

3.2 DataFrame类 



一、开发环境

1.anaconda

集成环境:集成了数据分析和机器学习中所需要的全部环境。

下载官网: https://www.anaconda.com/

2.jupyter

anaconda 提供的一个基于浏览器的可视化开发工具。

二、使用步骤

jupyter

1.启用:在终端中输入 jupyter notebook 指令。

2.新建:Python3源文件。

3.cell两种模式:code(代码)、Markdown(笔记)

4.cell快捷键:添加(a/b)、删除(x)、修改模式(m笔记、y代码)

5.执行cell:shift+enter

6.解释说明:shift+tab

三、数据分析

1.numpy模块

数值型数组 

1.创建

        使用np.array()创建

        使用plt创建

        使用np的outines函数创建 

2.数组和列表的区别

        数组中存储的数据元素类型必须一致。

        优先级:字符串>浮点型>整数

3.函数

        zero()

        ones()

        linespace()

        arange()

        random系列

'''
1.使用array()创建一个一维数组
2.使用array()创建一个二维数组
3.使用array()创建一个多类型数组
anaconda集成环境已经安装好了各个模块(numpy)
array(object,type='') 
    object 源数据
    type 数据类型
'''
import numpy as np #导入numpy模块取别名np
arr1 = np.array([1,2,3]) 
arr1 #打印arr,无需写print 
arr2 = np.array([[1,2,3],[4,5,6]]) 
arr2
arr3 = np.array([1,2.2,'three']) 
arr3
np.ones(shape = (3,4)) #创建各个元素都是1的3行4列的数组
np.linespace(0,100,num=20) #创建含有20个元素且各个元素之间的差都是一样的一维等差数列
np.arange(10,20,step=2) #创建10-20每隔2的一维等差数列
np.random.randint(0,100,size=(5,3)) #创建含有5行3列的0-100的随机元素的数组

打印结果:

---------------------------------------------------

array([1,2,3]) 

---------------------------------------------------

array([[1,2,3],

        [4,5,6]])

---------------------------------------------------

array(['1','2.2','three'],dtype='

---------------------------------------------------

2.matplotlib模块

1.imread()

2.imshow()

import matplotlib.pyplot as plt
image_arr = plt.imread('./1.jpg') #读取图片(三维数组),返回数组,数组中装载的是图片内容
image_arr #打印
plt.imshow(image_arr) #将numpy数组进行可视化展示,图片展读取到im坐标系中显示出来
image_arr = image_arr - 100 #将每一个数据元素都减去100
plt.imshow(image_arr)

打印结果:

-------------------------------------------

array([[[1,2,3],

        [4,5,6]

        ...

        [7,8,9]],

        [[1,2,3],

        [4,5,6]

        ...

        [7,8,9]]])

-------------------------------------------

1.jpg图片(原始图片)

-------------------------------------------

1.jpg图片(处理后的图片)

-------------------------------------------


3. pandas模块 

常用的类:

  • Series
  • DataFrame

3.1 Series类 

Series类 是一种类似一维数组的对象,由以下两部分组成:

  • values :一组数据(ndarray类型)
  • index:相关的数据索引标签

Series 的创建:

  • 由列表或numpy数组创建
  • 由字典创建

Series(data=[指定数组],index=[指定索引])

  • index:默认(隐式索引)是0开始的数值型索引,指定索引可增强可读性

Series的索引和切片

  • s[索引]  或者  s.指定索引
  • 切片  s[开始索引:结束索引]

Series常用属性

  • shape:形状
  • size:元素个数
  • index:索引
  • values:值
  • dtype:类型(dtype('O')表示object,字符串类型)

Series常用方法

  • head(n),tail(n):显示前/后n个数据,n默认是5
  • unique():去重
  • isnull(),notnull():判断元素是否为空/不为空
  • add(),sub(),mul(),div():算数运算,索引一致的元素进行算术运算,否则补空。
from pandas import Series
import numpy as np

s1 = Series(data=[1,2.15,'three'])
s1 #打印

s2 = Series(data=np.random.randint(0,100,size=(3,)))  #numpy创建一维数组
s2

s3 = Series(data=[1,2.15,'three'],index=['a','b','c'])
s3 #打印

dic = {
    '语文':99,
    '数学':88,
    '英语':77
}
s4 = Series(data=dic)
s4 #打印s4

s4[0]  #打印 99
s4.语文  #打印 99

s4[0:2]  #切片

s4.shape  #(3,) 即3行列,类似一维数组的对象
s4.size  #3 元素个数
s4.index  # 索引
s4.values  #值
s4.dtype #类型

s4.head(2)
s4.tail(2)

s4.unique()

s4.isnull()
s4.notnull()

s5 = Series(data=[1,2,3],index=['a','b','c'])
s6 = Series(data=[1,2,3],index=['a','d','c'])
s7 = s5 + s6
s7

 打印结果:

----------------------------

0                1

1                2.15

2                three

dtype:object

----------------------------

0                3

1                23

2                12

dtype:int64

----------------------------

a                1

b                2.15

c                three

dtype:object

----------------------------

语文                99

数学                88

英语                77

dtype:int64

----------------------------

99

99

----------------------------

语文                99

数学                88

dtype:int64

----------------------------

(3,)

3

index(['语文','数学','英语'],dtype='object')

array([99,88,77])

dtype('int64')

----------------------------

语文                99

数学                88

dtype:int64

----------------------------

数学                88

英语                77

dtype:int64

----------------------------

array([99,88,77])

----------------------------

语文                False

数学                False

英语                False

dtype:bool

----------------------------

语文                True

数学                True

英语                True

dtype:bool

----------------------------

a                2.0

b                NaN

c                6.0

d                NaN

dtype:float64

----------------------------

3.2 DataFrame类 

 DataFrame是一个【表格型】的数据结构,由按一定顺序排列的多列数据组成,多维。

  • 行索引:index
  • 列索引:columns
  • 值:values

DataFrame的创建:

  • 由narray创建
  • 由字典创建

DataFrame常用属性

  • shape:形状
  • index:行索引
  • columns:列索引
  • values:值

DataFrame的索引和切片

  • df[索引] :取单列,若df有显示索引,则通过索引机制取行或列时只可以使用显示索引
  • df[[索引,索引]] :取多列
  • df.iloc[索引]:取单行
  • df.iloc[[索引,索引]]:取多行

                loc:通过显示索引取行

                iloc:通过隐式索引取行

  • df[索引][索引]:取单个元素
  • df[[索引,索引],索引]:取多个元素
  • df[索引开始:索引结束]:切行
  • df.iloc[:,索引开始:索引结束]:切列

pandas的方法

  • pd.to_datetime(col):时间数据类型的转换
  • df.set_index():将某一列设置为行索引
from pandas import DataFrame
import pandas as pd

df1 = DateFrame(data=[[1,2,3],[4,5,6]])
df1

df2 = DateFrame(data=np.random.randint(0,10,size=(4,3)))
df2

dic3 = {
    'name':['zs','ls','ww'],
    'salary':[100,200,300]
}
df3 = DateFrame(data=dic)
df3

df3.iloc['name'] #取单列
df3.iloc[['name','salary']] #取多列

df3.iloc[0] #取单行
df3.iloc[[0,1]] #取多行

df3.iloc[0,0] #取单行
df3.iloc[[0,1],0] #取多行

dic4 = {
    'time':['2020-10-12','2021-10-12','2022-10-12'],
    'salary':[100,200,300]
}
df4 = DateFrame(data=dic)
df4['time'].dtype  #dtype('O') 

df4['time'] = pd.to_datetime(df4['time'])
df4['time'].dtype  #datetime64[ns]

df4.set_index('time',inplace=True)  #将time列作为源数据行索引
df4  #inplace=True改变原始数列,默认不改变

 输出结果:

--------------------------------------

 .       0        1        2

——————————

0        1        2        3

1        4        5        6

--------------------------------------

 .       0        1        2

——————————

0        7        2        5

1        1        9        6

2        2        4        3

3        5        8        0

--------------------------------------

 .       name       salary

————————————

0        zs              100

1        ls               200

2        ww             300

--------------------------------------

0        zs             

1        ls            

2        ww     

name:c,dtype:int64       

--------------------------------------

 .       name       salary

————————————

0        zs              100

1        ls               200

2        ww             300

--------------------------------------

name        zs

salary        100

name:c,dtype:int64

--------------------------------------

 .       name       salary

————————————

0        zs              100

1        ls               200

2        ww             300

--------------------------------------

zs

--------------------------------------

0        zs

1        ls

name:c,dtype:int64

--------------------------------------

 

暑期编程PK赛 得CSDN机械键盘等精美礼品!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1015854.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号