重学python【numpy】

#-*- encoding:utf-8 -*-
#创建数据
import numpy as np
a = np.array([1, 2, 4])
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b[1, 2] = 99
#获取数组的大小
print a.shape
print b.shape
#获取数组的类型
print a.dtype
print b
#创建结构数组
persontype = np.dtype({
    'names': ['name', 'sex', 'chinese', 'math', 'english'],
    'formats': ['S32', 'S32', 'i', 'i', 'f']
})
people = np.array([("XiaoYu", "woman", "100", "90", "59"),
                   ("XiaoLin", "woman", "90", "10", "80"),
                   ("XiaoLi", "man", "43", "43", "99.9"),
                   ("LV", "non", "30", "90", "40.3")],dtype=persontype)
print people
#获取所有哦姓名
names = people[:]["name"]
print names
#获取语文的平均分数
chinese = people[:]["chinese"]
print np.mean(chinese)
#获取数学的最大成绩
math = people[:]["math"]
print np.amax(math)

#获取一个数组[1, 3, 5, 7, 9]
x1 = np.arange(1, 10, 2)
print x1
##线性等分向量
x2 = np.linspace(1, 9, 5)
print x2
#数组之间的加减乘除、N次方,取余
print np.add(x1, x2)
print np.subtract(x1, x2)
print np.multiply(x1, x2)
print np.divide(x1, x2)
print np.power(x1, 3)
print np.power(x1, x2)
print np.remainder(x1, x2)

#统计函数
b = np.array([[1, 2, 10], [4, 5, 6], [7, 8, 9]])
print np.amin(b)
#获取每一列的最小值, 行话叫axis=0 轴
print np.amin(b, 0)
#获取每一行的最小值
print np.amin(b, 1)
#获取最大值和最小值之差
print np.ptp(b)
print np.ptp(b, 0)
print np.ptp(b, 1)
#统计数组的百分位数,p=50相当于求平均值,p的范围是0-100
print np.percentile(b, 100)
print np.percentile(b, 50, 0)
print np.percentile(b, 30, axis=1)
#统计数组的中位数
print np.median(b)
print np.median(b, 0)
#计算加权平均书(加权平均数的计算方法,我小妹都知道~)
c = np.array([1, 2, 3, 4])
wts = np.array([1, 2, 3, 4])
print np.average(c)
print np.average(c, weights=wts)
#统计数组的标准差,标准差是方差的算术平方根
print np.std(c)
#统计数组的方差,每个数值与平均值之差的平方求和的平均
print np.var(c)
##做个验证
print np.power(np.std(c), 2)

#数组的排序,
d = np.array([[4, 6, 1], [3, 5, 2]])
#默认参数sort(a, axis=-1, kind=‘quicksort’, order=None),-1为数组的最后一个轴,指定 quicksort、mergesort、heapsort 分别表示快速排序、合并排序、堆排序
print np.sort(d)
print np.sort(d, axis=None)
# 用 NumPy 统计下在语文、英语、数学中的平均成绩、最小成绩、最大成绩、方差、标准差。
# 然后把这些人的总成绩排序,得出名次进行成绩输出。
# 构建数据
studenttype = np.dtype({
    'names': ['name', 'chinese', 'english', 'math', 'total'],
    'formats': ['S32', 'f', 'f', 'f','f']
})
students = np.array([('lv', 66, 65, 30, 0),
                     ('ly', 95, 85, 98, 0),
                     ('bfd', 93, 92, 96, 0),
                     ('mi', 90, 88, 77, 0),
                     ('baidu', 80, 90, 90, 0)],dtype=studenttype)
#计算语文的平均成绩
print np.mean(students[:]["chinese"])
#计算数学的最小值
print np.amin(students[:]["math"])
#计算英语的最大成绩
print np.amax(students[:]["english"])
#计算语文的方差
print np.var(students[:]["chinese"])
#语文的标准差
print np.std(students[:]["chinese"])
print "======总成绩排序======="
print students[:]["chinese"]
students[:]["total"] = np.add(np.add(students[:]["chinese"], students[:]["math"]), students[:]["english"])
print np.sort(students,order="total")

不要太在意别人的评价,做自己喜欢的事情就好~温故而知新嘛

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值