DIKW

DATA-->INFOMATION-->KNOWLEDGE-->WISDOM

数据-->信息-->知识-->智慧

爬虫-->数据库-->数据分析-->机器学习

  • 信息:通过某种方式组织和处理数据,分析数据间的关系,数据就有了意义
  • 知识:如果说数据是一个事实的集合,从中可以得出关于事实的结论。那么知识(Knowledge)就是信息的集合,它使信息变得有用。知识是对信息的应用,是一个对信息判断和确认的过程,这个过程结合了经验、上下文、诠释和反省。知识可以回答“如何?”的问题,可以帮助我们建模和仿真
  • 智慧:智慧可以简单的归纳为做正确判断和决定的能力,包括对知识的最佳使用。智慧可以回答“为什么”的问题。回到前面的例子,根据故障对客户的业务影响可以识别改进点

数学

微积分

  1. # import math
  2. # s = 0
  3. # for i in range(1, 1001):
  4. # x = (math.pi / 1000) * i
  5. # y = math.sin((math.pi / 1000) * i)
  6. # s = (math.pi / 1000) * y + s
  7. # print(s)
  1. # import numpy as np
  2. # def sin_integral(l,r,p):
  3. # sum_result = 0
  4. # delta = (r - l) / p
  5. # for i in range(p):
  6. # left = i * delta
  7. # delta_area = delta * np.sin(left)
  8. # sum_result += delta_area
  9. # return sum_result
  10. # print(sin_integral(0.0,np.pi,100000))

numpy

  1. # coding=utf-8
  2. import numpy as np
  3. import matplotlib.pyplot as pt
  4.  
  5. # x的3次方
  6. # X = np.linspace(-100, 100, 100)
  7. # Y = X * X * X
  8.  
  9. # tan
  10. # X = np.linspace(-np.pi//2,np.pi//2,1000)
  11. # Y = np.tan(X)
  12.  
  13. # log
  14. # X = np.linspace(-10,10,100)
  15. # Y = np.log(X)
  16. #
  17. # pt.plot(X, Y)
  18. # pt.show()
  19.  
  20. # -----------------------------------------------------------------------
  21.  
  22. # 鸡兔同笼
  23. # for x in range(36):
  24. # y = 35 - x
  25. # if x+2*y == 47:
  26. # print(x,y)
  27.  
  28. # sinx面积0-pi
  29. # import math
  30. # s = 0
  31. # for i in range(1, 1001):
  32. # x = (math.pi / 1000) * i
  33. # y = math.sin((math.pi / 1000) * i)
  34. # s = (math.pi / 1000) * y + s
  35. # print(s)
  36. # 承上封装为函数
  37. # import numpy as np
  38. # def sin_integral(l,r,p):
  39. # sum_result = 0
  40. # delta = (r - l) / p
  41. # for i in range(p):
  42. # left = i * delta
  43. # delta_area = delta * np.sin(left)
  44. # sum_result += delta_area
  45. # return sum_result
  46. # print(sin_integral(0.0,np.pi,100000))
  47.  
  48. # --------------------------------------------------------
  49. # a = np.arange(18).reshape(3, 6) #二维数组矩阵
  50. a = np.arange(24).reshape(2,3,4) #三维数组矩阵
  51. # print a
  52. # print a.ndim #矩阵维数
  53. # print np.ndim([[1,1],[2,2]]) #矩阵维数
  54. # print a.dtype.name #数值类型 int32
  55. # print a.size #元素个数
  56. # print a.itemsize #每个数组元素的字节大小
  57. # print type(a) #a的类型
  58.  
  59. b = np.array([[1.2, 2, 3], [4, 5, 6]])
  60. # print b.dtype #float64类型的数组
  61.  
  62. c = np.array([[1, 1], [2, 2]], dtype=complex)
  63. # print c,c.dtype #复数类型complex128类型的数组
  64.  
  65. z = np.zeros((3, 4))
  66. # print z #创建全零数组,默认为float64形式
  67.  
  68. o = np.ones((2, 3, 4), dtype=np.int16)
  69. # print o
  70. # 创建一个三维全1的数组,并且创建时指定类型,可以认为是一个长方体里有序的充满了1
  71. #两层,每一层是三行四列的二维数组
  72.  
  73. e = np.empty((2,3))
  74. # print e #创建一个二维空数组,电脑不同显示不同
  75.  
  76. # f = np.arange(1,9,2)
  77. f = np.arange(0,3,0.5)
  78. # print f #[1 3 5 7],2和0.5为步进值
  79.  
  80. # print np.arange(10000) #如果数组太大而无法全部打印,NumPy会自动跳过中央部分,只能打印出边界部分(首尾)
  81. # np.set_printoptions(threshold='nan') #禁用此省略并强制NumPy打印整个数组,使用set_printoptions更改打印选项
  82. # print np.arange(10000).reshape(100,100)
  83.  
  84. a = np.array([20,30,40,50])
  85. b = np.arange(4)
  86. # print a-b #相减
  87. # print b**2 #平方
  88. # print 10*np.sin(a) #a数组先进行sin运算,然后结果乘10

运算

阶乘

  1. np.math.factorial(100)

对数

  1. np.log()

开方

1.准备每一个条件的数据表示 2.准备程序的逻辑 3.将你的数据应用到逻辑 4.优化结构

  1. # np.sqrt(3)
  2.  
  3. # A = (2, 7)
  4. # B = (8, 3) # 欧几里得距离
  5. # AB = np.sqrt((A[0] - B[0]) ** 2 + (A[1] - B[1]) ** 2)
  6. # print AB

三角函数

  • np.arctan()
  • np.cos()
  • np.sin()
  • np.rad2deg()——弧度转角度
  • np.deg2rad——角度转弧度
  • ……
  1. # x = np.array([3, 0]) + np.array([0, 3])
  2. # x = np.array([3,3])
  3. # l = np.linalg.norm(x) #矢量x的范数(长度)
  4. # h = np.arctan(3.0/3.0) #计算弧度 π/4
  5. # j = np.rad2deg(h) #弧度转角度 45度
  6. # np.deg2rad() #角度转弧度
  7. # print j

点乘

numpy数组 (矢量) 默认的 +-*/ 操作都是对应位置的元素相操作

  1. array1.dot(array2)
  2.  
  3. # d1 = np.array([2, 7])
  4. # d2 = np.array([8, 3])
  5. # print d1.dot(d2) #点乘(内积) 2*8+7*3 结果:实数
  1. # 余弦相似度,向量内积,对应元素相乘再相加
  2. '''
  3. 设两个向量分别为a=(x1,y1),b=(x2,y2),
  4. 其夹角为α,因为ab=|a||b|cosα,
  5. 所以cosα=ab/|a||b|=(x1y1+x2,y2)/(根号(x1^2+y1^2)根号(x2^2+y1^2))
  6. '''
  7. # d12 = d1.dot(d2) #d1·d2
  8. # d1_len = np.linalg.norm(d1) #|d1|
  9. # d2_len = np.linalg.norm(d2) #|d2|
  10. # cosa = d12 / (d1_len * d2_len) #余弦值cosa
  11. # a = np.rad2deg(np.arccos(cosa)) #角度a
  12. # print a

复数

  1. # a = 1 + 2j #复数 complex
  2. # b = 2 + 3j #泰勒级数,傅里叶级数
  3. # print a,type(a),a*b,a-b
  1. # np.nan #not a number 当数据读取缺失或计算异常时会出现,本质是一个浮点数
  2. # np.exp(10) #以e为底的指数
  3. # np.log(10) #以e为底的对数,即ln
  4. # np.e #e,2.71828182
  5. # np.inf #无穷大

函数

空数组

默认值是0或正无穷或负无穷

实数在计算机里只能用浮点数无限逼近精度,不能确切表示,所以在处理0的时候要格外小心 ;a - b < 0.1e-10 相减的时候当结果小于一个极小的数值就认为相等

  1. np.empty((3, 3))

数组

矢量是有方向和长度的变量,可以用numpy的多位数组来表示,二维矢量就是平面的一个点

  1. np.array([[1,2,3],[4,5,6]])

范数

矢量的范数(长度)

  1. np.linalg.norm(np.array([3,3]))

类型转换

  1. array.astype(np.int)

数组信息

  1. array.shape
  2. array.shape[0]
  3. array.shape[1]
  1. # 使用两个矢量相减,可以计算两点距离
  2. d1 = np.array([2, 7])
  3. # d2 = np.array([8,3])
  4. # np.linalg.norm(d1-d2)
  5. # d1.astype(np.int) #将数组类型强制转换为int
  6. # d1.shape #返回数组的行列数
  7. # d1.shape[0] #返回数组的行数
  8. # d1.shape[1] #返回数组的列数

均分

  1. # np.linspace()
  1. # xs = np.linspace(-1000, 1000, 10000)
  2. # idx = []
  3. # max_result = []
  4. # for x in xs:
  5. # y = -3 * (x ** 2) + 5 * x - 6
  6. # idx.append(x)
  7. # max_result.append(y)
  8. # print max(max_result),idx[max_result.index(max(max_result))]
  9.  
  10. # def poly_test(l,r):
  11. # r_len = r - l
  12. # max_num = l
  13. # m_idx = l
  14. # for i in range(r_len):
  15. # r_num = l + i
  16. # result = (r_num ** 2) * -3 + (5 * r_num) - 6
  17. # if result > max_num:
  18. # max_num = result
  19. # m_idx = i
  20. # return max_num,m_idx
  21. # print poly_test(-10000,10000)
  1. # 在X轴上生成2000个从-10000到10000的离散点
  2. # 使用矢量计算直接生成对应上述多项式的所有结果,这里没有使用循环,一次计算了20000个结果
  3. # X = np.linspace(-1000, 10000, 20000)
  4. # Y = (X ** 2) * -3 + 5 * X - 6 # 矢量运算,计算机会加速此类运算
  5. # Y.max() # 获取当前矢量的最大值
  6. # Y.argmax() # 获取当前数组最大值对应的索引(X值,不是函数中的X)

数组切片

二维数组

  1. n_array = np.arange(25).reshape(5, 5)
  2. # print n_array #第一个数选行,第二个选列
  3. # print n_array[:,:2] #前两列
  4. # print n_array[:3,:] #前三行
  5. # print n_array[1:4,1:4] #1-3行且1-3列
  6. # print n_array[2,2] #第3行的第3个数
  7. # print n_array[2][2] #同上
  8. # print n_array[::-2] #隔行选择
  9. # print n_array[::2]

三维数组

  1. n3_array = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [3, 2, 1]], [[6, 5, 4], [9, 8, 7]]])
  2. # print n3_array #第一个数选层,第二个数选行,第三个数选列
  3. # print n3_array[:,:,2] #最后一列
  4. # print n3_array[:,:1,:].sum() #每一层的第一行
  5. # print n3_array[:1,:,:].mean() #平均值
  6. # print n3_array[:,1,:2].std() #方差小,更稳定

数组元素选取

  1. # d1 < 3 #返回满足条件的布尔类型矩阵
  2. # np.count_nonzero(d1 < 3) #统计数组中小于3的元素个数
  3. # d1[d1<3] #选出指定范围的元素

学生成绩案例

数据准备

  1. # score_array = np.loadtxt(open('score.csv', 'rb'), delimiter=',', dtype=int)
  2. score_array = np.genfromtxt('score.csv', delimiter=',', dtype=int)
  3. students = []
  4. courses = ['数学', '语文', '化学', '地理', '音乐', '体育']

课程成绩最好

  1. def course_score():
  2. course_score_max = 0
  3. cid_max = -1
  4. for c in range(6):
  5. course_score = score_array[:, c].sum()
  6. print course_score
  7. if course_score_max < course_score:
  8. course_score_max = course_score
  9. cid_max = c
  10. return courses[cid_max], course_score_max

学生成绩最好

  1. def student_score():
  2. student_score_max = 0
  3. sid_max = -1
  4. for s in range(6):
  5. student_score = score_array[s, :].sum()
  6. print '{}号学生成绩:{}分'.format(s, student_score)
  7. if student_score_max < student_score:
  8. student_score_max = student_score
  9. sid_max = s
  10. return '{}号学生成绩最好,总分为{}分'.format(sid_max, student_score_max)

学生偏科

  1. def pian():
  2. pian_max = 0
  3. pid_max = -1
  4. for p in range(6):
  5. student_score_std = score_array[p, :].std()
  6. print '{}号学生成绩方差为:{}'.format(p, student_score_std)
  7. if pian_max < student_score_std:
  8. pian_max = student_score_std
  9. pid_max = p
  10. return '{}号学生偏科,方差为:{}'.format(pid_max, pian_max)

主课成绩最好

  1. def main_course_score():
  2. main_course_score_max = 0
  3. cid_max = -1
  4. for c in range(3):
  5. main_course_score = score_array[:, c].sum()
  6. print main_course_score
  7. if main_course_score_max < main_course_score:
  8. main_course_score_max = main_course_score
  9. cid_max = c
  10. return cid_max, main_course_score_max

该班主课副课对比哪个成绩好

  1. def than():
  2. main_course_std = 0
  3. side_course_std = 0
  4. for t in range(3):
  5. main_course_std += score_array[:, t].std()
  6. main_course_std /= 3
  7. for t in range(3, 6):
  8. side_course_std += score_array[:, t].std()
  9. side_course_std /= 3
  10. if main_course_std > side_course_std:
  11. return '该班主课成绩更好'
  12. else:
  13. return '该班副课成绩更好'

这个班有多少学生出现了不及格

  1. def bad():
  2. badstudent = []
  3. for b in range(6):
  4. if min(score_array[b, :]) < 60:
  5. badstudent.append(b)
  6. # print '{}学生不及格'.format(b)
  7. return '不及格学生:{}'.format(badstudent)

封装成类

  1. name_dic = {0: '数学', 1: '语文', 2: '化学', 3: '地理', 4: '音乐', 5: '体育'}
  2.  
  3. class CoursaDesc(object):
  4. def __init__(self):
  5. self.name = ''
  6. self.std = 0
  7. self.max = 0
  8. self.min = 0
  9. self.mean = 0
  10. self.num = 0
  11.  
  12. class ComputerDesc(object):
  13. def __init__(self, n_array):
  14. self.score_array = n_array
  15. self.result = []
  16.  
  17. def counter_all_coursa(self):
  18. for i in range(6):
  19. c_desc = CoursaDesc()
  20. c_desc.name = name_dic[i]
  21. c_desc.std = self.score_array[:, i].std()
  22. c_desc.mean = self.score_array[:, i].mean()
  23. c_desc.max = self.score_array[:, i].max()
  24. c_desc.min = self.score_array[:, i].min()
  25. c_desc.sum = self.score_array[:, i].sum()
  26. self.result.append(c_desc)
  27.  
  28. def best_coursa(self):
  29. # std_list = [coursa.std for coursa in self.result]
  30. # sum_list = [coursa.sum for coursa in self.result]
  31. std_list = []
  32. sum_list = []
  33. for coursa in self.result:
  34. std_list.append(coursa.std)
  35. sum_list.append(coursa.sum)
  36. std_array = np.array(std_list)
  37. sum_array = np.array(sum_list)
  38.  
  39. max_sum_coursa = sum_array.max()
  40. max_sum_index = sum_array.argmax()
  41.  
  42. min_std_coursa = std_array.min()
  43. min_std_index = std_array.argmin()
  44.  
  45. if max_sum_index == min_std_index:
  46. return name_dic[max_sum_index]
  47. else:
  48. # 方差最小的课程的成绩总和
  49. min_std_coursa_sum = sum_array[min_std_index]
  50. # 总和成绩最大的课程的方差
  51. max_sum_coursa_std = std_array[max_sum_index]
  52.  
  53. sum_delta = max_sum_coursa - min_std_coursa_sum
  54. std_delta = max_sum_coursa_std - min_std_coursa
  55. sum_percent = sum_delta / max_sum_coursa
  56. std_percent = std_delta / min_std_coursa
  57. if sum_percent < 0.05 and std_percent > 0.2:
  58. return name_dic[min_std_index]
  59.  
  60. if __name__ == '__main__':
  61. c = ComputerDesc(score_array)
  62. c.counter_all_coursa()
  63. print c.best_coursa()

数据分析——numpy的更多相关文章

  1. 利用Python进行数据分析——Numpy基础:数组和矢量计算

    利用Python进行数据分析--Numpy基础:数组和矢量计算 ndarry,一个具有矢量运算和复杂广播能力快速节省空间的多维数组 对整组数据进行快速运算的标准数学函数,无需for-loop 用于读写 ...

  2. Python数据分析-Numpy数值计算

    Numpy介绍: NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础. NumPy的主要功能: 1)ndarray,一个多维数组结构,高效且节省空间 2)无需循环对整组 ...

  3. Python数据分析numpy库

    1.简介 Numpy库是进行数据分析的基础库,panda库就是基于Numpy库的,在计算多维数组与大型数组方面使用最广,还提供多个函数操作起来效率也高 2.Numpy库的安装 linux(Ubuntu ...

  4. Python数据分析Numpy库方法简介(二)

    数据分析图片保存:vg 1.保存图片:plt.savefig(path) 2.图片格式:jpg,png,svg(建议使用,不失真) 3.数据存储格式: excle,csv csv介绍 csv就是用逗号 ...

  5. python数据分析Numpy(二)

    Numpy (Numerical Python) 高性能科学计算和数据分析的基础包: ndarray,多维数组(矩阵),具有矢量运算能力,快速.节省空间: 矩阵运算,无需循环,可以完成类似Matlab ...

  6. python 数据分析----numpy

    NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础. NumPy的主要功能: ndarray,一个多维数组结构,高效且节省空间 无需循环对整组数据进行快速运算的数学函数 ...

  7. 数据分析--numpy的基本使用

    一.numpy概述 NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础. NumPy的主要功能: ndarray,一个多维数组结构,高效且节省空间 无需循环对整组数据进 ...

  8. 数据分析 - Numpy

    简介 Numpy是高性能科学计算和数据分析的基础包.它也是pandas等其他数据分析的工具的基础,基本所有数据分析的包都用过它.NumPy为Python带来了真正的多维数组功能,并且提供了丰富的函数库 ...

  9. 数据分析-numpy的用法

    一.jupyter notebook 两种安装和启动的方式: 第一种方式: 命令行安装:pip install jupyter 启动:cmd 中输入 jupyter notebook 缺点:必须手动去 ...

随机推荐

  1. Node的express配置使用ejs模板

    注册ejs模板为html页.以.ejs为后缀的模板页,现在的后缀名可以是.html app.engine('.html', require('ejs').__express); 设置视图模板的默认后缀 ...

  2. git add . 提示 `Changes not staged for commit`

  3. Lambda表达式概念与基本语法

    Lambda表达式是Java 8的重要更新,一个被广大开发者期待已久的新特性.Lambda表达式支持将代码块作为方法参数,Lambda表达式允许使用更简洁的代码来创建只有一个抽象方法的接口(这种接口被 ...

  4. Matcher.replaceFirst(String replacement)

    java.util.regex.Matcher.replaceFirst(String replacement)方法是用来进行字符串的替换操作. public String replaceFirst( ...

  5. L2-006 树的遍历 (25 分)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...

  6. Sping 里面的适配器模式的实现

    适配器模式----------设计模式最近在看SpringMVC源码,从中看到了比较优秀的设计模式所以来分享下. 1.适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口,Adap ...

  7. VS2012发布网站详细步骤问题

    http://blog.csdn.net/mrobama/article/details/43118387

  8. TCP-IP详解笔记8

    TCP-IP详解笔记8 TCP超时与重传 下层网络层(IP)可能出现丢失, 重复或丢失包的情况, TCP协议提供了可靠的数据传输服务. TCP启动重传操作, 重传尚未确定的数据. 基于时间重传. 基于 ...

  9. java程序员技术范围

    1 工具 开发工具.源代码管理.构建工具.测试工具(压力.安全等).接口测试工具.反编译工具.日志工具.第三方工具等 2 java jvm.多线程.socket.io(两种方式).集合(两大接口).异 ...

  10. j2ee之监听页面请求

    本博客的起因是我想监听浏览器端每个页面都访问了哪些资源~~: 我是个菜鸡,所以我要记在我的小本本上,我怕忘了又~~~: 代码我是写在springboot2.1中的,有兴趣的同学可以玩一下~ 1:代码如 ...