[Python] Scipy and Numpy(1)
import numpy as np #Create an array of 1*10^7 elements
arr = np.arange(1e7) #Converting ndarray to list
larr = arr.tolist() #Create a 2D numpy array
arr = np.zeros((3,3)) #Converting a array to matrix
mat = np.matrix(arr)
np.matrix('1,2,3;4,5,6;7,8,9'); #Array Creation
#First we create a list and then
#wrap it with the np.array() function
alist = [1,2,3]
arr = np.array(alist) #Creating an array of zeros with 5 elements
arr = np.zeros(5) #Creating an array going from 0 to 100
#not include 100
arr = np.arange(100) #from 10 to 100 (not include 100)
arr = np.arange(10, 100) #100 steps form 1 to 100
#(start, end, step)
arr = np.linspace(0, 1, 100) #Creating an 5X5 array of zeros
image = np.zeros((5,5)) #Creating a 5X5X5 cube of 1's
#The astype() method sets the array with integer elements
cube = np.zeros(5,5,5).astype(int) + 1 #Or even simpler with 16-bit floating-point precision
cube = np.ones((5,5,5)).astype(np.float16) #Change Data type
#Use dtype: int numpy.float16, numpy.float32, numpy.float64
arr = np.zeros(2, dtype=int)
arr = np.zeros(2, dtype=np.float32) '''
The restructured arrays are just different views
of the same data in memory.
If chang one of them, you will change all.
If you don't want this to happen, then use the numpy.copy function
to separete the arrays mamory-wise.
'''
#Created arrays and reshape them in many others ways
#Creating an array with elements from 0 to 999
arr1d = np.arange(1000) #reshaping the array to a 10x10x10 3D array
arr3d = arr1d.reshape((10,10,10))
arr3d = np.reshape(arr1d, (10,10,10)) #Invesely, we can flatten arrays
arr4d = np.zeros((10,10,10,10))
arr1d = arr4d.ravel()
print arr1d.shape recarr = np.zeros((2,), dtype('i4, f4, a10'))
#the type for the first to third columns
#i4 := 32-bit integer
#f4 := 32-bit float
#a10 := a string 10 characters long #We can assign names to each column
recarr.dtype.names = ('Integers', 'Floats', 'Strings') #Indexing and Slicing
alist = [[1,2],[3,4]]
arr = np.array(alist)
arr[0,1]#It's the same as arr[0][1]
arr[:,1]#return the last column
arr[1,:]#return the bottom row
[Python] Scipy and Numpy(1)的更多相关文章
- python数值计算模块NumPy scipy安装
NumPy为Python提供了快速的多维数组处理的能力,而SciPy则在NumPy基础上添加了众多的科学计算所需的各种工具包,有了这两个库,Python就有几乎和Matlab一样的处理数据和计算的能力 ...
- Python中的Numpy、SciPy、MatPlotLib安装与配置
Python安装完Numpy,SciPy和MatplotLib后,可以成为非常犀利的科研利器.网上关于这三个库的安装都写得非常不错,但是大部分人遇到的问题并不是如何安装,而是安装好后因为配置不当,在使 ...
- 给深度学习入门者的Python快速教程 - numpy和Matplotlib篇
始终无法有效把word排版好的粘贴过来,排版更佳版本请见知乎文章: https://zhuanlan.zhihu.com/p/24309547 实在搞不定博客园的排版,排版更佳的版本在: 给深度学习入 ...
- 使用python scipy.optimize linprog和lingo线性规划求解最大值,最小值(运筹学学习笔记)
1.线性规划模型: 2.使用python scipy.optimize linprog求解模型最优解: 在这里我们用到scipy中的linprog进行求解,linprog的用法见https://doc ...
- python及pandas,numpy等知识点技巧点学习笔记
python和java,.net,php web平台交互最好使用web通信方式,不要使用Jypython,IronPython,这样的好处是能够保持程序模块化,解耦性好 python允许使用'''.. ...
- SciPy和Numpy处理能力
1.SciPy和Numpy的处理能力: numpy的处理能力包括: a powerful N-dimensional array object N维数组: advanced array slicing ...
- Windows下安装Scipy和Numpy失败的解决方案
使用 pip 安装 Scipy 库时,经常会遇到安装失败的问题 pip install numpy pip install scipy 后来网上搜寻了一番才得以解决.scipy 库需要依赖 numpy ...
- Python 机器学习库 NumPy 教程
0 Numpy简单介绍 Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy.matplotlib一起使用.其实,list已经提供了类似于矩阵的表示形式,不过numpy ...
- python scipy样条插值函数大全(interpolate里interpld函数)
scipy样条插值 scipy样条插值1.样条插值法是一种以可变样条来作出一条经过一系列点的光滑曲线的数学方法.插值样条是由一些多项式组成的,每一个多项式都是由相邻的两个数据点决定的,这样,任意的两个 ...
随机推荐
- python3 线性同余发生器 ( random 随机数生成器 ) 伪随机数产生周期的一些探究
import random x=[str(random.randint(0, 5)) for i in range(10)] x_str=''.join(x) y=[str(random.randin ...
- [LeetCode&Python] Problem 807. Max Increase to Keep City Skyline
In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...
- 很全面的WinRAR实用技巧系列 - imsoft.cnblogs
WinRAR也可以管理我的桌面时间长了,桌面上堆的东西实在太多,平时该如何管理呢?安装了WinRAR的朋友可以请它来帮忙,用它管理清除无用的桌面文件或图标. 以XP系统为例,系统所在目录是“c:\wi ...
- LOJ2542. 「PKUWC2018」随机游走【概率期望DP+Min-Max容斥(最值反演)】
题面 思路 我们可以把到每个点的期望步数算出来取max?但是直接算显然是不行的 那就可以用Min-Max来容斥一下 设\(g_{s}\)是从x到s中任意一个点的最小步数 设\(f_{s}\)是从x到s ...
- 51Nod:独木舟问题(贪心)
n个人,已知每个人体重,独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? 输入 第一行包含两个正 ...
- .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
.NET 中的 async / await 写异步代码用起来真的很爽,就像写同步一样.我们可以在各种各样的异步代码中看到 Task 返回值,这样大家便可以使用 await 等待这个方法.不过,有时需要 ...
- Flask第31课——include标签
我们在上一节代码基础上增加一些代码,样式: 文件名index.html,代码: {% from 'macros/forms.html' import input %} <!DOCTYPE htm ...
- stenciljs 学习十二 官方doc 路由使用的例子
路由在单页面应用开发中是一个相对比较重要的位置 以下为官方网站的路由配置 <stencil-router scrollTopOffset={0}> <stencil-route-sw ...
- [NN] 对于BackPropagation(BP, 误差反向传播)的一些理解
本文大量参照 David E. Rumelhart, Geoffrey E. Hinton and Ronald J. Williams, Learning representation by bac ...
- Tcp 编程 的时代 已经到来
这是 一个 网友 在 Github 上的 项目 SmartRoute : https://github.com/IKende/SmartRoute ZooKeeper 和 Consul 的 “投票 ...