Skip to content

Commit

Permalink
Chapter 05 section 06 completed.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Dec 9, 2017
1 parent ea5e142 commit 530e737
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@


def accuracy_score(y_true, y_predict):
'''计算y_true和y_predict之间的准确率'''
"""计算y_true和y_predict之间的准确率"""
assert len(y_true) == len(y_predict), \
"the size of y_true must be equal to the size of y_predict"

return np.sum(y_true == y_predict) / len(y_true)


def mean_squared_error(y_true, y_predict):
'''计算y_true和y_predict之间的MSE'''
"""计算y_true和y_predict之间的MSE"""
assert len(y_true) == len(y_predict), \
"the size of y_true must be equal to the size of y_predict"

return np.sum((y_true - y_predict)**2) / len(y_true)


def root_mean_squared_error(y_true, y_predict):
'''计算y_true和y_predict之间的RMSE'''
"""计算y_true和y_predict之间的RMSE"""

return sqrt(mean_squared_error(y_true, y_predict))


def mean_absolute_error(y_true, y_predict):
'''计算y_true和y_predict之间的RMSE'''
"""计算y_true和y_predict之间的RMSE"""

return np.sum(np.absolute(y_true - y_predict)) / len(y_true)
19 changes: 10 additions & 9 deletions 05-Linear-Regression/06-R-Squared/06-R-Squared.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,19 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.61293168039373225"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
"ename": "NameError",
"evalue": "name 'y_test' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-2-a7a5d5c1ca17>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mplayML\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmetrics\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mmean_squared_error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;36m1\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mmean_squared_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my_test\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_predict\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my_test\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'y_test' is not defined"
]
}
],
"source": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def _predict(self, x_single):
"""给定单个待预测数据x,返回x的预测结果值"""
return self.a_ * x_single + self.b_

def score(self, X_test, y_test):
"""根据测试数据集 X_test 和 y_test 确定当前模型的准确度"""
def score(self, x_test, y_test):
"""根据测试数据集 x_test 和 y_test 确定当前模型的准确度"""

y_predict = self.predict(X_test)
y_predict = self.predict(x_test)
return r2_score(y_test, y_predict)

def __repr__(self):
Expand Down
10 changes: 5 additions & 5 deletions 05-Linear-Regression/06-R-Squared/playML/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@


def accuracy_score(y_true, y_predict):
'''计算y_true和y_predict之间的准确率'''
"""计算y_true和y_predict之间的准确率"""
assert len(y_true) == len(y_predict), \
"the size of y_true must be equal to the size of y_predict"

return np.sum(y_true == y_predict) / len(y_true)


def mean_squared_error(y_true, y_predict):
'''计算y_true和y_predict之间的MSE'''
"""计算y_true和y_predict之间的MSE"""
assert len(y_true) == len(y_predict), \
"the size of y_true must be equal to the size of y_predict"

return np.sum((y_true - y_predict)**2) / len(y_true)


def root_mean_squared_error(y_true, y_predict):
'''计算y_true和y_predict之间的RMSE'''
"""计算y_true和y_predict之间的RMSE"""

return sqrt(mean_squared_error(y_true, y_predict))


def mean_absolute_error(y_true, y_predict):
'''计算y_true和y_predict之间的RMSE'''
"""计算y_true和y_predict之间的RMSE"""
assert len(y_true) == len(y_predict), \
"the size of y_true must be equal to the size of y_predict"

return np.sum(np.absolute(y_true - y_predict)) / len(y_true)


def r2_score(y_true, y_predict):
'''计算y_true和y_predict之间的R Square'''
"""计算y_true和y_predict之间的R Square"""

return 1 - mean_squared_error(y_true, y_predict)/np.var(y_true)
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def _predict(self, x_single):
"""给定单个待预测数据x,返回x的预测结果值"""
return self.a_ * x_single + self.b_

def score(self, X_test, y_test):
"""根据测试数据集 X_test 和 y_test 确定当前模型的准确度"""
def score(self, x_test, y_test):
"""根据测试数据集 x_test 和 y_test 确定当前模型的准确度"""

y_predict = self.predict(X_test)
y_predict = self.predict(x_test)
return r2_score(y_test, y_predict)

def __repr__(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@


def accuracy_score(y_true, y_predict):
'''计算y_true和y_predict之间的准确率'''
"""计算y_true和y_predict之间的准确率"""
assert len(y_true) == len(y_predict), \
"the size of y_true must be equal to the size of y_predict"

return np.sum(y_true == y_predict) / len(y_true)


def mean_squared_error(y_true, y_predict):
'''计算y_true和y_predict之间的MSE'''
"""计算y_true和y_predict之间的MSE"""
assert len(y_true) == len(y_predict), \
"the size of y_true must be equal to the size of y_predict"

return np.sum((y_true - y_predict)**2) / len(y_true)


def root_mean_squared_error(y_true, y_predict):
'''计算y_true和y_predict之间的RMSE'''
"""计算y_true和y_predict之间的RMSE"""

return sqrt(mean_squared_error(y_true, y_predict))


def mean_absolute_error(y_true, y_predict):
'''计算y_true和y_predict之间的RMSE'''
"""计算y_true和y_predict之间的RMSE"""
assert len(y_true) == len(y_predict), \
"the size of y_true must be equal to the size of y_predict"

return np.sum(np.absolute(y_true - y_predict)) / len(y_true)


def r2_score(y_true, y_predict):
'''计算y_true和y_predict之间的R Square'''
"""计算y_true和y_predict之间的R Square"""

return 1 - mean_squared_error(y_true, y_predict)/np.var(y_true)

0 comments on commit 530e737

Please sign in to comment.