FINS3648/5548 -fins5548代写
时间:2023-10-13
FINS3648/5548
Python Quiz 1 - Code Review
Tips
1. ⼀定要⾃⼰动⼿跑代码!
1. 学习顺序:notes -> Ed -> Sample Code
1. 复习重点:
Ed:Week 1-3,Week4部分(取决于考试范围)
Sample Code:FinTech_Hello, FinTech_Basics, FinTech_Plot
1. 复习的时候,Ed的代码复制到IDE⾥⾯逐⾏跑,逐⾏看结果才能理解代码的意思
1. 学会⽤google或者百度去学习代码, e.g. google np.random.standard_normal
1. 如果是开卷或者不计切屏,把IDE打开好,题⽬⾥的代码直接复制粘贴过去跑
1. 写Coding的题⽬,代码测试好了再提交
IDE
重要的是理解代码的意思和⽤法,⾄于⽤什么IDE跑⽆所谓的。
Jupyter Notebook - Anaconda
Google Colab - https://colab.research.google.com/
Pycharm - 不推荐新⼿
Basics
Print()
Hello World
Hello World
In [1]: print('Hello World')
print("Hello World")
Hello world
Hello
world
Hello
world
"Hello FinTech Class!!!"
Penny said, "This is my dog".
input()
Enter a value: 55
55
Data Type
2
2.0
2
In [2]: print("Hello\tworld") # \t is a tab character
In [3]: print("Hello\nworld") # \n is a newline character
In [4]: print('''Hello
world''')
In [5]: print("\"Hello FinTech Class!!!\"")
In [6]: print('Penny said, "This is my dog".')
In [7]: value = input('Enter a value: ')
print(value)
In [8]: print(type(2))
print(type(2.0))
print(type('2'))
print(type(True)) # Boolean - 只有True或者False两个值
In [9]: a = 2
b = '2'
c = 2.5
#数据类型之间的转换
d = str(a)
e = float(a)
f = int(c)
print(d)
print(e)
print(f)
None
[1, 2, 3]
(1, 2, 3)
{1, 2, 3}
{'Alice': 0, 'Bob': 1, 'Eve': 2, 'Mallory': 3}
Variables
Hello there
Hello
Goodbye
2
4
Operations
In [10]: empty = ''
none = None
print(empty)
print(none)
print(type(empty))
print(type(none))
In [11]: x = [1, 2, 3] # list 数据是有顺序的
y = (1, 2, 3) # Tuple 数据及顺序⼀旦创建⽆法修改
z = {1, 2, 3} # Sets ⽆序,每个值是唯⼀的
scores = {'Alice': 0,'Bob': 1, 'Eve': 2,'Mallory': 3} # Dictionary key:value
print(x)
print(type(x))
print(y)
print(type(y))
print(z)
print(type(z))
print(scores)
print(type(scores))
In [12]: message = 'Hello there'
print(message)
In [13]: a, b = 'Hello', 'Goodbye'
print(a)
print(b)
In [14]: x = 2
print(x)
x += 2 # x = x + 2. Augmented assignment operators(Ed 第⼀周!)
print(x)
48.980149999999995
100
3
1
48.98015
5
100
How many days? 15
15 days is 21600 minutes
Strings
'this is a line'
14
3
['this', 'is', 'a', 'line']
4
In [15]: tax = 48.5 / 100
price = 100.99
tax_net = price * tax
print(tax_net)
In [16]: print(10**2) #次⽅
print(10//3) #只取除后的整数
print(10%3) #余数 10 = 3*3+1
In [17]: print(round(48.980149999999995,5))
print(abs(-5))
print(pow(10, 2)) #power, This is the same as **
In [18]: # example - 结合了上⾯⼏个点,⾃⼰回去看
HOURS_PER_DAY = 24
MINUTES_PER_HOUR = 60
num_days = int(input('How many days? '))
num_minutes = num_days * HOURS_PER_DAY * MINUTES_PER_HOUR
print(num_days, 'days is', num_minutes, 'minutes')
In [19]: a = 'this is a line'
a
Out[19]:
In [20]: len(a)
Out[20]:
In [21]: a.count('i') # string method(Ed 第⼀周!)
Out[21]:
In [22]: a.split()
Out[22]:
In [23]: len(a.split()) # list⾥⾯有⼏个元素
Out[23]:
Indexing
[1, 4, 9, 16, 25]
1
25
[9, 16, 25]
[4, 9]
[1, 4, 9, 16]
[['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5]]
['a', 'b', 'c', 'd', 'e']
[1, 2, 3, 4, 5]
b
1
Copy and Clone
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25]
Add new elements
[1, 4, 9, 16, 25]
In [24]: squares = [1, 4, 9, 16, 25]
In [25]: print(squares)
print(squares[0]) # 正整数,计数从0开始
print(squares[-1]) # 倒着数,计数从-1开始
print(squares[-3:]) # 从-3位到最后⼀位
print(squares[1:3]) # 从第1位到第⼆位 [包含,不包含]
print(squares[:-1]) # 从第0位到倒数第⼆位
In [26]: x = ['a', 'b', 'c', 'd', 'e']
y = [1, 2, 3, 4, 5]
z = [x, y]
In [27]: print(z)
print(z[0])
print(z[1])
print(z[0][1])
print(z[1][0])
In [28]: # Copy:原list⾥的数据改变,copy之后的list⾥的数据也会做出改变
copy_squares = squares
copy_squares
Out[28]:
In [29]: # Clone:原list⾥的数据改变,clone之后的list⾥的数据不会跟着改变
clone_squares = squares[:]
clone_squares
Out[29]:
In [30]: squares = [1, 4, 9, 16, 25]
squares
Out[30]:
[1, 4, 9, 16, 25, 121]
[1, 4, 9, 16, 25, 121, 200, 300, 400]
[1, 4, 9, 16, 25, 121, 200, 300, 400, 36, 49, 64, 81, 100]
[1, 4, 1000, 9, 16, 25, 121, 200, 300, 400, 36, 49, 64, 81, 100]
Conditions
1. 判断符号:==, !=, >, >=, <, <=
2. 连接多个判断条件:
and - 全部条件都要满⾜
or - 满⾜其中⼀个即可
3. not - 表示相反
False
False
True
if elif else
In [31]: squares.append(11*11) #append()在list中添加新元素, 括号⾥的作为⼀个整体添加
print(squares)
In [32]: x = [200, 300, 400]
squares.extend(x) #extend()在list中逐⼀添加
print(squares)
In [33]: new_squares = squares + [36, 49, 64, 81, 100]
print(new_squares)
In [34]: new_squares.insert(2, 1000) #在指定的index位置添加
print(new_squares)
In [35]: # removing, other methods and functions, sorting - Ed 第⼆周!
In [35]: # '!=' 不等于
1 != 1.0 # 产⽣的结果数据类型是boolean
Out[35]:
In [36]: (1 > 2) and (3 > 2)
Out[36]:
In [37]: not(1 > 2)
Out[37]:
a is not zero
this line will be printed anyway
a is zero
That is a big number
It is bigger than 100
Good bye
while Loop
In [38]: a = 5
if a == 0:
print('a is zero')
else:
print ('a is not zero')
print("this line will be printed anyway")
In [39]: a = 0
if a == 0:
print('a is zero')
elif a == 1:
print('a is one')
elif a == 2:
print('a is two')
else:
print ('a is not zero')
In [40]: number = 200
if number > 10:
print('That is a big number')
if number > 100:
print('It is bigger than 100')
print('Good bye')
In [41]: a, b = 0, 1 # 同时赋值给a和b
c = []
while b < 1000:
# print(b)
print(a, b)
a, b = b, a+b
c.append(b)
print(c)
0 1
1 1
1 2
2 3
3 5
5 8
8 13
13 21
21 34
34 55
55 89
89 144
144 233
233 377
377 610
610 987
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]
for loop
1
2
3
4
5
1
2
3
4
5
Packages
In [42]: vowels = [1, 2, 3, 4, 5]
for v in vowels:
print(v)
In [43]: #range(开头,结尾,步⻓):开头包含,结尾不包含,步⻓默认为1
for i in range(1, 6):
print(i)
anaconda安装 package
windows:
系统菜单打开Anaconda Prompt (Anaconda 3)
pip install 包名称 或 conda install 包名称
回⻋
Mac:
打开terminal
conda install -c anaconda 包名称
When asked Proceed, type y + 回⻋
0.049787068367863944
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2,
1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5,
2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8,
3.9, 4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9])
array([ 1.62434536, -0.61175641, -0.52817175, ..., -1.01414382,
-0.06269623, -1.43786989])
Functions
In [44]: # load the package
import numpy as np # numpy: 数学函数,数组、矩阵的运算
In [45]: t = 3
np.exp(-t) * np.cos(2*np.pi*t)
# np.exp(x): e^x
# np.cos()
# np.pi: 3.141592653589793
Out[45]:
In [46]: t1 = np.arange(0.0, 5.0, 0.1)
t1
# np.arange(start, end, step)
# 从start到end,以step为步⻓递增,形成array
# end没有包含在内
Out[46]:
In [47]: # Fixing random state for reproducibility
np.random.seed(1)
x = np.random.randn(10000)
x
# np.random.randn 以标准正态分布产⽣随机数
Out[47]:
In [48]: #⾃定义⼀个函数,⽅便后续的重复调⽤
def say_hello(name): #开头:def 函数名(参数1,参数2...):
print('Hello,', name) #结尾:输出通过函数得到的结果
Hello, James
Hello, Ben
Hello, James
Hello, Sarah
'\ndef divide(numerator, denominator, num_places):\n result = round(nu
merator/denominator, num_places)\n return result\n'
0.833
0.833
结尾输出的⽅式有两种:print(), return()。可以单独使⽤⼀个,也可以两个都使⽤
3 plus 1 is 4
4
In [49]: # 调⽤⾃定义函数
say_hello('James')
In [50]: say_hello('Ben')
In [51]: def say_hello(name = 'James'): #设置参数默认值
print('Hello,', name)
In [52]: say_hello() # The default value will be used
In [53]: say_hello('Sarah') # The default value will not be used
In [54]: def divide(numerator, denominator, num_places):
return round(numerator/denominator, num_places)
'''
def divide(numerator, denominator, num_places):
result = round(numerator/denominator, num_places)
return result
'''
Out[54]:
In [55]: divide(5, 6, 3)
Out[55]:
In [56]: divide(num_places = 3, numerator = 5, denominator = 6)
Out[56]:
In [57]: def add_1(a):
b = a + 1
print(a, "plus 1 is ", b)
return(b) #如果以return()结尾,所有⾃定义函数要执⾏的内容需要在return之前
In [58]: add_1(3)
Out[58]:
In [59]: # Example - 结合了上⾯⼏个点,⾃⼰回去看
def factorial(n):
result = 1
for x in range(1, n+1):
result *= x
return result
Enter a number: 5
The factorial of 5 is 120
6
Plot
In [61]: number = int(input('Enter a number: ')) # Must convert the input to an integer
print('The factorial of', number, 'is', factorial(number))
In [62]: # Tips: 写function的时候,先把⾥⾯的内容写了再去套!
result = 1
for x in range(1, 3+1):
result *= x

print(result)
In [63]: # load the package
import matplotlib.pyplot as plt #画图
import numpy as np
In [64]: c = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]
In [65]: # Step 0:创建图像窗⼝
plt.figure()
# # Step 1:创建坐标轴
plt.subplot()
# Step 2: 根据图像类型选择不同function
# line plot: plt.plot(x, y, "格式控制字符串")
plt.plot(c)
# # step 4:显示图像
plt.show()
In [66]: def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
In [67]: # Step 0:创建图像窗⼝
plt.figure()
# Step 1:创建坐标轴
plt.subplot(211)
# Step 2: 根据图像类型选择不同function
# line plot: plt.plot(x, y, "格式控制字符串")
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
# Step 1:创建坐标轴
plt.subplot(212)
# Step 2: 根据图像类型选择不同function
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
# step 4:显示图像
plt.show()
In [68]: np.random.seed(111111)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
In [69]: # Step 0:创建图像窗⼝
plt.figure()
# Step 1:创建坐标轴
plt.subplot()
# Step 2: 根据图像类型选择不同function
# histogram: plt.hist(x, bins)
# plt.hist(x, 50)
plt.hist(x, bins=50, density=True, facecolor='g', alpha=0.75)
# bins:分成多少个组
# density:y轴统计的是频率还是频次
# facecolor: 直⽅图的颜⾊
# alpha: 图像的透明度
# # step 4:显示图像
plt.show()
In [70]: # Step 0:创建图像窗⼝
plt.figure()
# Step 1:创建坐标轴
plt.subplot()
# Step 2: 根据图像类型选择不同function
# histogram: plt.hist(x, bins)
plt.hist(x, bins=50, density=True, facecolor='g', alpha=0.75)
# Step 3: 添加图像的细节
plt.xlabel('Smarts') # 添加X轴名称
plt.ylabel('Probability') # 添加Y轴名称
plt.title('Histogram of IQ') #添加标题
plt.text(60, .025, r'$\mu=100,\ \sigma=15$') #在图⽚中添加⽂字
plt.axis([40, 160, 0, 0.03]) #设置坐标轴
plt.grid(True) #添加参考线
# step 4:显示图像
plt.show()
Step 2 总结
Step 2 - 根据图像类型选择不同function:
plt.plot(): 折线图
plt.hist(): 直⽅图
plt.scatter(): 散点图
step 2 中出现⼏个画图的function,坐标轴上就有⼏个图,example:MktPrices,
MktPrices_Local
Pandas Package
In [71]: # Import initial libraries
import numpy as np
import pandas as pd # pandas: 主要⽤来处理⼆维数据(Dataframe)以及数据分析
In [72]: # use pandas package to extract preprocessed files from you local directory
stock1 = pd.read_csv("APT.csv")
stock1
Date Open High Low Close Adj Close Volume
0 14/6/2018 8.450000 9.080000 8.410000 9.080000 9.080000 2287478
1 15/6/2018 9.150000 9.470000 9.090000 9.120000 9.120000 7692647
2 18/6/2018 9.180000 9.180000 8.680000 8.900000 8.900000 1264673
3 19/6/2018 8.950000 9.000000 8.700000 8.800000 8.800000 1203052
4 20/6/2018 8.840000 8.940000 8.700000 8.810000 8.810000 1233112
... ... ... ... ... ... ... ...
247 5/6/2019 23.030001 24.570000 22.950001 23.900000 23.900000 2439496
248 6/6/2019 25.010000 25.200001 22.959999 23.450001 23.450001 3680154
249 7/6/2019 23.500000 24.370001 22.950001 24.170000 24.170000 2484087
250 11/6/2019 24.170000 24.170000 24.170000 24.170000 24.170000 0
251 12/6/2019 24.600000 26.490000 24.400000 25.639999 25.639999 6512239
252 rows × 7 columns
0 9.080000
1 9.120000
2 8.900000
3 8.800000
4 8.810000
...
247 23.900000
248 23.450001
249 24.170000
250 24.170000
251 25.639999
Name: Close, Length: 252, dtype: float64
0 9.080000
1 9.120000
2 8.900000
3 8.800000
4 8.810000
...
247 23.900000
248 23.450001
249 24.170000
250 24.170000
251 25.639999
Name: Close, Length: 252, dtype: float64
Out[72]:
In [73]: stock1.Close
Out[73]:
In [74]: stock1['Close']
Out[74]:
In [75]: stock1.head(5)
# stock1.tail()
Date Open High Low Close Adj Close Volume
0 14/6/2018 8.45 9.08 8.41 9.08 9.08 2287478
1 15/6/2018 9.15 9.47 9.09 9.12 9.12 7692647
2 18/6/2018 9.18 9.18 8.68 8.90 8.90 1264673
3 19/6/2018 8.95 9.00 8.70 8.80 8.80 1203052
4 20/6/2018 8.84 8.94 8.70 8.81 8.81 1233112
/var/folders/04/l2g9kr1x6s51n_mp3vxzq4bw0000gn/T/ipykernel_11064/66788415
7.py:1: FutureWarning: Dropping of nuisance columns in DataFrame reductio
ns (with 'numeric_only=None') is deprecated; in a future version this wil
l raise TypeError. Select only valid columns before calling the reductio
n.
stock1.mean() # BASIC ANALYTICS - Ed 第四周!
Open 1.676952e+01
High 1.721474e+01
Low 1.633407e+01
Close 1.673865e+01
Adj Close 1.673865e+01
Volume 2.118193e+06
dtype: float64
16.73865072222222
Finance Applications
Define functions: ForwardPrice, Pricing
95.85885906851745
Out[75]:
In [76]: stock1.mean() # BASIC ANALYTICS - Ed 第四周!
Out[76]:
In [77]: stock1.Close.mean()
Out[77]:
In [78]: # Import initial libraries
import math # 数学运算与公式
In [79]: def fwd_price_cash_income(spot, div_cash, rate, timeToMaturity):
F = (spot - div_cash) * math.exp( rate * timeToMaturity )
return F
In [80]: fwd_price_cash_income(100, 5, 0.003, 3)
Out[80]:
In [81]: # Input Variables
spot = 100 #in spot currency
rate = 0.003 #annual risk free rate
timeToMaturity = 3 #in years
div_cash = 5 #in cash as spot currency
div_yield = 0.003 #in % yield
K = 101 #K delivery price
95.85885906851745
Fwd cash income: 95.85885906851745
Plots: MktPrices, MktPrices_Local
从线上获取数据
[*********************100%***********************] 1 of 1 completed
[*********************100%***********************] 1 of 1 completed
从本地获取数据
In [82]: fwd_price_cash_income(spot, div_cash, rate, timeToMaturity)
Out[82]:
In [83]: print('Fwd cash income:', fwd_price_cash_income(spot, div_cash, rate, timeToMaturity
In [86]: from pandas_datareader import data as pdr
import yfinance as yf #yahoo finacne数据
import datetime #时间处理
In [87]: # Set date ranges
start_sp = datetime.datetime(2017, 1, 1)
end_sp = datetime.datetime(2018, 8, 1)
In [88]: # pandas reader as pdr and use uf as yahoo call
yf.pdr_override()
stock1 = pdr.get_data_yahoo('AAPL', start_sp, end_sp)
stock2 = pdr.get_data_yahoo('V', start_sp, end_sp)
In [89]: # Import initial libraries
import pandas as pd # pandas: 处理⼆维数据以及数据分析
import matplotlib.pyplot as plt
In [90]: # use pandas package to extract preprocessed files from you local directory
stock1 = pd.read_csv("APT.csv")
stock2 = pd.read_csv("V.csv")
In [91]: stock1
Date Open High Low Close Adj Close Volume
0 14/6/2018 8.450000 9.080000 8.410000 9.080000 9.080000 2287478
1 15/6/2018 9.150000 9.470000 9.090000 9.120000 9.120000 7692647
2 18/6/2018 9.180000 9.180000 8.680000 8.900000 8.900000 1264673
3 19/6/2018 8.950000 9.000000 8.700000 8.800000 8.800000 1203052
4 20/6/2018 8.840000 8.940000 8.700000 8.810000 8.810000 1233112
... ... ... ... ... ... ... ...
247 5/6/2019 23.030001 24.570000 22.950001 23.900000 23.900000 2439496
248 6/6/2019 25.010000 25.200001 22.959999 23.450001 23.450001 3680154
249 7/6/2019 23.500000 24.370001 22.950001 24.170000 24.170000 2484087
250 11/6/2019 24.170000 24.170000 24.170000 24.170000 24.170000 0
251 12/6/2019 24.600000 26.490000 24.400000 25.639999 25.639999 6512239
252 rows × 7 columns
0 9.080000
1 9.120000
2 8.900000
3 8.800000
4 8.810000
...
247 23.900000
248 23.450001
249 24.170000
250 24.170000
251 25.639999
Name: Close, Length: 252, dtype: float64
0 NaN
1 0.004405
2 -0.024123
3 -0.011236
4 0.001136
...
247 0.070789
248 -0.018828
249 0.030704
250 0.000000
251 0.060819
Name: Close, Length: 252, dtype: float64
Out[91]:
In [92]: stock1_close = stock1.Close
stock1_close
Out[92]:
In [93]: stock1_diff = stock1.Close.pct_change()
stock1_diff
# pct_change(): 当前元素与先前元素的相差百分⽐
# (9.12-9.08)/9.08 = 0.004405
Out[93]:
Rolling [window=30,center=False,axis=0,method=single]
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
...
247 25.334333
248 25.324333
249 25.336000
250 25.311000
251 25.312666
Name: Close, Length: 252, dtype: float64
16.73865072222222
5.003837856162234
145.3152775833334
10.426993494992253
In [94]: stock1_roll = stock1_close.rolling(window=30)
stock1_roll
Out[94]:
In [ ]: for i in stock1_roll:
print(i)
In [96]: stock1_roll_mean = stock1_roll.mean()
stock1_roll_mean
Out[96]:
In [97]: # show mean share price and standard deviation
print(stock1_close.mean())
print(stock1_close.std())
In [98]: # Stock2: "V.csv"
stock2_close = stock2.Close
stock2_diff = stock2.Close.pct_change()
stock2_roll = stock2_close.rolling(window=30)
stock2_roll_mean = stock2_roll.mean()
# show mean share price and standard deviation
print(stock2_close.mean())
print(stock2_close.std())
Define functions & Plots: Regression
In [99]: # Step 0:创建图像窗⼝
plt.figure()
# Step 1:创建坐标轴
plt.subplot(221)
# Step 2: 根据图像类型选择不同function
stock1_close.plot(c="b", markersize=8, label="Stock1 ",title='Stock1')
stock1_roll_mean.plot(color='red')
# Step 1:创建坐标轴
plt.subplot(222)
# Step 2: 根据图像类型选择不同function
stock1_diff.plot(c="r", markersize=8, label="Stock1", title='Stock1 %Diff'
# Step 1:创建坐标轴
plt.subplot(223)
# Step 2: 根据图像类型选择不同function
stock2_close.plot(c="b", markersize=8, label="Stock2 ",title='Stock2')
stock2_roll_mean.plot(color='red')
# Step 1:创建坐标轴
plt.subplot(224)
# Step 2: 根据图像类型选择不同function
stock2_diff.plot(c="r", markersize=8, label="Stock2 Diff", title='Stock2 %Diff'
# Step 3: ⽆
# step 4:显示图像
plt.show()
In [100… import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [101… def coefficients(x, y):
# basic calculations
n = np.size(x) # size(x): x中⼀共有多少个元素
m_x, m_y = np.mean(x), np.mean(y)
# show deviations about x
SS_xy = np.sum(y * x - n * m_y * m_x)
SS_xx = np.sum(x * x - n * m_x * m_x)
# calculating reg coefficients
b_1 = SS_xy / SS_xx
b_0 = m_y - b_1 * m_x
return (b_0, b_1)
In [102… def plotter(x, y, b):
# use our model to predict projected values
y_pred = b[0] + b[1] * x
# use scatter for closer representations
plt.scatter(x, y, color="m", marker="o", s=30)
# plotting model line as derived by this simple model
plt.plot(x, y_pred, color="g")

plt.xlabel('x')
plt.ylabel('y')

plt.show()
In [103… # Fixing random state for reproducibility
rng = np.random.RandomState(1)
print(rng.rand(50)) #rand(50): ⽣成50个[0,1)之间随机浮点数
print(rng.randn(50)) #randn(50): ⽣成50个随机数,符合标准正态分布(均值为0,⽅差为1)
[4.17022005e-01 7.20324493e-01 1.14374817e-04 3.02332573e-01
1.46755891e-01 9.23385948e-02 1.86260211e-01 3.45560727e-01
3.96767474e-01 5.38816734e-01 4.19194514e-01 6.85219500e-01
2.04452250e-01 8.78117436e-01 2.73875932e-02 6.70467510e-01
4.17304802e-01 5.58689828e-01 1.40386939e-01 1.98101489e-01
8.00744569e-01 9.68261576e-01 3.13424178e-01 6.92322616e-01
8.76389152e-01 8.94606664e-01 8.50442114e-02 3.90547832e-02
1.69830420e-01 8.78142503e-01 9.83468338e-02 4.21107625e-01
9.57889530e-01 5.33165285e-01 6.91877114e-01 3.15515631e-01
6.86500928e-01 8.34625672e-01 1.82882773e-02 7.50144315e-01
9.88861089e-01 7.48165654e-01 2.80443992e-01 7.89279328e-01
1.03226007e-01 4.47893526e-01 9.08595503e-01 2.93614148e-01
2.87775339e-01 1.30028572e-01]
[-0.6871727 -0.84520564 -0.67124613 -0.0126646 -1.11731035 0.2344157
1.65980218 0.74204416 -0.19183555 -0.88762896 -0.74715829 1.6924546
0.05080775 -0.63699565 0.19091548 2.10025514 0.12015895 0.61720311
0.30017032 -0.35224985 -1.1425182 -0.34934272 -0.20889423 0.58662319
0.83898341 0.93110208 0.28558733 0.88514116 -0.75439794 1.25286816
0.51292982 -0.29809284 0.48851815 -0.07557171 1.13162939 1.51981682
2.18557541 -1.39649634 -1.44411381 -0.50446586 0.16003707 0.87616892
0.31563495 -2.02220122 -0.30620401 0.82797464 0.23009474 0.76201118
-0.22232814 -0.20075807]
(0.04398052467557445, 0.9473755997672432)
In [104… rng = np.random.RandomState(1)
x = 10 * rng.rand(50)
y = 2 * x - 5 + rng.randn(50)
In [105… # estimating coefficients
b = coefficients(x, y)
b
Out[105]:
In [106… # plotting regression line
plotter(x, y, b)

plt.subplot(nrows, ncols, plot_number)
这个函数用来表示把 figure分成 nrows*ncols的子图表示,
nrows:子图的行数
ncols:子图的列数
plot_number索引值,表示把图画在第 plot_number个位置
plt.plot(x, y, "格式控制字符串" )
线条的格式可以用“格式控制字符串”来设置
"格式控制字符串"最多可以包括三部分, "颜色", "线型", "点型"
color=['b','g','r','c','m','y','k','w']
linestyle=['-','--','-.',':']
marker=['.',',','o','v','^','<','>','1','2','3','4','s','p','*','h','H','+','x','D','d','|','_','.',',']
除了用“格式控制字符串”,也可以用参数来控制线条的格式 e.g.
#蓝色,线宽 20,圆点,点尺寸 50,点填充红色,点边缘宽度 6,点边缘灰色
plt.plot(y,color="blue",linewidth=20,marker="o",markersize=50,
markerfacecolor="red",markeredgewidth=6,markeredgecolor="grey")
Rolling(window=3)
Window 1 window 2 window 3 window 4 window 5 window 6 window 7 window 8 window 9 window 10 
essay、essay代写