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-Day24
12.粘包现象 客户端接收的信息指定了的字节,TCP协议没有丢失协议,只是只能接收指定的字节数,于是产生出了粘包现象 服务端接收命令只能接收1024字节,服务端执行命令结果以后传输给客户端,客户端再以 ...
- POJ 3370 Halloween treats( 鸽巢原理简单题 )
链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...
- COGS——T 1175. [顾研NOIP] 旅游电车
http://www.cogs.pro/cogs/problem/problem.php?pid=1175 ★★☆ 输入文件:buss.in 输出文件:buss.out 简单对比时间限制: ...
- 学一下HDFS,很不错(大数据技术原理及应用)
http://study.163.com/course/courseMain.htm?courseId=1002887002 里面的HDFS这一部分.
- Azure RBAC(Roles Based Access Control)正式上线了
期盼已久的Azure RBAC(Roles Based Access Control)正式上线了. 在非常多情况下.客户须要对各种类型的用户加以区分,以便做出适当的授权决定.基于角色的訪问控制 (RB ...
- scanf使用与运算符
scanf接收输入 #include <stdio.h> #include <stdlib.h> // 接收用户输入的小写字母,输出大写字母 int main() { char ...
- Git 跟 GitHub 是什么关系?
Git 跟 GitHub 是什么关系? 大概就是「魔兽争霸」与「对战平台」的关系吧. git是一个版本控制工具github是一个用git做版本控制的项目托管平台. git是一个版本管理工具,githu ...
- Python 异常(Exception)
1. 字符串为构造函数的参数 >> raise Exception('hyperdirve overload') Exception Traceback (most recent call ...
- 137.CPP自带异常
#include <iostream> #include <exception> using namespace std; //继承自带的异常 class sizeerror ...
- POJ 2189 枚举
题意: n头牛,p长度的道路,问至多包括c头牛的道路最长有多长. 思路: 按照题意暴力就好-- 注意边界. // by SiriusRen #include <cstdio> #inclu ...