Skip to content

Commit b982c80

Browse files
authored
Add files via upload
1 parent b86020d commit b982c80

File tree

4 files changed

+647
-0
lines changed

4 files changed

+647
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Sun Jul 29 16:03:37 2018
4+
5+
@author: wzy
6+
"""
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
10+
"""
11+
函数说明:加载数据
12+
13+
Parameters:
14+
filename - 文件名
15+
16+
Returns:
17+
xArr - x数据集
18+
yArr - y数据集
19+
20+
Modify:
21+
2018-07-29
22+
"""
23+
def loadDataSet(filename):
24+
# 计算特征个数,由于最后一列为y值所以减一
25+
numFeat = len(open(filename).readline().split('\t')) - 1
26+
xArr = []
27+
yArr = []
28+
fr = open(filename)
29+
for line in fr.readlines():
30+
lineArr = []
31+
curLine = line.strip().split('\t')
32+
for i in range(numFeat):
33+
lineArr.append(float(curLine[i]))
34+
xArr.append(lineArr)
35+
yArr.append(float(curLine[-1]))
36+
return xArr, yArr
37+
38+
39+
"""
40+
函数说明:计算回归系数w
41+
42+
Parameters:
43+
xArr - x数据集
44+
yArr - y数据集
45+
46+
Returns:
47+
ws - 回归系数
48+
49+
Modify:
50+
2018-07-29
51+
"""
52+
def standRegres(xArr, yArr):
53+
xMat = np.mat(xArr)
54+
yMat = np.mat(yArr).T
55+
xTx = xMat.T * xMat
56+
# 求矩阵的行列式
57+
if np.linalg.det(xTx) == 0.0:
58+
print("矩阵为奇异矩阵,不能求逆")
59+
return
60+
# .I求逆矩阵
61+
ws = (xTx.I) * (xMat.T) * yMat
62+
return ws
63+
64+
65+
"""
66+
函数说明:绘制数据集
67+
68+
Parameters:
69+
None
70+
71+
Returns:
72+
None
73+
74+
Modify:
75+
2018-07-29
76+
"""
77+
def plotDataSet():
78+
xArr, yArr = loadDataSet('ex0.txt')
79+
ws = standRegres(xArr, yArr)
80+
xMat = np.mat(xArr)
81+
yMat = np.mat(yArr)
82+
xCopy = xMat.copy()
83+
# 排序
84+
xCopy.sort(0)
85+
yHat = xCopy * ws
86+
# 以下两行是通过corrcoef函数比较预测值和真实值的相关性
87+
# corrcoef函数得到相关系数矩阵
88+
# 得到的结果中对角线上的数据是1.0,因为yMat和自己的匹配是完美的
89+
# 而yHat1和yMat的相关系数为0.98
90+
yHat1 = xMat * ws
91+
print(np.corrcoef(yHat1.T, yMat))
92+
fig = plt.figure()
93+
ax = fig.add_subplot(111)
94+
ax.plot(xCopy[:, 1], yHat, c='red')
95+
# 绘制样本点即
96+
# flatten返回一个折叠成一维的数组。但是该函数只能适用于numpy对象,即array或者mat,普通的list列表是不行的
97+
# 矩阵.A(等效于矩阵.getA())变成了数组
98+
ax.scatter(xMat[:, 1].flatten().A[0], yMat.flatten().A[0], s=20, c='blue', alpha=.5)
99+
plt.title('DataSet')
100+
plt.xlabel('X')
101+
plt.show()
102+
103+
104+
if __name__ == '__main__':
105+
plotDataSet()
106+

LinearRegression_Project1/ex0.txt

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
1.000000 0.067732 3.176513
2+
1.000000 0.427810 3.816464
3+
1.000000 0.995731 4.550095
4+
1.000000 0.738336 4.256571
5+
1.000000 0.981083 4.560815
6+
1.000000 0.526171 3.929515
7+
1.000000 0.378887 3.526170
8+
1.000000 0.033859 3.156393
9+
1.000000 0.132791 3.110301
10+
1.000000 0.138306 3.149813
11+
1.000000 0.247809 3.476346
12+
1.000000 0.648270 4.119688
13+
1.000000 0.731209 4.282233
14+
1.000000 0.236833 3.486582
15+
1.000000 0.969788 4.655492
16+
1.000000 0.607492 3.965162
17+
1.000000 0.358622 3.514900
18+
1.000000 0.147846 3.125947
19+
1.000000 0.637820 4.094115
20+
1.000000 0.230372 3.476039
21+
1.000000 0.070237 3.210610
22+
1.000000 0.067154 3.190612
23+
1.000000 0.925577 4.631504
24+
1.000000 0.717733 4.295890
25+
1.000000 0.015371 3.085028
26+
1.000000 0.335070 3.448080
27+
1.000000 0.040486 3.167440
28+
1.000000 0.212575 3.364266
29+
1.000000 0.617218 3.993482
30+
1.000000 0.541196 3.891471
31+
1.000000 0.045353 3.143259
32+
1.000000 0.126762 3.114204
33+
1.000000 0.556486 3.851484
34+
1.000000 0.901144 4.621899
35+
1.000000 0.958476 4.580768
36+
1.000000 0.274561 3.620992
37+
1.000000 0.394396 3.580501
38+
1.000000 0.872480 4.618706
39+
1.000000 0.409932 3.676867
40+
1.000000 0.908969 4.641845
41+
1.000000 0.166819 3.175939
42+
1.000000 0.665016 4.264980
43+
1.000000 0.263727 3.558448
44+
1.000000 0.231214 3.436632
45+
1.000000 0.552928 3.831052
46+
1.000000 0.047744 3.182853
47+
1.000000 0.365746 3.498906
48+
1.000000 0.495002 3.946833
49+
1.000000 0.493466 3.900583
50+
1.000000 0.792101 4.238522
51+
1.000000 0.769660 4.233080
52+
1.000000 0.251821 3.521557
53+
1.000000 0.181951 3.203344
54+
1.000000 0.808177 4.278105
55+
1.000000 0.334116 3.555705
56+
1.000000 0.338630 3.502661
57+
1.000000 0.452584 3.859776
58+
1.000000 0.694770 4.275956
59+
1.000000 0.590902 3.916191
60+
1.000000 0.307928 3.587961
61+
1.000000 0.148364 3.183004
62+
1.000000 0.702180 4.225236
63+
1.000000 0.721544 4.231083
64+
1.000000 0.666886 4.240544
65+
1.000000 0.124931 3.222372
66+
1.000000 0.618286 4.021445
67+
1.000000 0.381086 3.567479
68+
1.000000 0.385643 3.562580
69+
1.000000 0.777175 4.262059
70+
1.000000 0.116089 3.208813
71+
1.000000 0.115487 3.169825
72+
1.000000 0.663510 4.193949
73+
1.000000 0.254884 3.491678
74+
1.000000 0.993888 4.533306
75+
1.000000 0.295434 3.550108
76+
1.000000 0.952523 4.636427
77+
1.000000 0.307047 3.557078
78+
1.000000 0.277261 3.552874
79+
1.000000 0.279101 3.494159
80+
1.000000 0.175724 3.206828
81+
1.000000 0.156383 3.195266
82+
1.000000 0.733165 4.221292
83+
1.000000 0.848142 4.413372
84+
1.000000 0.771184 4.184347
85+
1.000000 0.429492 3.742878
86+
1.000000 0.162176 3.201878
87+
1.000000 0.917064 4.648964
88+
1.000000 0.315044 3.510117
89+
1.000000 0.201473 3.274434
90+
1.000000 0.297038 3.579622
91+
1.000000 0.336647 3.489244
92+
1.000000 0.666109 4.237386
93+
1.000000 0.583888 3.913749
94+
1.000000 0.085031 3.228990
95+
1.000000 0.687006 4.286286
96+
1.000000 0.949655 4.628614
97+
1.000000 0.189912 3.239536
98+
1.000000 0.844027 4.457997
99+
1.000000 0.333288 3.513384
100+
1.000000 0.427035 3.729674
101+
1.000000 0.466369 3.834274
102+
1.000000 0.550659 3.811155
103+
1.000000 0.278213 3.598316
104+
1.000000 0.918769 4.692514
105+
1.000000 0.886555 4.604859
106+
1.000000 0.569488 3.864912
107+
1.000000 0.066379 3.184236
108+
1.000000 0.335751 3.500796
109+
1.000000 0.426863 3.743365
110+
1.000000 0.395746 3.622905
111+
1.000000 0.694221 4.310796
112+
1.000000 0.272760 3.583357
113+
1.000000 0.503495 3.901852
114+
1.000000 0.067119 3.233521
115+
1.000000 0.038326 3.105266
116+
1.000000 0.599122 3.865544
117+
1.000000 0.947054 4.628625
118+
1.000000 0.671279 4.231213
119+
1.000000 0.434811 3.791149
120+
1.000000 0.509381 3.968271
121+
1.000000 0.749442 4.253910
122+
1.000000 0.058014 3.194710
123+
1.000000 0.482978 3.996503
124+
1.000000 0.466776 3.904358
125+
1.000000 0.357767 3.503976
126+
1.000000 0.949123 4.557545
127+
1.000000 0.417320 3.699876
128+
1.000000 0.920461 4.613614
129+
1.000000 0.156433 3.140401
130+
1.000000 0.656662 4.206717
131+
1.000000 0.616418 3.969524
132+
1.000000 0.853428 4.476096
133+
1.000000 0.133295 3.136528
134+
1.000000 0.693007 4.279071
135+
1.000000 0.178449 3.200603
136+
1.000000 0.199526 3.299012
137+
1.000000 0.073224 3.209873
138+
1.000000 0.286515 3.632942
139+
1.000000 0.182026 3.248361
140+
1.000000 0.621523 3.995783
141+
1.000000 0.344584 3.563262
142+
1.000000 0.398556 3.649712
143+
1.000000 0.480369 3.951845
144+
1.000000 0.153350 3.145031
145+
1.000000 0.171846 3.181577
146+
1.000000 0.867082 4.637087
147+
1.000000 0.223855 3.404964
148+
1.000000 0.528301 3.873188
149+
1.000000 0.890192 4.633648
150+
1.000000 0.106352 3.154768
151+
1.000000 0.917886 4.623637
152+
1.000000 0.014855 3.078132
153+
1.000000 0.567682 3.913596
154+
1.000000 0.068854 3.221817
155+
1.000000 0.603535 3.938071
156+
1.000000 0.532050 3.880822
157+
1.000000 0.651362 4.176436
158+
1.000000 0.901225 4.648161
159+
1.000000 0.204337 3.332312
160+
1.000000 0.696081 4.240614
161+
1.000000 0.963924 4.532224
162+
1.000000 0.981390 4.557105
163+
1.000000 0.987911 4.610072
164+
1.000000 0.990947 4.636569
165+
1.000000 0.736021 4.229813
166+
1.000000 0.253574 3.500860
167+
1.000000 0.674722 4.245514
168+
1.000000 0.939368 4.605182
169+
1.000000 0.235419 3.454340
170+
1.000000 0.110521 3.180775
171+
1.000000 0.218023 3.380820
172+
1.000000 0.869778 4.565020
173+
1.000000 0.196830 3.279973
174+
1.000000 0.958178 4.554241
175+
1.000000 0.972673 4.633520
176+
1.000000 0.745797 4.281037
177+
1.000000 0.445674 3.844426
178+
1.000000 0.470557 3.891601
179+
1.000000 0.549236 3.849728
180+
1.000000 0.335691 3.492215
181+
1.000000 0.884739 4.592374
182+
1.000000 0.918916 4.632025
183+
1.000000 0.441815 3.756750
184+
1.000000 0.116598 3.133555
185+
1.000000 0.359274 3.567919
186+
1.000000 0.814811 4.363382
187+
1.000000 0.387125 3.560165
188+
1.000000 0.982243 4.564305
189+
1.000000 0.780880 4.215055
190+
1.000000 0.652565 4.174999
191+
1.000000 0.870030 4.586640
192+
1.000000 0.604755 3.960008
193+
1.000000 0.255212 3.529963
194+
1.000000 0.730546 4.213412
195+
1.000000 0.493829 3.908685
196+
1.000000 0.257017 3.585821
197+
1.000000 0.833735 4.374394
198+
1.000000 0.070095 3.213817
199+
1.000000 0.527070 3.952681
200+
1.000000 0.116163 3.129283

0 commit comments

Comments
 (0)