谱聚类算法java代码,python谱聚类算法
Java用查找算法的一段代码如下: 其中boolean A=false; if(name.equals(arr[i])) 麻烦解释一下 尽量直白
数组从第一个开始比较,完全相同(当前数组值和输入值一模一样)A就赋值为true;不一样A的值不变
成都创新互联是一家专注于网站设计、网站制作与策划设计,上犹网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:上犹等地区。上犹做网站价格咨询:18980820575
如何用Java写出泰森多边形算法代码???我找了好多资源表示无解呀?
package com.wangyin.seapay.loginkgo;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.process.Process;
import org.geotools.process.ProcessException;
import org.geotools.process.ProcessFactory;
import org.geotools.process.spatialstatistics.core.Params;
import org.geotools.process.spatialstatistics.enumeration.ThiessenAttributeMode;
import org.geotools.process.spatialstatistics.operations.ThiessenPolygonOperation;
import org.geotools.text.Text;
import org.geotools.util.NullProgressListener;
import org.geotools.util.logging.Logging;
import org.opengis.util.ProgressListener;
import com.vividsolutions.jts.geom.Geometry;
/**
* Created by hanxiaofei on 2018/4/11.
*/
public class ThiessenPolygonProcess extends AbstractStatisticsProcess {
protected static final Logger LOGGER = Logging.getLogger(ThiessenPolygonProcess.class);
private boolean started = false;
public ThiessenPolygonProcess(ProcessFactory factory) {
super(factory);
}
public ProcessFactory getFactory() {
return factory;
}
public static SimpleFeatureCollection process(SimpleFeatureCollection inputFeatures,
ThiessenAttributeMode attributes, Geometry clipArea, ProgressListener monitor) {
MapString, Object map = new HashMapString, Object();
map.put(ThiessenPolygonProcessFactory.inputFeatures.key, inputFeatures);
map.put(ThiessenPolygonProcessFactory.attributes.key, attributes);
map.put(ThiessenPolygonProcessFactory.clipArea.key, clipArea);
Process process = new ThiessenPolygonProcess(null);
MapString, Object resultMap;
try {
resultMap = process.execute(map, monitor);
return (SimpleFeatureCollection) resultMap
.get(ThiessenPolygonProcessFactory.RESULT.key);
} catch (ProcessException e) {
LOGGER.log(Level.FINER, e.getMessage(), e);
}
return null;
}
@Override
public MapString, Object execute(MapString, Object input, ProgressListener monitor)
throws ProcessException {
if (started)
throw new IllegalStateException("Process can only be run once");
started = true;
if (monitor == null)
monitor = new NullProgressListener();
try {
monitor.started();
monitor.setTask(Text.text("Grabbing arguments"));
monitor.progress(10.0f);
SimpleFeatureCollection inputFeatures = (SimpleFeatureCollection) Params.getValue(
input, ThiessenPolygonProcessFactory.inputFeatures, null);
if (inputFeatures == null) {
throw new NullPointerException("inputFeatures parameter required");
}
ThiessenAttributeMode attributes = (ThiessenAttributeMode) Params.getValue(input,
ThiessenPolygonProcessFactory.attributes,
ThiessenPolygonProcessFactory.attributes.sample);
Geometry clipArea = (Geometry) Params.getValue(input,
ThiessenPolygonProcessFactory.clipArea, null);
monitor.setTask(Text.text("Processing ..."));
monitor.progress(25.0f);
if (monitor.isCanceled()) {
return null; // user has canceled this operation
}
// start process
ThiessenPolygonOperation operation = new ThiessenPolygonOperation();
operation.setAttributeMode(attributes);
if (clipArea != null) {
operation.setClipArea(clipArea);
}
SimpleFeatureCollection resultFc = operation.execute(inputFeatures);
// end process
monitor.setTask(Text.text("Encoding result"));
monitor.progress(90.0f);
MapString, Object resultMap = new HashMapString, Object();
resultMap.put(ThiessenPolygonProcessFactory.RESULT.key, resultFc);
monitor.complete(); // same as 100.0f
return resultMap;
} catch (Exception eek) {
monitor.exceptionOccurred(eek);
return null;
} finally {
monitor.dispose();
}
}
}
k-means聚类算法的java代码实现文本聚类
K-MEANS算法:
k-means 算法接受输入量 k ;然后将n个数据对象划分为 k个聚类以便使得所获得的聚类满足:同一聚类中的对象相似度较高;而不同聚类中的对象相似度较小。聚类相似度是利用各聚类中对象的均值所获得一个“中心对象”(引力中心)来进行计算的。
k-means 算法的工作过程说明如下:首先从n个数据对象任意选择 k 个对象作为初始聚类中心;而对于所剩下其它对象,则根据它们与这些聚类中心的相似度(距离),分别将它们分配给与其最相似的(聚类中心所代表的)聚类;然后再计算每个所获新聚类的聚类中心(该聚类中所有对象的均值);不断重复这一过程直到标准测度函数开始收敛为止。一般都采用均方差作为标准测度函数. k个聚类具有以下特点:各聚类本身尽可能的紧凑,而各聚类之间尽可能的分开。
具体如下:
输入:k, data[n];
(1) 选择k个初始中心点,例如c[0]=data[0],…c[k-1]=data[k-1];
(2) 对于data[0]….data[n], 分别与c[0]…c[n-1]比较,假定与c[i]差值最少,就标记为i;
(3) 对于所有标记为i点,重新计算c[i]=/标记为i的个数;
(4) 重复(2)(3),直到所有c[i]值的变化小于给定阈值。
算法实现起来应该很容易,就不帮你编写代码了。
java代码怎么实现计算图像二值连通区域的质心
一:几何距(Geometric Moments)知识与质心寻找原理
1. Image Moments是图像处理中非常有用的算法,可以用来计算区域图像的质心,方向等几何特性,同时Mpq的高阶具有旋转不变性,可以用来实现图像比较分类,正是因为Moments有这些特性,很多手绘油画效果也会基于该算法来模拟实现。它的数学表达为:
它的低阶M00,M01, M10可以用来计算质心,中心化以后M11,M02,M20可以用来计算区域的方向/角度
2. 什么是质心
就是通过该点,区域达到一种质量上的平衡状态,可能物理学上讲的比较多,简单点的说就是规则几何物体的中心,不规则的可以通过挂绳子的方法来寻找。
二:算法流程
1. 输入图像转换为二值图像
2. 通过连通组件标记算法找到所有的连通区域,并分别标记
3. 对每个连通区域运用计算几何距算法得到质心
4. 用不同颜色绘制连通区域与质心,输出处理后图像
三:算法效果
左边为原图, 右边蓝色为连通组件标记算法处理以后结果,白色点为质心
四:关键代码解析
1. 计算几何距算法代码
doublem00 = moments(pixels, width, height, 0, 0);
doublexCr = moments(pixels, width, height, 1, 0) / m00;// row
doubleyCr = moments(pixels, width, height, 0, 1) / m00;// column
return new double[]{xCr, yCr};
如何用70行Java代码实现神经网络算法
如何用70行Java代码实现神经网络算法
import java.util.Random;
public class BpDeep{
public double[][] layer;//神经网络各层节点
public double[][] layerErr;//神经网络各节点误差
public double[][][] layer_weight;//各层节点权重
public double[][][] layer_weight_delta;//各层节点权重动量
public double mobp;//动量系数
public double rate;//学习系数
public BpDeep(int[] layernum, double rate, double mobp){
this.mobp = mobp;
this.rate = rate;
layer = new double[layernum.length][];
layerErr = new double[layernum.length][];
layer_weight = new double[layernum.length][][];
layer_weight_delta = new double[layernum.length][][];
Random random = new Random();
for(int l=0;llayernum.length;l++){
layer[l]=new double[layernum[l]];
layerErr[l]=new double[layernum[l]];
if(l+1layernum.length){
layer_weight[l]=new double[layernum[l]+1][layernum[l+1]];
layer_weight_delta[l]=new double[layernum[l]+1][layernum[l+1]];
for(int j=0;jlayernum[l]+1;j++)
for(int i=0;ilayernum[l+1];i++)
layer_weight[l][j][i]=random.nextDouble();//随机初始化权重
}
}
}
//逐层向前计算输出
public double[] computeOut(double[] in){
for(int l=1;llayer.length;l++){
for(int j=0;jlayer[l].length;j++){
double z=layer_weight[l-1][layer[l-1].length][j];
for(int i=0;ilayer[l-1].length;i++){
layer[l-1][i]=l==1?in[i]:layer[l-1][i];
z+=layer_weight[l-1][i][j]*layer[l-1][i];
}
layer[l][j]=1/(1+Math.exp(-z));
}
}
return layer[layer.length-1];
}
//逐层反向计算误差并修改权重
public void updateWeight(double[] tar){
int l=layer.length-1;
for(int j=0;jlayerErr[l].length;j++)
layerErr[l][j]=layer[l][j]*(1-layer[l][j])*(tar[j]-layer[l][j]);
while(l--0){
for(int j=0;jlayerErr[l].length;j++){
double z = 0.0;
for(int i=0;ilayerErr[l+1].length;i++){
z=z+l0?layerErr[l+1][i]*layer_weight[l][j][i]:0;
layer_weight_delta[l][j][i]= mobp*layer_weight_delta[l][j][i]+rate*layerErr[l+1][i]*layer[l][j];//隐含层动量调整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隐含层权重调整
if(j==layerErr[l].length-1){
layer_weight_delta[l][j+1][i]= mobp*layer_weight_delta[l][j+1][i]+rate*layerErr[l+1][i];//截距动量调整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距权重调整
}
}
layerErr[l][j]=z*layer[l][j]*(1-layer[l][j]);//记录误差
}
}
}
public void train(double[] in, double[] tar){
double[] out = computeOut(in);
updateWeight(tar);
}
}
求ECDSA的Java代码
【方案1】
package ECDSA;
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class Ecdsa {
private static String src = "hello berber" ;
public static void main(String []args){
jdkECDSA();
}
public static void jdkECDSA(){
// 1.初始化密钥
try{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
keyPairGenerator.initialize(256);
KeyPair keyPair = keyPairGenerator.generateKeyPair() ;
ECPublicKey ecPublicKey = (ECPublicKey)keyPair.getPublic() ;
ECPrivateKey ecPrivateKey = (ECPrivateKey)keyPair.getPrivate() ;
// 执行签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("EC") ;
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec) ;
Signature signature = Signature.getInstance("SHA1withECDSA");
signature.initSign(privateKey);
signature.update(src.getBytes());
byte []arr = signature.sign();
System.out.println("jdk ecdsa sign :"+ HexBin.encode(arr));
// 验证签名
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
keyFactory = KeyFactory.getInstance("EC");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
signature = Signature.getInstance("SHA1withECDSA");
signature.initVerify(publicKey);
signature.update(src.getBytes());
boolean bool = signature.verify(arr);
System.out.println("jdk ecdsa verify:"+bool);
}catch(Exception e){
}
}
}
Java数字签名——ECDSA算法
【方案2】
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
new MyTest().getSign();
}
void getSign() {
// Get the instance of the Key Generator with "EC" algorithm
try {
KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec kpgparams = new ECGenParameterSpec("secp256r1");
g.initialize(kpgparams);
KeyPair pair = g.generateKeyPair();
// Instance of signature class with SHA256withECDSA algorithm
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initSign(pair.getPrivate());
System.out.println("Private Keys is::" + pair.getPrivate());
System.out.println("Public Keys is::" + pair.getPublic());
String msg = "text ecdsa with sha256";//getSHA256(msg)
ecdsaSign.update((msg + pair.getPrivate().toString())
.getBytes("UTF-8"));
byte[] signature = ecdsaSign.sign();
System.out.println("Signature is::"
+ new BigInteger(1, signature).toString(16));
// Validation
ecdsaSign.initVerify(pair.getPublic());
ecdsaSign.update(signature);
if (ecdsaSign.verify(signature))
System.out.println("valid");
else
System.out.println("invalid!!!!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}}
java – 使用secp256r1曲线和SHA256算法生
怎么验证生成的Ecdsa签名是正确的呢,可以看下这篇文章:RSA,ECC,Ecdsa,国密SM2的签名,验签,加密
网页标题:谱聚类算法java代码,python谱聚类算法
网页地址:http://myzitong.com/article/hohhdj.html