1. IDE说明

  所有的案例用Anacoda中的Jupiter工具进行交互式讲解。

2. 版本和安装

  NumPy从如下网站安装:http://sourceforge.net/projects/numpy/files。

  我们通过环境查看相关的版本。如果电脑上安装了Anaconda的话这些版本基本上都是最新版本的。

如果Anaconda的库不是最新的可以通过Prompt进行安装和更新。可以参照如下博客,非常简单。https://blog.csdn.net/xiexu911/article/details/80282440

3. 我们通过Anaconda打开Jupiter或spyder打开进行讲解

4. 第一个简单操作:通过对比Python和NumPy的计算观察NumPy的运算速度

from datetime import datetime
import numpy as np # 纯Python写的程序
def pythonsum(n):
a = []
b = []
c = [] for i in range(n):
a.append(i)
b.append(i)
c.append(a[i]**2 + b[i]**3)
return c # NumPy写的程序
def numpysum(n):
a = np.arange(n,dtype=object) ** 2
b = np.arange(n,dtype=object) ** 3
c = a + b
return c # 进行比较测试
size = 30000 start = datetime.now()
c1 = pythonsum(size)
delta = datetime.now()-start
print("The last 2 elements of the sum",c1[-2:])
print("PythonSum elapsed time in microsecond",delta.microseconds) start = datetime.now()
c2 = numpysum(size)
delta = datetime.now()-start
print("The last 2 elements of the sum",c2[-2:])
print("NumPySum elapsed time in microsecond",delta.microseconds) # 10000的情况下:
#The last 2 elements of the sum [999500079996, 999800010000]
#PythonSum elapsed time in microsecond 15625
#The last 2 elements of the sum [999500079996 999800010000]
#NumPySum elapsed time in microsecond 15623 # 20000的情况下:
#The last 2 elements of the sum [7998000159996, 7999200020000]
#PythonSum elapsed time in microsecond 31247
#The last 2 elements of the sum [7998000159996 7999200020000]
#NumPySum elapsed time in microsecond 0 # 30000的情况下:
#The last 2 elements of the sum [26995500239996, 26998200030000]
#PythonSum elapsed time in microsecond 46871
#The last 2 elements of the sum [26995500239996 26998200030000]
#NumPySum elapsed time in microsecond 0

  我们发现越是数据大NumPy的优势就能够体现出来了。注意我们用NumPy的时候规定dtype = object是为了放置数组的溢出,这个在很多教材中都没有提及。如果不写,在数值过大的时候,数组会产生溢出,导致计算的记过不一样。

第二个简单操作:通过help查看NumPy的帮助文档

# -*- coding: utf-8 -*-
"""
Spyder Editor This is a temporary script file.
""" import numpy as np help(np.arange) #Help on built-in function arange in module numpy.core.multiarray:
#
#arange(...)
# arange([start,] stop[, step,], dtype=None)
#
# Return evenly spaced values within a given interval.
#
# Values are generated within the half-open interval ``[start, stop)``
# (in other words, the interval including `start` but excluding `stop`).
# For integer arguments the function is equivalent to the Python built-in
# `range <http://docs.python.org/lib/built-in-funcs.html>`_ function,
# but returns an ndarray rather than a list.
#
# When using a non-integer step, such as 0.1, the results will often not
# be consistent. It is better to use ``linspace`` for these cases.
#
# Parameters
# ----------
# start : number, optional
# Start of interval. The interval includes this value. The default
# start value is 0.
# stop : number
# End of interval. The interval does not include this value, except
# in some cases where `step` is not an integer and floating point
# round-off affects the length of `out`.
# step : number, optional
# Spacing between values. For any output `out`, this is the distance
# between two adjacent values, ``out[i+1] - out[i]``. The default
# step size is 1. If `step` is specified as a position argument,
# `start` must also be given.
# dtype : dtype
# The type of the output array. If `dtype` is not given, infer the data
# type from the other input arguments.
#
# Returns
# -------
# arange : ndarray
# Array of evenly spaced values.
#
# For floating point arguments, the length of the result is
# ``ceil((stop - start)/step)``. Because of floating point overflow,
# this rule may result in the last element of `out` being greater
# than `stop`.
#
# See Also
# --------
# linspace : Evenly spaced numbers with careful handling of endpoints.
# ogrid: Arrays of evenly spaced numbers in N-dimensions.
# mgrid: Grid-shaped arrays of evenly spaced numbers in N-dimensions.
#
# Examples
# --------
#np.arange(3)
# array([0, 1, 2])
#np.arange(3.0)
# array([ 0., 1., 2.])
#np.arange(3,7)
# array([3, 4, 5, 6])
#np.arange(3,7,2)
# array([3, 5])

  

Python笔记_第五篇_Python数据分析基础教程_相关安装和版本查看的更多相关文章

  1. Python笔记_第五篇_Python数据分析基础教程_前言

    1. 前言: 本部分会讲解在Python环境下进行数值运算.以NumPy为核心,并讲解其他相关库的使用,诸如Matplotlib等绘图工具等. C.C++和Forttran等变成语言各有各的优势,但是 ...

  2. Python笔记_第五篇_Python数据分析基础教程_文件的读写

    1. 读写文件(基本) savetxt.loadtxt i2 = np.eye(2) print(i2) np.savetxt(r"C:\Users\Thomas\Desktop\eye.t ...

  3. Python笔记_第五篇_Python数据分析基础教程_NumPy基础

    1. NumPy的基础使用涵盖如下内容: 数据类型 数组类型 类型转换 创建数组 数组索引 数组切片 改变维度 2. NumPy数组对象: NumPy中的ndarray是一个多维数组对象,该兑现共有两 ...

  4. Python学习笔记【第五篇】:基础函数

    一.函数:函数定义关键字def  后跟函数名称 def 函数名(参数):             ...     函数体     ...     返回值 案例: # 定义函数 def say_hei( ...

  5. Python数据分析基础教程

    Python数据分析基础教程(第2版)(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1_FsReTBCaL_PzKhM0o6l0g 提取码:nkhw 复制这段内容后 ...

  6. Python 3基础教程1-环境安装和运行环境

    本系列开始介绍Python3的基础教程,为什么要选中Python 3呢?之前呢,学Python 2,看过笨方法学Python,学了不到一个礼拜,就开始用Python写Selenium脚本.最近看到一些 ...

  7. Python全栈开发记录_第五篇(装饰器)

    单独记录装饰器这个知识点是因为这个知识点是非常重要的,必须掌握的(代码大约150行). 了解装饰器之前要知道三个知识点 作用域,上一篇讲到过顺序是L->E->G->B 高阶函数: 满 ...

  8. Python笔记(二十五)_魔法方法_描述符

    描述符的属性方法 __get__(self, instance, owner): 用于访问属性,返回属性的值 __set__(self, instance, value): 用于给属性赋值时,返回属性 ...

  9. [Python笔记]第十六篇:web框架之Tornado

    Tornado是一个基于python的web框架,xxxxx 安装 python -m pip install tornado 第一个Tornado程序 安装完毕我们就可以新建一个app.py文件,放 ...

随机推荐

  1. 字符串题汇总(python3)

    1.最小编辑距离 假设有两个字符串s1和s2,计算通过增添.删除.替换三种操作后,从s1转变为s2所需要的操作次数. #coding=utf-8 class Solution: def editDis ...

  2. tp5日志分表

    /** * 记录网站日志 * * @return bool */ public function record() { // 组装数据 $log = self::$param; $log[self:: ...

  3. python3.7的一些心得,不定期更新。

    学习的python3.7.2,最新目前是3.8.1 这里记一下主要的几点: pip 是python的模块管理器,姑且这么叫它.和nodejs的npm一样的功能 官网下载python安装包,默认就会按照 ...

  4. go_http

    httpSvr // HandleFunc registers the handler function for the given pattern // in the DefaultServeMux ...

  5. 《ES6标准入门》(阮一峰)--3.变量的解构赋值

    1.数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). 以前,为变量赋值,只能直接指定值. let a = 1; l ...

  6. MongoDB_01

    解释:MongoDB可应对 --三高需求 High performance-对数据库高并发读写的需求 Huge Storage -对海量数据的高效率存储和访问的需求 High Scalability ...

  7. 剑指offer自学系列(三)

    题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变,例如{5,1,4,2 ...

  8. int类型和char类型的区别

    下面三个定义式的区别: int i = 1; char i = 1; char i = '1'; int用来定义整型变量,char用来定义字符型变量,要清楚的知道三个定义式的区别,可以比较它们在内存中 ...

  9. 123-PHP类构造函数

    <?php class ren{ //定义人类 private $name; //定义成员属性 public function __construct($name){ //定义构造函数 $thi ...

  10. Python MongoDB 插入文档

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...