numpy---one
import numpy as np #创建数组(给array函数传递Python序列对象)
a = np.array([1,2,3,4,5])
b = np.array((1,2,3,4,5,6))
c = np.array([ [1,2,3,4,5], [6,7,8,9,10] ]) #数组的大小用shape属性获得
print(type(a), a.shape, a, '\n')
print(type(b), b.shape, b,'\n')
print(type(c),c.shape, c,'\n') #改变数组的shape属性,改变自身元素排列
c.shape = 2, 5
print(c.shape, c) c.shape = 10, -1
print(c.shape, c) #通过reshape改变数组排序,赋值给新数组,但是共享同一块内存
d = b.reshape((2,3))
print(d.shape, d)
b[1]=100
print(b,d) 输出:
<class 'numpy.ndarray'> (5,) [1 2 3 4 5]
<class 'numpy.ndarray'> (6,) [1 2 3 4 5 6]
<class 'numpy.ndarray'> (2, 5) [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
(2, 5) [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
(10, 1) [[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]]
(2, 3) [[1 2 3]
[4 5 6]]
[ 1 100 3 4 5 6] [[ 1 100 3]
[ 4 5 6]]
import numpy as np #创建数组(通过numpy函数)
a = np.arange(0, 1, 0.1) #不包括终值
b = np.linspace(0, 1, 10) #包括终值,等差10个数
c = np.logspace(0, 2, 10) #从1到100,等比10个数 s = "abcdef"
d = np.fromstring(s, dtype=np.int8)
e = np.fromstring(s, dtype=np.int16)
print(a,'\n',b,'\n',c,'\n',d,'\n',e,'\n')
输出:
[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
[ 0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
0.66666667 0.77777778 0.88888889 1. ]
[ 1. 1.66810054 2.7825594 4.64158883 7.74263683
12.91549665 21.5443469 35.93813664 59.94842503 100. ]
[ 97 98 99 100 101 102]
[25185 25699 26213]
import numpy as np #创建10个元素的一维数组
def func(i):
return i%4+1 print ( np.fromfunction(func,(10,)) )
输出:
[ 1. 2. 3. 4. 1. 2. 3. 4. 1. 2.]
import numpy as np def func(i,j):
return (i + 1) * (j + 1) print(np.fromfunction(func, (9,9)))
输出:
[[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 2. 4. 6. 8. 10. 12. 14. 16. 18.]
[ 3. 6. 9. 12. 15. 18. 21. 24. 27.]
[ 4. 8. 12. 16. 20. 24. 28. 32. 36.]
[ 5. 10. 15. 20. 25. 30. 35. 40. 45.]
[ 6. 12. 18. 24. 30. 36. 42. 48. 54.]
[ 7. 14. 21. 28. 35. 42. 49. 56. 63.]
[ 8. 16. 24. 32. 40. 48. 56. 64. 72.]
[ 9. 18. 27. 36. 45. 54. 63. 72. 81.]]
ndim:维度,shape:(行数,列数),size:元素总个数 dtype:指定数据类型
# -*- coding: utf-8 -*-
import numpy as np matrix = np.array([[1,2,3], [4,5,6]]) #矩阵
print("dim; ",matrix.ndim)
print("shape: ",matrix.shape)
print("size: ",matrix.size) list1 = np.array([1,2,3,4],dtype=np.int32)
print("list1 dtype: ",list1.dtype) list2 = np.array([1,2,3,4])
print("list2 dtype: ",list2.dtype) list3 = np.array([1,2,3,4],dtype=np.float)
print("list3 dtype: ",list3.dtype) list4 = np.array([1,2,3,4],dtype=np.float32)
print("list4 dtype: ",list4.dtype) list5 = np.ones((3,4),dtype=np.int)
print("list5: ",list5) list6 = np.empty((3,4))
print("list6: ",list6) list7 = np.arange(5,15).reshape((2,5))
print("list7: ",list7) list8 = np.linspace(1,11,10)
print("list8: ",list8)
输出;
dim; 2
shape: (2, 3)
size: 6
list1 dtype: int32
list2 dtype: int32
list3 dtype: float64
list4 dtype: float32
list5: [[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
list6: [[ 6.95332630e-310 1.69118108e-306 2.04722549e-306 1.29061142e-306]
[ 2.22522597e-306 1.33511969e-306 1.29061753e-306 1.11261027e-306]
[ 9.34609790e-307 1.11260619e-306 1.42410974e-306 8.34449381e-308]]
list7: [[ 5 6 7 8 9]
[10 11 12 13 14]]
list8: [ 1. 2.11111111 3.22222222 4.33333333 5.44444444
6.55555556 7.66666667 8.77777778 9.88888889 11. ]
# -*- coding: utf-8 -*-
import numpy as np a = np.arange(5)
b = np.array([1,2,3,4,5]) print("a: ",a)
print("b: ",b)
addc = a + b
print("add: ", addc) minusc = a -b
print("minus: ",minusc) timec = a * b
print("times: ",timec) squc = a**2
print("square: ",squc) sinc = 10 * np.sin(a)
print("sin: ",sinc) print("compare: ",a<3) matrix1 = np.array([[1,2,3,4],[5,6,7,8]])
matrix2 = np.arange(8).reshape((4,2))
print("matrix *: ",np.dot(matrix1,matrix2))
print("matrix *",matrix1.dot(matrix2)) suiji = np.random.random((2,4))
print("suiji: ",suiji)
print("max: ",np.max(suiji))
print("min: ",np.min(suiji))
print("sum: ",np.sum(suiji))
print("col: ",np.min(suiji,axis=0))
print("row: ",np.max(suiji,axis=1))
a: [0 1 2 3 4]
b: [1 2 3 4 5]
add: [1 3 5 7 9]
minus: [-1 -1 -1 -1 -1]
times: [ 0 2 6 12 20]
square: [ 0 1 4 9 16]
sin: [ 0. 8.41470985 9.09297427 1.41120008 -7.56802495]
compare: [ True True True False False]
matrix *: [[ 40 50]
[ 88 114]]
matrix * [[ 40 50]
[ 88 114]]
suiji: [[ 0.79302826 0.02704441 0.19401082 0.02216562]
[ 0.66149996 0.77353779 0.66565688 0.53205038]]
max: 0.793028259974
min: 0.0221656169264
sum: 3.66899411306
col: [ 0.66149996 0.02704441 0.19401082 0.02216562]
row: [ 0.79302826 0.77353779]
numpy---one的更多相关文章
- 利用Python进行数据分析(5) NumPy基础: ndarray索引和切片
概念理解 索引即通过一个无符号整数值获取数组里的值. 切片即对数组里某个片段的描述. 一维数组 一维数组的索引 一维数组的索引和Python列表的功能类似: 一维数组的切片 一维数组的切片语法格式为a ...
- 利用Python进行数据分析(4) NumPy基础: ndarray简单介绍
一.NumPy 是什么 NumPy 是 Python 科学计算的基础包,它专为进行严格的数字处理而产生.在之前的随笔里已有更加详细的介绍,这里不再赘述. 利用 Python 进行数据分析(一)简单介绍 ...
- 利用Python进行数据分析(6) NumPy基础: 矢量计算
矢量化指的是用数组表达式代替循环来操作数组里的每个元素. NumPy提供的通用函数(既ufunc函数)是一种对ndarray中的数据进行元素级别运算的函数. 例如,square函数计算各元素的平方,r ...
- python安装numpy、scipy和matplotlib等whl包的方法
最近装了python和PyCharm开发环境,但是在安装numpy和matplotlib等包时出现了问题,现总结一下在windows平台下的安装方法. 由于现在找不到了工具包新版本的exe文件,所以采 ...
- 深入理解numpy
一.为啥需要numpy python虽然说注重优雅简洁,但它终究是需要考虑效率的.别说运行速度不是瓶颈,在科学计算中运行速度就是瓶颈. python的列表,跟java一样,其实只是一维列表.一维列表相 ...
- Python Numpy,Pandas基础笔记
Numpy Numpy是python的一个库.支持维度数组与矩阵计算并提供大量的数学函数库. arr = np.array([[1.2,1.3,1.4],[1.5,1.6,1.7]])#创建ndarr ...
- broadcasting Theano vs. Numpy
broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...
- python之numpy
一.矩阵的拼接合并 列拼接:np.column_stack() >>> import numpy as np >>> a = np.arange(9).reshap ...
- win7系统下python安装numpy,matplotlib,scipy和scikit-learn
1.安装numpy,matplotlib,scipy和scikit-learn win7系统下直接采用pip或者下载源文件进行安装numpy,matplotlib,scipy时会遇到各种问题,这是因为 ...
- 给numpy矩阵添加一列
问题的定义: 首先我们有一个数据是一个mn的numpy矩阵现在我们希望能够进行给他加上一列变成一个m(n+1)的矩阵 import numpy as np a = np.array([[1,2,3], ...
随机推荐
- 浅谈Log4j2日志框架及使用
目录 1.日志框架 2.为什么需要日志接口,直接使用具体的实现不就行了吗? 3.log4j2日志级别 4.log4j2配置文件的优先级 5.对于log4j2配置文件的理解 6.对于Appender的理 ...
- 【做题】agc008f - Black Radius——计数&讨论&思维
原文链接 https://www.cnblogs.com/cly-none/p/9794411.html \[ \newcommand{\stif}[2]{\left[ \begin{matrix} ...
- office完全卸载
第一步:先暂停office服务,再通过 控制面板--卸载程序 --卸载office应用 第二步:通过office_move(自己命名的工具)软件卸载 工具分享:https://pan.baidu. ...
- dfs序七个经典问题
update-2018.07.23: 原文问题五思路描述有误,已更正. 参考自:<数据结构漫谈>-许昊然 dfs序是树在dfs先序遍历时的序列,将树形结构转化成序列问题处理. dfs有一个 ...
- P4822 [BJWC2012]冻结
思路 和p4568类似的分层图最短路 从上一层向下一层连边权/2的边即可 代码 #include <cstdio> #include <algorithm> #include ...
- (转载)C#语言开发规范
1. 命名规范a) 类[规则1-1]使用Pascal规则命名类名,即首字母要大写.eg:Class Test{...}[规则1-2]使用能够反映类功能的名词或名词短语命名类.[规则1-3]不要使用“ ...
- 剥开比原看代码11:比原是如何通过接口/create-account创建帐户的
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- 精通正则表达式(第三版)—Mastering Regular Expressions,3rd Edition—读书笔记2
1.肯定断言:必须匹配一个字符 排除型字符组:匹配未列出字符的字符组 2.范围表示法——列出范围内所有的字符 大多数情况下,不会影响执行速度.但是,某些实现方式不能完全优化字符组.所以,最好是有范围表 ...
- 《算法竞赛入门经典》刘汝佳 C语言部分(前四章)“注解与习题” 之思索 -<1>
此书我购于去年的十一月份,也是经前人推荐购买的一本比较有用的书籍,在寒假自学此书,其简洁清晰高效的示例代码令我印象深刻,于是我打算把这本书的前四章后面的注解与习题(未给出标准解答)认真的去思索和研究, ...
- VHDL 例程
以下程序未经仿真,仅供说明 语法 声明参考库ieee,使用ieee中的std_logic_1164包全部条目可见 library ieee; use ieee.std_logic_1164.all; ...