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的更多相关文章

  1. numpy基础篇-简单入门教程3

    np import numpy as np np.__version__ print(np.__version__) # 1.15.2 numpy.arange(start, stop, step, ...

  2. numpy基础篇-简单入门教程2

    import numpy as np Array 数组 print(np.zeros((2, 2))) # [[0. 0.] [0. 0.]] print(np.ones((2, 2))) # [[1 ...

  3. numpy基础篇-简单入门教程1

    np.split(A, 4, axis=1),np.hsplit(A, 4) 分割 A = np.arange(12).reshape((3, 4)) # 水平方向的长度是4 print(np.spl ...

  4. NumPy简单入门教程

    # NumPy简单入门教程 NumPy是Python中的一个运算速度非常快的一个数学库,它非常重视数组.它允许你在Python中进行向量和矩阵计算,并且由于许多底层函数实际上是用C编写的,因此你可以体 ...

  5. 程序员,一起玩转GitHub版本控制,超简单入门教程 干货2

    本GitHub教程旨在能够帮助大家快速入门学习使用GitHub,进行版本控制.帮助大家摆脱命令行工具,简单快速的使用GitHub. 做全栈攻城狮-写代码也要读书,爱全栈,更爱生活. 更多原创教程请关注 ...

  6. GitHub这么火,程序员你不学学吗? 超简单入门教程 【转载】

    本GitHub教程旨在能够帮助大家快速入门学习使用GitHub. 本文章由做全栈攻城狮-写代码也要读书,爱全栈,更爱生活.原创.如有转载,请注明出处. GitHub是什么? GitHub首先是个分布式 ...

  7. Flyway 简单入门教程

    原文地址:Flyway 简单入门教程 博客地址:http://www.extlight.com 一.前言 Flyway 是一款开源的数据库版本管理工具,它更倾向于规约优于配置的方式.Flyway 可以 ...

  8. .net 开源模板引擎jntemplate 实战演习:基础篇之入门

    一.简介 模板引擎是Web开发中非常重要的一环,它负责将页面上的动态内容呈现出最终的结果展现给前端用户,在asp.net mvc中,我们最熟悉的就是Razor了,作为官方的视图引擎(视图引擎不等同于模 ...

  9. 【ASP.NET 基础】WCF入门教程一(什么是WCF)?

    一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...

随机推荐

  1. JSP获取json格式的数据报错 Uncaught SyntaxError: Unexpected identifier

    后台json字符串是 {"id":"cmdb_ci.`fully_qualified_domain_name`","field":" ...

  2. 关于samsung连接BLE设备的一些资料汇总和开发过程一些经验总结

    1 忙了这么久,终于有时间把最近几个月弄的东西整理一下,顺便我的开发过程和经历. 被公司分到做一个蓝牙4.0的项目,对这种软硬结合的东西也比较感兴趣,所以很快投入到android蓝牙4.0的项目中来. ...

  3. 论文阅读《End-to-End Learning of Geometry and Context for Deep Stereo Regression》

    端到端学习几何和背景的深度立体回归 摘要     本文提出一种新型的深度学习网络,用于从一对矫正过的立体图像回归得到其对应的视差图.我们利用问题(对象)的几何知识,形成一个使用深度特征表示的代价量(c ...

  4. SQL SERVER-union

    UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每个 SELECT 语句中 ...

  5. POJ 1715

    同样是确定某位上的数,当确定某一位后,其后面的排列数是确定的,所以可以用除法和取余数的方法来确定这一位的值 #include <iostream> #include <cstdio& ...

  6. Scratch单机版下载

    Scratch单机版下载 这两个地址速度比较快: Adobe Air:http://7dx.pc6.com/wwb5/AdobeAIR2800127.zip Scratch :http://7dx.p ...

  7. 手写一个节点大小平衡树(SBT)模板,留着用

    看了一下午,感觉有了些了解.应该没有错,有错希望斧正,感谢 #include<stdio.h> #include<string.h> struct s { int key,le ...

  8. Java 递归、尾递归、非递归 处理阶乘问题

    n!=n*(n-1)! import java.io.BufferedReader; import java.io.InputStreamReader; /** * n的阶乘,即n! (n*(n-1) ...

  9. Bmob移动后端云服务平台--Android从零開始--(二)android高速入门

    Bmob移动后端云服务平台--Android从零開始--(二)android高速入门 上一篇博文我们简介何为Bmob移动后端服务平台,以及其相关功能和优势. 本文将利用Bmob高速实现简单样例,进一步 ...

  10. WinForm容器内控件批量效验是否同意为空?设置是否仅仅读?设置是否可用等方法分享

    WinForm容器内控件批量效验是否同意为空?设置是否仅仅读?设置是否可用等方法分享 在WinForm程序中,我们有时须要对某容器内的全部控件做批量操作.如批量推断是否同意为空?批量设置为仅仅读.批量 ...