java train_java-train

java-train

java技术训练项目

spring guide

1.Building a RESTful Web Service

use @RESTController to build RESTful API

2.Scheduling Tasks

view code in spring.guide.schedule

3.Consuming a RESTful Web Service

use restTemplate to consume RESTful Web Service

4.Building Java Projects with Gradle

view this project

5.Building Java Projects with Maven

ignore this

6.Accessing Relational Data using JDBC with Spring

view code in spring.guide.springjdbc

7.Uploading Files

8.Authenticating a User with LDAP

design pattern

1.Abstract Factory

Provides one level of interface higher than the factory pattern. It is used to return one of several factories.

2.Builder

Construct a complex object from simple objects step by step.

3.Factory Method

Provides an abstraction or an interface and lets subclass or implementing classes decide which class or method should be

instantiated or called, based on the conditions or parameters given.

4.Prototype

Cloning an object by reducing the cost of creation.

5.Singleton

One instance of a class or one value accessible globally in an application.

Structural Patterns

6.Adapter

Convert the existing interfaces to a new interface to achieve compatibility and reusability of the unrelated classes

in one application. Also known as Wrapper pattern.

7.Bridge

Decouple an abstraction or interface from its implementation so that the two can vary independently.

8.Composite

Build a complex object out of elemental objects and itself like a tree structure.

9.Decorator

add additional features or behaviors to a particular instance of a class, while not modifying the other instances of same class

10.Facade

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. Wrap a complicated subsystem with a simpler interface.

11.Flyweight

Make instances of classes on the fly to improve performance efficiently, like individual characters or icons on the screen.

12.Proxy

Use a simple object to represent a complex one or provide a placeholder for another object to control access to it.

Behavioral Patterns

13.Chain of Responsibility

Let more than one object handle a request without their knowing each other. Pass the request to chained objects until

it has been handled.

14.Command

Streamlize objects by providing an interface to encapsulate a request and make the interface implemented by subclasses

in order to parameterize the clients.

15.Interpreter

Provides a definition of a macro language or syntax and parsing into objects in a program.

16.Iterator

Define an object that encapsulates details and other objects interact with such object. The relationships are loosely decoupled.

17.Mediator

Decouple the direct communication between objects by introducing a middle object, the mediator, that facilitates the communication

between the objects.

18.Memento

To record an object internal state without violating encapsulation and reclaim it later without knowledge of the original object.

19.Observer

One object changes state, all of its dependents are updated automatically.

20.State

An object's behavior change is represented by its member classes, which share the same super class.

21.Strategy

Group several algorithms in a single module to provide alternatives. Also known as policy.

22.Template Method

Provide an abstract definition for a method or a class and redefine its behavior later or on the fly without changing its structure.

23.Visitor

Define a new operation to deal with the classes of the elements without changing their structures.

好的,以下是使用Java实现的AdaBoost算法代码,用于鸢尾花分类: ```java import java.util.ArrayList; import java.util.Collections; public class AdaBoost { // 训练数据集 private ArrayList<ArrayList<Double>> dataSet; // 类别标签 private ArrayList<Integer> labels; // 弱分类器数目 private int weakNum; // 训练好的弱分类器集合 private ArrayList<WeakClassifier> weakClassifiers; // AdaBoost构造函数 public AdaBoost(ArrayList<ArrayList<Double>> dataSet, ArrayList<Integer> labels, int weakNum) { this.dataSet = dataSet; this.labels = labels; this.weakNum = weakNum; this.weakClassifiers = new ArrayList<>(); } // 训练分类器 public void train() { int size = dataSet.size(); // 初始化权重向量 ArrayList<Double> weights = new ArrayList<>(); for (int i = 0; i < size; i++) { weights.add(1.0 / size); } // 训练 weakNum 个弱分类器 for (int i = 0; i < weakNum; i++) { // 训练单个弱分类器 WeakClassifier weakClassifier = new WeakClassifier(dataSet, labels, weights); weakClassifier.train(); // 计算错误率 double error = 0.0; for (int j = 0; j < size; j++) { if (weakClassifier.predict(dataSet.get(j)) != labels.get(j)) { error += weights.get(j); } } // 计算弱分类器权重 double alpha = 0.5 * Math.log((1 - error) / error); weakClassifier.setAlpha(alpha); // 更新权重向量 for (int j = 0; j < size; j++) { if (weakClassifier.predict(dataSet.get(j)) == labels.get(j)) { weights.set(j, weights.get(j) * Math.exp(-alpha)); } else { weights.set(j, weights.get(j) * Math.exp(alpha)); } } // 归一化权重向量 double sum = 0.0; for (int j = 0; j < size; j++) { sum += weights.get(j); } for (int j = 0; j < size; j++) { weights.set(j, weights.get(j) / sum); } // 将训练好的弱分类器加入集合 weakClassifiers.add(weakClassifier); } } // 预测分类结果 public int predict(ArrayList<Double> data) { double sum = 0.0; for (WeakClassifier wc : weakClassifiers) { sum += wc.predict(data) * wc.getAlpha(); } if (sum > 0) { return 1; } else { return -1; } } // 测试分类器 public void test(ArrayList<ArrayList<Double>> testData, ArrayList<Integer> testLabels) { int errorNum = 0; int size = testData.size(); for (int i = 0; i < size; i++) { if (predict(testData.get(i)) != testLabels.get(i)) { errorNum++; } } double accuracy = 1 - (double) errorNum / size; System.out.println("Accuracy: " + accuracy); } // 主函数 public static void main(String[] args) { // 读取数据集 ArrayList<ArrayList<Double>> dataSet = Util.loadDataSet("iris.data"); // 打乱数据集顺序 Collections.shuffle(dataSet); // 获取标签 ArrayList<Integer> labels = new ArrayList<>(); for (ArrayList<Double> data : dataSet) { if (data.get(data.size() - 1) == 1) { labels.add(1); } else { labels.add(-1); } } // 划分训练集和测试集 ArrayList<ArrayList<Double>> trainData = new ArrayList<>(); ArrayList<ArrayList<Double>> testData = new ArrayList<>(); ArrayList<Integer> trainLabels = new ArrayList<>(); ArrayList<Integer> testLabels = new ArrayList<>(); for (int i = 0; i < dataSet.size(); i++) { if (i % 5 == 0) { testData.add(dataSet.get(i)); testLabels.add(labels.get(i)); } else { trainData.add(dataSet.get(i)); trainLabels.add(labels.get(i)); } } // 训练 AdaBoost 分类器 AdaBoost adaBoost = new AdaBoost(trainData, trainLabels, 10); adaBoost.train(); // 测试分类器 adaBoost.test(testData, testLabels); } } ``` 需要注意的是,此代码中的 `WeakClassifier` 类是用于实现单个弱分类器的训练和预测的,需要自行实现。同时,数据集的加载和处理部分也需要根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值