第四章课后习题答案《从零开始学python数据分析与数据挖掘》
import numpy as np
num1 = np.array([2,3,5,7])
print(num1)
num2 = np.array([1,12,11,17,13,18,12,14])
print(num2[np.fmod(num2,2) == 0])
print(num2[num2 % 2 == 0])
num3 = np.array([[1,12,11,17],[13,18,12,14],[8,11,14,10],[6,8,9,22]])
print(num3[2,:])
print(num3[:,1])
num4 = num3
print(np.sum(num4,axis = 1))
num5_1 = np.random.rand(5,3)
num5_2 = np.random.rand(3,2)
print(num5_1,num5_2)
print(np.dot(num5_1,num5_2))
print(np.resize(num4,(8,2)))
a = np.arange(0,10).reshape(2,-1)
b = np.repeat(1,10).reshape(2,-1)
print(np.hstack((a,b)))
num_9 = np.array([1,2,3,2,3,4,3,4,5,6])
print(np.unique(num_9))
num_10 = num3
print(np.where(num_10 % 2 ==0,-1,num_10))
A = np.array([[1,1],[2,3]])
b = np.array([7,18])
beta = np.linalg.solve(A, b)
num12 = np.random.randint(0,10,size = 10)
print(num2)
print(np.where(num12 == np.max(num12),0,num12))
def game(money , counts):
i = 1
while i <= counts:
i = i + 1
p = np.random.uniform(0,1)
if p <= 0.5 :
money = money + 8
else :
money = money -8
if money < 0:
print('对不起您的余额不足请充值!')
return money
print('经过{}次的游戏,您还剩余{}元'.format(counts,money))
money = 1000
money = game(money,1000)
本章重点内容汇总:
我感觉这里面函数用的比较多的就是堆叠函数的使用,还有一些函数我在这里没有列举出来,像where函数,和if的使用方法有一些相同之处,但是还有一些其他的用途,大家可以自己多去尝试使用一下这些函数,做到熟悉掌握以后写代码的时候就会很快了。
|