GithubHelp home page GithubHelp logo

tishacy / machine-learning-course-codes Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 11.43 MB

机器学习的课程代码以及两个模块,neuralnetwork模块用于建立神经网络,k_means模块用于实现K-means算法

License: MIT License

Python 40.48% Jupyter Notebook 59.52%

machine-learning-course-codes's Introduction

Machine Learning 课程学习代码

这是上个暑假学习 Andrew Ng 的 Machine Learning 课程的代码,课程偏理论,所以在学习的同时用python将课程中的算法实现了一下。算法实现只使用numpy库,可视化使用matplotlib,一些数据集使用sklearn.datasets。

  • code文件夹中带01-14编号的是普通课程代码,剩余两个neuralnetwork.pyk_means.py是两个模块。
  • 代码用于学习交流。

neuralnetwork.py 模块

由于课程需要多次写神经网络,所以写了该模块,用于快速生成、训练神经网络。

1. 建立一个神经网络

该模块含有NeuralNetwork类,可以轻松生成一个神经网络。建立一个NeuralNetwork实例需要4个参数:

  • num_inputs:输入层结点数
  • num_hidden_layers:隐藏层层数
  • num_nodes_per_hidden:每个隐藏层的节点数(只支持所有的隐藏层有相同的节点数)
  • num_outputs:输出层节点数

示例:建立输入层3个结点,2个隐藏层,每个隐藏层含有4个结点,输出层1个结点的神经网络,如下图所示。

neural network

​ 图片来源于google.

from neuralnetwork import NeuralNetwork

# 建立输入层3个结点,2个隐藏层,每个隐藏层含有4个结点,输出层1个结点的神经网络
nn = NeuralNetwork(3, 2, 4, 1)

2. 训练一个神经网络

NeuralNetwork 目前仅支持梯度下降法,有gradient_descentmethod。

# 训练神经网络nn
Theta, steps, Js = nn.gradient_descent(X,   # input数据
                Y,                          # label数据
                ini_Theta,                  # 初始化的神经网络参数矩阵(建立NeuralNetwork示例的时候已经初始化好了,直接使用nn.ini_Theta即可)
                alpha=0.01,                 # 学习率
                lamda=1,                    # 正则化项
                exponential_decay=False,    # 是否用指数下降的学习率
                threshold=1e-7)	            # 结束训练的损失函数阈值

训练完成后会返回:训练后的神经网络参数矩阵(Theta),训练过程中的训练步数(steps)以及训练过程中的损失函数值列表(Js

3. 测试训练后的神经网络

神经网络训练后,只需要将测试数据、训练后的神经网络参数传入forward_propagationmethod中,进行一次前向传播即可得到训练后的结果。

# 测试神经网络nn
h, A = nn.forward_propagation(X_test,       # 测试数据
                              Theta)        # 训练后的神经网络参数

返回的h即为输出层的结果,A为前向传播过程中隐藏层各个节点的激活值。

4. 示例

具体代码见 09.0_test_for_Neural_Network_module.py

demo

k_means.py模块

1. 实现K_means算法

主要提供 K_means 函数,可以方便的实现K_means算法。

from k_means import K_means

# 对未标记的input数据进行K-means算法
labels, mus, J = K_means(X,         # 未标记的input数据
                         K,         # 需要标记的种类数
                         times)     # 进行多少步算法迭代

该函数返回三个参数:labels是对于input数据的标记结果,mus是对于K个种类的质心坐标,J是标记结果对应的损失函数值。

2. 示例

具体代码见 k_means.py 中的 test函数。

Already get dataset
the 1 times, J = 4.911510
the 2 times, J = 4.911510
the 3 times, J = 4.910132
the 4 times, J = 4.911510
the 5 times, J = 4.911510

Optimized J = 4.910132, costs 0.090155 s
Clusters' coordinates:
 [[3.08627186 2.98693656]
 [0.97862753 1.11267062]]

K-means demo

参考文献

[1]. 吴恩达机器学习课程 https://www.coursera.org/learn/machine-learning

License

Copyright (c) 2019 tishacy.

Licensed under the MIT License.

machine-learning-course-codes's People

Contributors

dependabot[bot] avatar tishacy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.