python学习【1】
学习目标:
回顾python知识点,重新学习《Machine learning》学习内容:
1、 复习了input,print,while,for等基本语法 2、 复习多元线性回归学习产出:
1、 小作业print(''' へ /|
/\7 ∠_/
/ │ / /
│ Z _,< / /`ヽ
│ ヽ / 〉
Y ` / /
イ● 、 ● ⊂⊃〈 /
() へ | \〈
>ー 、_ ィ │ //
/ へ / ノ<| \\
ヽ_ノ (_/ │//
7 |/
>―r ̄ ̄`ー―_''')
2、多元线性回归
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('data.csv')
x, y = data.iloc[:, :-1].values, data.iloc[:, -1].values
print(x)
print(y)
theta = np.linalg.inv(x.T.dot(x) + 0.01 * np.identity(x.shape[1])).dot(x.T).dot(y)
print(theta)
# error metrics: MAE, RMSE
y_hat = x.dot(theta)
difference = y - y_hat
MAE = np.mean(np.abs(difference))
RMSE = np.sqrt(np.mean(difference * difference))
print(MAE, RMSE)
x_axis = np.array([_ + 1 for _ in range(x.shape[0])])
plt.plot(y, y_hat, 'ro')
plt.show()
'''
C:\Anaconda3\python.exe E:/workspace/pythontest/mlr.py
[[ 1. 0.4 53. 158. ]
[ 1. 0.4 23. 163. ]
[ 1. 3.1 19. 37. ]
[ 1. 0.6 34. 157. ]
[ 1. 4.7 24. 59. ]
[ 1. 1.7 65. 123. ]
[ 1. 9.4 44. 46. ]
[ 1. 10.1 31. 117. ]
[ 1. 11.6 29. 173. ]
[ 1. 12.6 58. 112. ]
[ 1. 10.9 37. 111. ]
[ 1. 23.1 46. 114. ]
[ 1. 23.1 50. 134. ]
[ 1. 21.6 44. 73. ]
[ 1. 23.1 56. 168. ]
[ 1. 1.9 36. 143. ]
[ 1. 26.8 58. 202. ]
[ 1. 29.9 51. 124. ]]
[ 64 60 71 61 54 77 81 93 93 51 76 96 77 93 95 54 168 99]
[43.29999297 1.78393583 -0.07845679 0.16219113]
13.33342931320994 17.612577859851232
Process finished with exit code 0
'''