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 np
url = '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 1
i, 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.nan
print(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.nan
Method 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中文文档
numpy基础篇-简单入门教程4的更多相关文章
- numpy基础篇-简单入门教程3
np import numpy as np np.__version__ print(np.__version__) # 1.15.2 numpy.arange(start, stop, step, ...
- numpy基础篇-简单入门教程2
import numpy as np Array 数组 print(np.zeros((2, 2))) # [[0. 0.] [0. 0.]] print(np.ones((2, 2))) # [[1 ...
- numpy基础篇-简单入门教程1
np.split(A, 4, axis=1),np.hsplit(A, 4) 分割 A = np.arange(12).reshape((3, 4)) # 水平方向的长度是4 print(np.spl ...
- NumPy简单入门教程
# NumPy简单入门教程 NumPy是Python中的一个运算速度非常快的一个数学库,它非常重视数组.它允许你在Python中进行向量和矩阵计算,并且由于许多底层函数实际上是用C编写的,因此你可以体 ...
- 程序员,一起玩转GitHub版本控制,超简单入门教程 干货2
本GitHub教程旨在能够帮助大家快速入门学习使用GitHub,进行版本控制.帮助大家摆脱命令行工具,简单快速的使用GitHub. 做全栈攻城狮-写代码也要读书,爱全栈,更爱生活. 更多原创教程请关注 ...
- GitHub这么火,程序员你不学学吗? 超简单入门教程 【转载】
本GitHub教程旨在能够帮助大家快速入门学习使用GitHub. 本文章由做全栈攻城狮-写代码也要读书,爱全栈,更爱生活.原创.如有转载,请注明出处. GitHub是什么? GitHub首先是个分布式 ...
- Flyway 简单入门教程
原文地址:Flyway 简单入门教程 博客地址:http://www.extlight.com 一.前言 Flyway 是一款开源的数据库版本管理工具,它更倾向于规约优于配置的方式.Flyway 可以 ...
- .net 开源模板引擎jntemplate 实战演习:基础篇之入门
一.简介 模板引擎是Web开发中非常重要的一环,它负责将页面上的动态内容呈现出最终的结果展现给前端用户,在asp.net mvc中,我们最熟悉的就是Razor了,作为官方的视图引擎(视图引擎不等同于模 ...
- 【ASP.NET 基础】WCF入门教程一(什么是WCF)?
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
随机推荐
- Python基础数据类型list,tuple
列表是有序的可变的元素集合.列表中的每个元素可以使任何数据类型,包括列表本身. 列表生成 Python3中的列表通过定义,for循环,列表推导式等几种方式生成 定义 直接通过中括号`[]`定义一个列表 ...
- python--(常用模块-3-正则表达式)
python--(常用模块-3-正则表达式) 正则表达式是对字符串操作的⼀种逻辑公式. 我们⼀般使⽤正则表达式对字符串进⾏匹 配和过滤. 使⽤正则的优缺点: 优点: 灵活, 功能性强, 逻辑性强. 缺 ...
- 05006_Linux的jdk、mysql、tomcat安装
1.软件包下载链接:软件包下载 密码:advk 2.安装JDK (1)查看当前Linux系统是否已经安装java,输入 rpm -qa | grep java : (2)卸载两个openJDK (3) ...
- AssetBundle打包优化解决方式
第一阶段:AssetBundle出一套解决方式 1.解决如今同一个资源打2个bundle的冗余问题 2.測试验证节省资源的比率是多少 问题拆分 一.bundle反复 问 题 :同样资源拆分问题? ...
- HDU5411CRB and Puzzle(矩阵高速幂)
题目链接:传送门 题意: 一个图有n个顶点.已知邻接矩阵.问点能够反复用长度小于m的路径有多少. 分析: 首先我们知道了邻接矩阵A.那么A^k代表的就是长度为k的路径有多少个. 那么结果就是A^0+A ...
- windows编译ffmpeg出现gcc is unable to create an executable file 的普通情况
近期有个朋友在编译ffmpeg的时候出现这个问题,他非常郁闷. 我就说,为什么我弄的时候就没问题呢??直接./configure +加上后面的參数 安全度过. 然后,我就想了,预计他的gcc的系统变量 ...
- UVA - 10229 Modular Fibonacci 矩阵快速幂
Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...
- chrome控制台常用技巧有哪些
chrome控制台常用技巧有哪些 一.总结 一句话总结:别的里面支持的快捷键,chrome里面几乎都支持,比如sublime中的ctrl+d,其实真是一通百通,都差不多的 1.chrome如何快速切换 ...
- SaltStack介绍——SaltStack是一种新的基础设施管理方法开发软件,简单易部署,可伸缩的足以管理成千上万的服务器,和足够快的速度控制,与他们交流
SaltStack介绍和架构解析 简介 SaltStack是一种新的基础设施管理方法开发软件,简单易部署,可伸缩的足以管理成千上万的服务器,和足够快的速度控制,与他们交流,以毫秒为单位.SaltSta ...
- 自己定义CNN网络模型并使用caffe训练
caffe自带的例子中对mnist手写体数字训练使用的卷积神经网络是在lenet_train_test.prototxt中定义的,隐含层包含了2个卷积层,2个池化层,2个全连接层,1个激活函数层.网络 ...