Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 324

codenotsleep/Python

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (76)
master
fix-ruff
pipx-install-pre-commit-ruff
ruff-rule-ISC001
Re-enable-tests
Rename-is_palindrome.py-to-is_int_palindrome.py
Add-more-ruff-rules
pre-commit-ci-update-config
ruff
atbash.py-Tighten-up-the-benchmarks
prime_numbers.py-Tighten-up-the-benchmarks
dynamic_programming
maths/sum_of_digits.py-Streamline-benchmarks
maths/number_of_digits.py-Streamline-benchmarks
Python-3.11
7804-improve-the-maths-add-functionality-and-tests
7782-create-subtraction-method-in-maths-folder
quantum_random.py.DISABLED.txt
quantum_random.py.disabled
revert-7349-patch-4
master
Branches (76)
master
fix-ruff
pipx-install-pre-commit-ruff
ruff-rule-ISC001
Re-enable-tests
Rename-is_palindrome.py-to-is_int_palindrome.py
Add-more-ruff-rules
pre-commit-ci-update-config
ruff
atbash.py-Tighten-up-the-benchmarks
prime_numbers.py-Tighten-up-the-benchmarks
dynamic_programming
maths/sum_of_digits.py-Streamline-benchmarks
maths/number_of_digits.py-Streamline-benchmarks
Python-3.11
7804-improve-the-maths-add-functionality-and-tests
7782-create-subtraction-method-in-maths-folder
quantum_random.py.DISABLED.txt
quantum_random.py.disabled
revert-7349-patch-4
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (76)
master
fix-ruff
pipx-install-pre-commit-ruff
ruff-rule-ISC001
Re-enable-tests
Rename-is_palindrome.py-to-is_int_palindrome.py
Add-more-ruff-rules
pre-commit-ci-update-config
ruff
atbash.py-Tighten-up-the-benchmarks
prime_numbers.py-Tighten-up-the-benchmarks
dynamic_programming
maths/sum_of_digits.py-Streamline-benchmarks
maths/number_of_digits.py-Streamline-benchmarks
Python-3.11
7804-improve-the-maths-add-functionality-and-tests
7782-create-subtraction-method-in-maths-folder
quantum_random.py.DISABLED.txt
quantum_random.py.disabled
revert-7349-patch-4
Python
/
machine_learning
/
gradient_descent.py
Python
/
machine_learning
/
gradient_descent.py
gradient_descent.py 4.29 KB
Copy Edit Raw Blame History
pre-commit-ci[bot] authored 2023年08月29日 21:18 +08:00 . [pre-commit.ci] pre-commit autoupdate (#9013)
"""
Implementation of gradient descent algorithm for minimizing cost of a linear hypothesis
function.
"""
import numpy
# List of input, output pairs
train_data = (
((5, 2, 3), 15),
((6, 5, 9), 25),
((11, 12, 13), 41),
((1, 1, 1), 8),
((11, 12, 13), 41),
)
test_data = (((515, 22, 13), 555), ((61, 35, 49), 150))
parameter_vector = [2, 4, 1, 5]
m = len(train_data)
LEARNING_RATE = 0.009
def _error(example_no, data_set="train"):
"""
:param data_set: train data or test data
:param example_no: example number whose error has to be checked
:return: error in example pointed by example number.
"""
return calculate_hypothesis_value(example_no, data_set) - output(
example_no, data_set
)
def _hypothesis_value(data_input_tuple):
"""
Calculates hypothesis function value for a given input
:param data_input_tuple: Input tuple of a particular example
:return: Value of hypothesis function at that point.
Note that there is an 'biased input' whose value is fixed as 1.
It is not explicitly mentioned in input data.. But, ML hypothesis functions use it.
So, we have to take care of it separately. Line 36 takes care of it.
"""
hyp_val = 0
for i in range(len(parameter_vector) - 1):
hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
hyp_val += parameter_vector[0]
return hyp_val
def output(example_no, data_set):
"""
:param data_set: test data or train data
:param example_no: example whose output is to be fetched
:return: output for that example
"""
if data_set == "train":
return train_data[example_no][1]
elif data_set == "test":
return test_data[example_no][1]
return None
def calculate_hypothesis_value(example_no, data_set):
"""
Calculates hypothesis value for a given example
:param data_set: test data or train_data
:param example_no: example whose hypothesis value is to be calculated
:return: hypothesis value for that example
"""
if data_set == "train":
return _hypothesis_value(train_data[example_no][0])
elif data_set == "test":
return _hypothesis_value(test_data[example_no][0])
return None
def summation_of_cost_derivative(index, end=m):
"""
Calculates the sum of cost function derivative
:param index: index wrt derivative is being calculated
:param end: value where summation ends, default is m, number of examples
:return: Returns the summation of cost derivative
Note: If index is -1, this means we are calculating summation wrt to biased
parameter.
"""
summation_value = 0
for i in range(end):
if index == -1:
summation_value += _error(i)
else:
summation_value += _error(i) * train_data[i][0][index]
return summation_value
def get_cost_derivative(index):
"""
:param index: index of the parameter vector wrt to derivative is to be calculated
:return: derivative wrt to that index
Note: If index is -1, this means we are calculating summation wrt to biased
parameter.
"""
cost_derivative_value = summation_of_cost_derivative(index, m) / m
return cost_derivative_value
def run_gradient_descent():
global parameter_vector
# Tune these values to set a tolerance value for predicted output
absolute_error_limit = 0.000002
relative_error_limit = 0
j = 0
while True:
j += 1
temp_parameter_vector = [0, 0, 0, 0]
for i in range(len(parameter_vector)):
cost_derivative = get_cost_derivative(i - 1)
temp_parameter_vector[i] = (
parameter_vector[i] - LEARNING_RATE * cost_derivative
)
if numpy.allclose(
parameter_vector,
temp_parameter_vector,
atol=absolute_error_limit,
rtol=relative_error_limit,
):
break
parameter_vector = temp_parameter_vector
print(("Number of iterations:", j))
def test_gradient_descent():
for i in range(len(test_data)):
print(("Actual output value:", output(i, "test")))
print(("Hypothesis output:", calculate_hypothesis_value(i, "test")))
if __name__ == "__main__":
run_gradient_descent()
print("\nTesting gradient descent for a linear hypothesis function.\n")
test_gradient_descent()
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

Python 算法集
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/codenotsleep/Python.git
git@gitee.com:codenotsleep/Python.git
codenotsleep
Python
Python
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /