Numpy

将字符型数据转为datetime

import numpy as np
f = np.array(['','2019-01-01','2019-01-02 01:01:01']) # 把f数组的元素类型改为日期类型
g = f.astype('M8[D]') # M8[Y] M8[M] M8[D]
print(g) # 时间戳(将日期转为数) 上面g的单位不同,这边的数值也不同
# g中的值距离1970年总共有多少天
h = g.astype('int32')
print(h)
print(h[] - h[])

生成ndarray数组

- np.random.random((2,2))
- np.ones((3,4))
- np.zeros((2,2), dtype='int32')
- np.arange(1,10)
- np.linspace(0,2,10)
- np.eye(3)
- np.full((3,3),7)

np.random.random((,))
Out[]:
array([[ 0.61705652, 0.48264423],
[ 0.69303143, 0.35004567]]) np.ones((,))
Out[]:
array([[ ., ., ., .],
[ ., ., ., .],
[ ., ., ., .]]) np.zeros((,), dtype='int32')
Out[]:
array([[, ],
[, ]]) np.arange(,)
Out[]: array([, , , , , , , , ]) np.linspace(,,)
Out[]:
array([ . , 0.22222222, 0.44444444, 0.66666667, 0.88888889,
1.11111111, 1.33333333, 1.55555556, 1.77777778, . ]) np.eye()
Out[]:
array([[ ., ., .],
[ ., ., .],
[ ., ., .]]) np.full((,),)
Out[]:
array([[, , ],
[, , ],
[, , ]])

Numpy 的random模块

# 使用numpy.random的normal函数生成符合二项分布的随机数
n =
# :期望值
# :标准差
# n:数字生成数量
x= np.random.normal(, , n)
y= np.random.normal(, , n)

ndarray数组对象的维度操作

视图变维:array.reshape() array.ravel()

- ravel() 是扁平化但是不复制,公用一个对象
- flatten() 是扁平化同时复制,会生成一个新对象并且返回

import numpy as np
a = np.arange(,)
# 视图变维使用的还是原始数组中的数据,如果修改了原始数组中的数据,那么新数组读到的数据也会发生变化。
b = a.reshape((,))
print(a,b)
a[] =
print(b)
c = b.ravel()
print(c)

复制变维(数据独立):flatten()

# 测试flatten
d=b.flatten().reshape((,))
d[] =
print(b)
print(d)

就地变维:直接修改数组维度,不返回新数组 resize() shape

d.resize(,,)
d.shape=(,)
print(d)

ndarray数组的切片操作

# 数组的切片与列表的切片参数类似
# 步长为正:默认从前往后切
# 步长为负:默认从后往前切
array[起始位置:终止位置:步长]
a = np.arange(,) # array([, , , , , , , , ])
a.resize(,) # array([[, , ],
# [, , ],
# [, , ]])
a[:, :] # 第2行到最后一行,所有列

ndarray数组的掩码操作

a = np.array([,,,,,,,])
f = np.array([True, False, True, False,False, True, False, True])
a[f]
Out[]: array([, , , ]) # 现在有数组的1-,我们现在要拿到数组中3的倍数或7的倍数
flag_a = a%==
flag_b = a%== flag_a
Out[]:
array([False, False, True, False, False, True, False, False, True,
False, False, False, False, False, ... False, False, False,
False, False, True, False, False, True, False, False, True, False], dtype=bool) flag = np.any([flag_a, flag_b], axis=) a[flag]
Out[]:
array([ , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , ,
, , , , , , , , ])

多维数组的组合和拆分

垂直方向的操作:vstack() vsplit()

a = np.arange(,).reshape(,)
b = np.arange(,).reshape(,) a
Out[]:
array([[, , ],
[, , ]])
b
Out[]:
array([[ , , ],
[, , ]]) c = np.vstack((a,b))
c
Out[]:
array([[ , , ],
[ , , ],
[ , , ],
[, , ]]) a,b = np.vsplit(c, )

水平方向的操作:hstack() hsplit()

d = np.hstack((a,b))
a,b = np.hsplit(d, )

深度方向的操作:dstack() dsplit() 二维数组深度操作会变为三维数组,最后拆分也是三维数组

a
Out[]:
array([[, , ],
[, , ]]) b
Out[]:
array([[ , , ],
[, , ]]) e = np.dstack((a,b)) e
Out[]:
array([[[ , ],
[ , ],
[ , ]], [[ , ],
[ , ],
[ , ]]]) a,b = np.dsplit(e,)
a
Out[]:
array([[[],
[],
[]], [[],
[],
[]]])

多维数组组合与拆分的相关函数

# 根据axis所指定的轴向(,,)进行多维数组的组合
# 如果待组合的两个数组都是二维数组
# axis=:垂直方向
# axis=:水平方向
# 如果待组合的两个数组都是三维数组
# axis=:垂直方向
# axis=:水平方向
# axis=:深度方向
np.concatenate((a,b), axis=)
# 通过axis给定的轴向和拆分的份数对c进行拆分
np.split(c,,axis=)

长度不等的两个数组的组合操作

np.pad(ary, # 原始数组
pad_width=(,), # 补全方式(头部补0个,尾部补1个)
mode='constant', # 设置补全模式
constant_values=-) # 设置补全的默认值为- a = np.arange(,)
a
Out[]: array([, , , ])
# 返回一个新数组
np.pad(a, pad_width=(,),mode='constant',constant_values=-)
Out[]: array([ , , , , -, -, -])

简单的一维数组的组合方案

a = np.arange(,)
b = np.arange(,)
# 垂直方向叠加
np.row_stack((a,b))
# 水平方向叠加
np.column_stack((a,b))

Numpy数组的其他属性

1.shape 维度

2.dtype 元素类型

3.size 元素的个数

4.ndim 维度

5.itemsize 元素字节数

6.nbytes 数组的总字节数

7.real 复数数组的实部

8.imag 复数数组的虚部

9.T 数组对象的转置视图

10.flat 返回数组的扁平迭代器

a = np.arange(,)
a.resize(,,) a.size
Out[]: len(a)
Out[]: a.ndim
Out[]: a.shape
Out[]: (, , ) a.dtype
Out[]: dtype('int32') a.dtype.name
Out[]: 'int32' # ndarray数组的扁平迭代器
for i in a.flat:
print(i)
[e for e in a.flat]

数据分析第三篇:Numpy知识点的更多相关文章

  1. 【Python数据挖掘】第三篇--Numpy 和 可视化

    一.Numpy 数组是一系列同类型数据的集合,可以被非零整数进行索引,可以通过列表进行数组的初始化,数组也可以通过索引进行切片. Numpy提供了几乎全部的科学计算方式. # numpy 导入方式: ...

  2. 数据分析之路 第一篇 numpy

    第一篇 numpy 1.N维数组对象 :ndarray在Python中既然有了列表类型,为啥还要整个数组对象(类型)?那是因为:1.数组对象可以除去元素间运算所需要的循环,使得一维向量更像单个数据2. ...

  3. java面试必备知识点-上中下三篇写的很详细

    参考博客:写的还是相当的经典 http://www.cnblogs.com/absfree/p/5568849.html 上中下三篇写的很详细 http://blog.csdn.net/riverfl ...

  4. python数据挖掘第三篇-垃圾短信文本分类

    数据挖掘第三篇-文本分类 文本分类总体上包括8个步骤.数据探索分析->数据抽取->文本预处理->分词->去除停用词->文本向量化表示->分类器->模型评估.重 ...

  5. Spring第二篇和第三篇的补充【JavaConfig配置、c名称空间、装载集合、JavaConfig与XML组合】

    前言 在写完Spring第二和第三篇后,去读了Spring In Action这本书-发现有知识点要补充,知识点跨越了第二和第三篇,因此专门再开一篇博文来写- 通过java代码配置bean 由于Spr ...

  6. C#多线程编程(4)--异常处理+前三篇的总结

    本来是打算讲并行For和PLINQ的,但是我感觉前三篇我没有讲得很清晰.之前一直在看<CLR via C#>(后文简称CLR)的多线程部分,其中有些部分不是很明白,今天翻开<果壳中的 ...

  7. python数据分析---第04章 NumPy基础:数组和矢量计算

    NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...

  8. spring-cloud-kubernetes背后的三个关键知识点

    在<你好spring-cloud-kubernetes>一文中,对spring-cloud-kubernetes这个SpringCloud官方kubernetes服务框架有了基本了解,今天 ...

  9. 《VueRouter爬坑第三篇》-嵌套路由

    VueRouter系列的文章示例编写时,项目是使用vue-cli脚手架搭建. 项目搭建的步骤和项目目录专门写了一篇文章:点击这里进行传送 后续VueRouter系列的文章的示例编写均基于该项目环境. ...

随机推荐

  1. iOS中文输入法多次触发的问题及解决方案

    最近要在移动端实现一个文本框实时搜索的功能,即在文本框里每输入一个字,就向服务器请求一次搜索结果.暂且不考虑性能优化问题,第一时间想到的是用keyup实现: $('input').on('keyup' ...

  2. iOS UIView添加阴影

    _bottomView.layer.masksToBounds = NO; _bottomView.backgroundColor = [UIColor whiteColor]; _bottomVie ...

  3. [译]NeHe教程 - 创建一个OpenGL窗体

    原文: Setting Up An OpenGL Window 欢迎阅读我的OpenGL教程.我是一个热爱OpenGL的普通码农!我第一次听到OpenGL是在3Dfx刚发布他们给Voodoo I显卡的 ...

  4. u-boot-2014_04在TQ2440上的移植

    本文详细介绍了新版本的u-boot-2014_04在tq2440平台上的移植过程,期间参考了网上的其他移植文档,还有韦东山的移植uboot视频,讲的很好.下面是共享链接,欢迎下载,一同学习.其中有移植 ...

  5. 如何开启Apache Rewrite功能

    一.Ubuntu默认未开启Rewrite支持 apche模块加载工作已分散到不同的配置文件,这样看起来似乎更为合理,管理起来也非常方便.下面看一下如何开启Rewrite模块,当用户需使用301重定向. ...

  6. Crashing Robots - poj 2632

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8352   Accepted: 3613 Description In ...

  7. erlang实现一个进程池 pool

    erlang的实现一个简单的进程池. erlang进程是非常轻量级的,这个进程池的主要目的是用一种通用的方式去管理和限制系统中运行的资源占用.当运行的工作者进程数量达到上限,进程池还可以把任务放到队列 ...

  8. Codeforces Round #392 (Div. 2) F. Geometrical Progression

    原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 se ...

  9. Tomcat startup.bat启动隐藏弹出的信息窗口

    to make tomcat to use javaw.exe instead of java.exe using some startup parameter or environment vari ...

  10. poj1061(extendgcd)

    看完题目后,题目要求: 设时间为t (x+mt)%L = (y+nt)%L ( x-y + (m-n)*t )= k*L (k是整数,可为负) 然后就是经典的 xa+yb=c 求解x,y的经典题目了. ...