import numpy as np
功能 | 函数 |
---|---|
设置打印输出的精度 | np.set_printoptions(precision=4) |
>>> X = np.random.randn(5, 3)
>>> ops = [np.argmax, np.argmin]
>>> np.asarray([op(X, 1) for op in ops])
[[1 2 2 2 2]
[2 0 1 1 1]]
两个向量的拼接 例如两个长度为n的一维向量,拼接为2×n的矩阵,可以使用np.vstack(),
>>> x, y = np.ones(3), np.zeros(3)
>>> np.vstack((x, y))
array([[ 1.,1.,1.],
[ 0.,0.,0.]])
【numpy实用技巧(一)】也可使用作为np.array()构造函数的参数,进行创建:
>>> np.array([x, y])
array([[ 1.,1.,1.],
[ 0.,0.,0.]])
xx, yy = np.meshgrid(np.arange(, , ), np.arange(, , ))
z = clf.predict(np.array([xx.ravel(), yy.ravel()]).T)
z = z.reshape(xx.shape)
plt.contoutf(xx, yy, z, alpha=.4, cmap=cmap)
numpy.ndarray的遍历
>>> X = np.random.randn(3, 3)
array([[-0.24882132, -0.32389773, -0.96069467],
[ 1.26331248,1.59089579, -0.97145676],
[-0.03989954,0.28587614,0.04657364]])>>> for i in X:
print(i)
[-0.24882132 -0.32389773 -0.96069467]
[ 1.263312481.59089579 -0.97145676]
[-0.039899540.285876140.04657364]>>> for i in X:
for j in i:
print j-0.248821323982
-0.32389773407
-0.960694672326
1.26331248229
1.59089578902
-0.971456755866
-0.0398995441063
0.285876139182
0.0465736443469
numpy.ndarray()类型转换 有时编译器会报如下的错误:
TypeError: Cannot cast array data from dtype(‘float64’) to
dtype(‘int32’) according to the rule ‘safe’
>>> print(np.bincount(y_train))TypeError: Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'safe'
如果我们使用python基本模块下的强制类型转换,又会提示如下的错误:
>>> print(np.bincount(int(y_train)))TypeError: only length-1 arrays can be converted to Python scalars
此时可以使用x.astype(type)成员
>>> print(np.bincount(y_train.astype(np.int32))
二维数组的逆序 列向的逆序 A[:, -1::-1]:
>>> np.set_printoptions(precision=4)
>>> A = np.random.randn(3, 3)
>>> A
array([[ 1.2381, -0.2428, -0.4687],
[-1.0588,0.0432,0.9937],
[ 0.2708,1.4833,0.2697]])>>> A[:, -1::-1]
array([[-0.4687, -0.2428,1.2381],
[ 0.9937,0.0432, -1.0588],
[ 0.2697,1.4833,0.2708]])
行向的逆序:A[-1::-1, :]
>>> A[-1::-1, :]
array([[ 0.2708,1.4833,0.2697],
[-1.0588,0.0432,0.9937],
[ 1.2381, -0.2428, -0.4687]])
references basics types in Numpy
推荐阅读
- 推荐系统论文进阶|CTR预估 论文精读(十一)--Deep Interest Evolution Network(DIEN)
- Python专栏|数据分析的常规流程
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置
- Python绘制小红花
- Pytorch学习|sklearn-SVM 模型保存、交叉验证与网格搜索
- OpenCV|OpenCV-Python实战(18)——深度学习简介与入门示例
- python|8. 文件系统——文件的删除、移动、复制过程以及链接文件
- 爬虫|若想拿下爬虫大单,怎能不会逆向爬虫,价值过万的逆向爬虫教程限时分享
- 分布式|《Python3网络爬虫开发实战(第二版)》内容介绍
- java|微软认真聆听了开源 .NET 开发社区的炮轰( 通过CLI 支持 Hot Reload 功能)