numpy基础篇-简单入门教程4
numpy基础篇-简单入门教程4
np.set_printoptions(precision=3),只显示小数点后三位
np.random.seed(100)rand_arr = np.random.random([2, 2])
np.set_printoptions(suppress=True, precision=3) # 设置为可使用科学计数法
print(rand_arr) # [[0.54340494 0.27836939] [0.42451759 0.84477613]]np.set_printoptions(suppress=False) # 设置为不使用科学计数法
rand_arr = rand_arr/1e10 # 强制转成科学计数法表示。通过除以科学技术实现
print(rand_arr) # [[5.43404942e-11 2.78369385e-11] [4.24517591e-11 8.44776132e-11]]np.set_printoptions(threshold=6) # 设置只显示6个数据
np.set_printoptions(threshold=np.nan) # 设置显示所有的数据
import numpy as npurl = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
#### import a dataset with numbers and texts
iris = np.genfromtxt(url, delimiter=',', dtype='object')
iris_1d = np.genfromtxt(url, delimiter=',', dtype=None)
names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')print(iris[:3])
print(iris.shape) # (150, 5)print(iris_1d[:3])
print(iris_1d.shape) # (150,)
How to extract a particular column from 1D array of tuples?
species = np.array([row[4] for row in iris_1d])
print(species[:2]) # [b'Iris-setosa' b'Iris-setosa']
How to convert a 1d array of tuples to a 2d numpy array?
Method 1: Convert each row to a list and get the first 4 items
iris_2d = np.array([row.tolist()[:] for row in iris_1d])
print(iris_2d[:4])
Alt Method 2: Import only the first 4 columns from source url
iris_2d = np.genfromtxt(url, delimiter=',', usecols=[0, 1, 2, 3])
print(iris_2d[:4])
How to compute the mean, median, standard deviation of a numpy array?
sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])
mu, med, sd = np.mean(sepallength), np.median(sepallength), np.std(sepallength)
print(mu, med, sd)
How to normalize an array so the values range exactly between 0 and 1?
Smax, Smin = sepallength.max(), sepallength.min()
S = (sepallength - Smin) / (Smax - Smin)
# or
S = (sepallength - Smin) / sepallength.ptp()
print(S[:4])
30. How to compute the softmax score?
def softmax(x):e_x = np.exp(x - np.max(x)) # ???????????return e_x / e_x.sum(axis=0)print(softmax(sepallength[:3]))
How to find the percentile scores of a numpy array?
print(np.percentile(sepallength, q=[5, 95]))
How to insert values at random positions in an array?
iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')
print(np.shape(iris_2d)) # (150, 5)# Method 1i, j = np.where(iris_2d)
np.random.seed(200)
iris_2d[np.random.choice((i), 20), np.random.choice((j), 20)] = np.nan
print(iris_2d[:4])
> [[b'5.1' b'3.5' b'1.4' b'0.2' b'Iris-setosa']
> [b'4.9' b'3.0' nan b'0.2' b'Iris-setosa']
> [b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']
> [b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']]# Method 2
np.random.seed(100)
iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan
print(iris_2d[:4])
# [[b'5.1' b'3.5' b'1.4' b'0.2' b'Iris-setosa']
# [b'4.9' b'3.0' nan b'0.2' b'Iris-setosa']
# [b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']
# [b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']]
How to filter a numpy array based on two or more conditions?
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0, 1, 2, 3])
iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nanprint(np.isnan(iris_2d[:, 0]).sum()) # 5
print(np.where(np.isnan(iris_2d[:, 0]))) # (array([ 38, 80, 106, 113, 121]),)
How to filter a numpy array based on two or more conditions?
Q. Filter the rows of iris_2d that has petallength (3rd column) > 1.5 and sepallength (1st column) < 5.0
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0, 1, 2, 3])conditon = (iris_2d[:, 2] < 1.5) & (iris_2d[:, 0] < 5.0)
print(iris_2d[conditon][:4])
35. How to drop rows that contain a missing value from a numpy array?
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0, 1, 2, 3])
iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nanMethod 1:
any_nan_in_row = np.array([~np.any(np.isnan(row)) for row in iris_2d])
print(iris_2d[any_nan_in_row][:5])Methond 2:
print(iris_2d[np.sum(np.isnan(iris_2d), axis=1) == 0][:5])
numpy英文文档
numpy中文文档
```
posted @ 2019-02-25 19:05 YangZhaonan 阅读(...) 评论(...) 编辑 收藏
numpy基础篇-简单入门教程4相关推荐
- Git快速入门篇—— Windows版本淘宝镜像快速下载安装详细步骤及简单入门教程(附带图文教程)
Git快速入门篇-- Windows版本淘宝镜像快速下载安装详细步骤及简单入门教程(附带图文教程) 前言:我们平时在整理代码的时候,尤其是与别人一起开发项目的时候,常常涉及到代码的更新,因此代码版本问 ...
- uni-ui简单入门教程 - 如何用HBuilderX为uni-app项目启用uni-ui扩展组件?
须知 uni-app是一个前端框架 简单来说,uni-app的组件,类似HTML的标签,例如a转navigation.span转text等 uni-app的组件包括 基础组件 (自带免安装) + 扩展 ...
- Proteus简单入门教程以及使用Proteus仿真STM32F103单片机和Arduino单片机
工欲善其事必先利其器,有条件的朋友直接使用开发板学习即可,但有时候手边没有实物开发板可以用,那么可以借助一些仿真软件运行我们的程序,Proteus算是使用的比较多的一种仿真软件,我们使用它来做STM3 ...
- python零基础入门教程视频下载-零基础学Python入门教程,视频资源下载
课程名称 零基础学Python入门教程,视频资源下载 课程目录 第一章 :Python介绍和安装 01.Python语言的特点 02.Python的发展历史与版本 03.Python的安装 第二章 : ...
- vue 单相绑定_Vuejs第一篇之入门教程详解(单向绑定、双向绑定、列表渲染、响应函数)...
Vuejs第一篇之入门教程详解(单向绑定.双向绑定.列表渲染.响应函数) 2018-12-30 什么是组件? 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素 ...
- 计算机语言中的逻辑型数据,零基础易语言入门教程(五)之逻辑型数据类型
在上篇文章给大家介绍了零基础易语言入门教程(四)之数据类型,上篇针对数值到文本类型知识,今天给大家介绍下逻辑型数据. 具体方法和步骤如下所示: 1.逻辑型数据非真即假: 首先申请一个局部变量(A)类型 ...
- emacs 自带的简单入门教程
emacs 自带的教程是 英文版和繁体中文版,下面的内容是利用在线繁体转简单工具生成 有些地方翻译的不精准,凑和看 在emacs 中按下 Ctrl-h t 或者F1 t即可打开自带的此文档 原文:em ...
- 技术图文:NumPy 的简单入门教程
背景 这段时间,LSGO软件技术团队正在组织 "机器学习实战刻意练习"活动,这个活动是"Python基础刻意练习"活动的升级,是对学员们技术的更深层次的打磨.在 ...
- python tornado教程_Tornado 简单入门教程(零)——准备工作
前言: 这两天在学着用Python + Tornado +MongoDB来做Web开发(哈哈哈这个词好高端).学的过程中查阅了无数资料,也收获了一些经验,所以希望总结出一份简易入门教程供初学者参考.完 ...
最新文章
- 字符串php手册,php知识点复习之字符串
- 信息学奥赛C++语言:蛋糕盒子
- centos查看磁盘转速_Linux 磁盘管理
- 为什么软件开发这么难?
- MongoDB Sharding 机制分析
- fms +fme 视频直播
- python最小特征值_阿里巴巴举荐,Python视频,免费分享,用python求解特征向量和拉普拉斯矩阵...
- python安装grpcio的心路历程
- windows下搭建自己的跨平台tts语音合成播报技术
- C# winform 魔兽MH全图制作教程(3):魔兽1.20E.1.24B.1.24E全图内存地址 转自breeze...
- 一年级下册计算机教学计划,人教版一年级数学下册教学计划
- 幼麟棋牌创建房间简短分析
- 低效率只因环境太乱?43 个方法帮你减少干扰
- ROS常用局部路径规划算法比较
- 5分钟读懂UML类图
- INFO zkclient.ZkEventThread - Starting ZkClient
- 理解matplotlib、pylab与pyplot之间的关系
- SOUI自定义控件(1)
- png背景变黑原理解析
- win2008 r2 配置程序office访问权限