首页 文章 精选 留言 我的

精选列表

搜索[demo],共9481篇文章
优秀的个人博客,低调大师

Storm实现数字累加Demo

1 import java.util.Map; 2 3 import backtype.storm.Config; 4 import backtype.storm.LocalCluster; 5 import backtype.storm.spout.SpoutOutputCollector; 6 import backtype.storm.task.OutputCollector; 7 import backtype.storm.task.TopologyContext; 8 import backtype.storm.topology.OutputFieldsDeclarer; 9 import backtype.storm.topology.TopologyBuilder; 10 import backtype.storm.topology.base.BaseRichBolt; 11 import backtype.storm.topology.base.BaseRichSpout; 12 import backtype.storm.tuple.Fields; 13 import backtype.storm.tuple.Tuple; 14 import backtype.storm.tuple.Values; 15 import backtype.storm.utils.Utils; 16 17 /** 18 * 数字累加求和 19 * 先添加storm依赖 20 * 21 * @author Administrator 22 * 23 */ 24 public class LocalTopologySum { 25 26 27 /** 28 * spout需要继承baserichspout,实现未实现的方法 29 * @author Administrator 30 * 31 */ 32 public static class MySpout extends BaseRichSpout{ 33 private Map conf; 34 private TopologyContext context; 35 private SpoutOutputCollector collector; 36 37 /** 38 * 初始化方法,只会执行一次 39 * 在这里面可以写一个初始化的代码 40 * Map conf:其实里面保存的是topology的一些配置信息 41 * TopologyContext context:topology的上下文,类似于servletcontext 42 * SpoutOutputCollector collector:发射器,负责向外发射数据(tuple) 43 */ 44 @Override 45 public void open(Map conf, TopologyContext context, 46 SpoutOutputCollector collector) { 47 this.conf = conf; 48 this.context = context; 49 this.collector = collector; 50 } 51 52 int num = 1; 53 /** 54 * 这个方法是spout中最重要的方法, 55 * 这个方法会被storm框架循环调用,可以理解为这个方法是在一个while循环之内 56 * 每调用一次,会向外发射一条数据 57 */ 58 @Override 59 public void nextTuple() { 60 System.out.println("spout发射:"+num); 61 //把数据封装到values中,称为一个tuple,发射出去 62 this.collector.emit(new Values(num++)); 63 Utils.sleep(1000); 64 } 65 66 /** 67 * 声明输出字段 68 */ 69 @Override 70 public void declareOutputFields(OutputFieldsDeclarer declarer) { 71 //给values中的数据起个名字,方便后面的bolt从这个values中取数据 72 //fields中定义的参数和values中传递的数值是一一对应的 73 declarer.declare(new Fields("num")); 74 } 75 76 } 77 78 79 /** 80 * 自定义bolt需要实现baserichbolt 81 * @author Administrator 82 * 83 */ 84 public static class MyBolt extends BaseRichBolt{ 85 private Map stormConf; 86 private TopologyContext context; 87 private OutputCollector collector; 88 89 /** 90 * 和spout中的open方法意义一样 91 */ 92 @Override 93 public void prepare(Map stormConf, TopologyContext context, 94 OutputCollector collector) { 95 this.stormConf = stormConf; 96 this.context = context; 97 this.collector = collector; 98 } 99 100 int sum = 0; 101 /** 102 * 是bolt中最重要的方法,当spout发射一个tuple出来,execute也会被调用,需要对spout发射出来的tuple进行处理 103 */ 104 @Override 105 public void execute(Tuple input) { 106 //input.getInteger(0);//也可以根据角标获取tuple中的数据 107 Integer value = input.getIntegerByField("num"); 108 sum+=value; 109 System.out.println("和:"+sum); 110 } 111 112 /** 113 * 声明输出字段 114 */ 115 @Override 116 public void declareOutputFields(OutputFieldsDeclarer declarer) { 117 //在这没必要定义了,因为execute方法中没有向外发射tuple,所以就不需要声明了。 118 //如果nextTuple或者execute方法中向外发射了tuple,那么declareOutputFields必须要声明,否则不需要声明 119 } 120 121 } 122 /** 123 * 注意:在组装topology的时候,组件的id在定义的时候,名称不能以__开头。__是系统保留的 124 * @param args 125 */ 126 public static void main(String[] args) { 127 //组装topology 128 TopologyBuilder topologyBuilder = new TopologyBuilder(); 129 topologyBuilder.setSpout("spout1", new MySpout()); 130 //.shuffleGrouping("spout1"); 表示让MyBolt接收MySpout发射出来的tuple 131 topologyBuilder.setBolt("bolt1", new MyBolt()).shuffleGrouping("spout1"); 132 133 //创建本地storm集群 134 LocalCluster localCluster = new LocalCluster(); 135 Config config = new Config(); 136 localCluster.submitTopology("sumTopology", config, topologyBuilder.createTopology()); 137 } 138 139 } 本文转自SummerChill博客园博客,原文链接:http://www.cnblogs.com/DreamDrive/p/5774957.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android:系统demo布局汇总

1.CheckedView单选listview:android.R.layout.simple_list_item_single_choice 2.CheckedView多选listview:android.R.layout.simple_list_item_multiple_choice 3.TextView布局:android.R.layout.simple_list_item_1 4.Spinner结果显示款布局:android.R.layout.simple_spinner_item 5.Spinner弹出对话框布局:android.R.layout.simple_spinner_dropdown_item 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1202978,如需转载请自行联系原作者

优秀的个人博客,低调大师

腾讯地图实现轨迹回放demo

前言 在地图接入使用中,很多开发者咨询我们腾讯位置服务是否支持轨迹回放功能,所以今天特意将我们JavaScript API GL的轨迹回放&小车移动示例放到我们本篇文章分享。 轨迹回放&小车移动 在JavaScript API GL中,使用MultiMarker(点标记)中的moveAlong()方法 ,可以方便的实现轨迹回放功能,而且您可以对样式进行各种想要的修改,比如修改小车图片、不显示路线或者改成您想要的颜色等。 代码 //初始化地图 var map = new TMap.Map("container", { zoom: 15, center: new TMap.LatLng(39.984104, 116.307503) }); //小车移动路线 var path = [ new TMap.LatLng(39.98481500648338, 116.30571126937866), new TMap.LatLng(39.982266575222155, 116.30596876144409), new TMap.LatLng(39.982348784165886, 116.3111400604248), new TMap.LatLng(39.978813710266024, 116.3111400604248), new TMap.LatLng(39.978813710266024, 116.31699800491333) ]; //创建mareker(小车) var marker = new TMap.MultiMarker({ map, styles: { //样式设置 'car-down': new TMap.MarkerStyle({ 'width': 40, //小车图片宽度(像素) 'height': 40, //高度 'anchor': { //图片中心的像素位置(小车会保持车头朝前,会以中心位置进行转向) x: 20,y: 20, }, 'faceTo': 'map', //取’map’让小车贴于地面,faceTo取值说明请见下文图示 'rotate': 180, //初始小车朝向(正北0度,顺时针一周为360度,180为正南) 'src': './img/car.png', //小车图片(图中小车车头向上,即正北0度) }) }, geometries: [{ //小车marker的位置信息 id: 'car', //因MultiMarker支持包含多个点标记,因此要给小车一个id styleId: 'car-down', //绑定样式 position: new TMap.LatLng(39.98481500648338, 116.30571126937866),//初始坐标位置 }] }); //调用moveAlong,实现小车移动 marker.moveAlong({ "car": { //设置让"car"沿"path"移动,速度70公里/小时 path, speed: 70 } }, { autoRotation:true //车头始终向前(沿路线自动旋转) } ) 在线示例:https://lbs.qq.com/webDemoCenter/glAPI/glMarker/markerMoveAlong 关于MultiMarker的faceTo说明: JavascriptAPI GL为可倾斜旋转的3D地图,这就带来了图片是贴在地面,还是贴向屏幕的问题: faceTo: “map” 贴在地面,轨迹回放场景,车是要贴地的(左图) faceTo:“screen” 贴在屏幕,小车场景就不合适了,它会始终“立着”(中图),"sreen"适合于标注位置使用(右图) 视角跟随小车移动(近期推出,敬请期待) 小车延路线运动的同时,控制视角跟随小车运动,可以达到类似模拟导航、第三人称游戏视角的感觉,非常炫酷。 作者:腾讯位置服务 链接:https://blog.csdn.net/weixin_45628602/article/details/109103634 来源:CSDN 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

优秀的个人博客,低调大师

随机森林算法demo python spark

关键参数 最重要的,常常需要调试以提高算法效果的有两个参数:numTrees,maxDepth。 numTrees(决策树的个数):增加决策树的个数会降低预测结果的方差,这样在测试时会有更高的accuracy。训练时间大致与numTrees呈线性增长关系。 maxDepth:是指森林中每一棵决策树最大可能depth,在决策树中提到了这个参数。更深的一棵树意味模型预测更有力,但同时训练时间更长,也更倾向于过拟合。但是值得注意的是,随机森林算法和单一决策树算法对这个参数的要求是不一样的。随机森林由于是多个的决策树预测结果的投票或平均而降低而预测结果的方差,因此相对于单一决策树而言,不容易出现过拟合的情况。所以随机森林可以选择比决策树模型中更大的maxDepth。 甚至有的文献说,随机森林的每棵决策树都最大可能地进行生长而不进行剪枝。但是不管怎样,还是建议对maxDepth参数进行一定的实验,看看是否可以提高预测的效果。 另外还有两个参数,subsamplingRate,featureSubsetStrategy一般不需要调试,但是这两个参数也可以重新设置以加快训练,但是值得注意的是可能会影响模型的预测效果(如果需要调试的仔细读下面英文吧)。 We include a few guidelines for using random forests by discussing the various parameters. We omit some decision tree parameters since those are covered in the decision tree guide. The first two parameters we mention are the most important, and tuning them can often improve performance: (1)numTrees: Number of trees in the forest. Increasing the number of trees will decrease the variance in predictions, improving the model’s test-time accuracy. Training time increases roughly linearly in the number of trees. (2)maxDepth: Maximum depth of each tree in the forest. Increasing the depth makes the model more expressive and powerful. However, deep trees take longer to train and are also more prone to overfitting. In general, it is acceptable to train deeper trees when using random forests than when using a single decision tree. One tree is more likely to overfit than a random forest (because of the variance reduction from averaging multiple trees in the forest). The next two parameters generally do not require tuning. However, they can be tuned to speed up training. (3)subsamplingRate: This parameter specifies the size of the dataset used for training each tree in the forest, as a fraction of the size of the original dataset. The default (1.0) is recommended, but decreasing this fraction can speed up training. (4)featureSubsetStrategy: Number of features to use as candidates for splitting at each tree node. The number is specified as a fraction or function of the total number of features. Decreasing this number will speed up training, but can sometimes impact performance if too low. We include a few guidelines for using random forests by discussing the various parameters. We omit some decision tree parameters since those are covered in the decision tree guide. """ Random Forest Classification Example. """ from __future__ import print_function from pyspark import SparkContext # $example on$ from pyspark.mllib.tree import RandomForest, RandomForestModel from pyspark.mllib.util import MLUtils # $example off$ if __name__ == "__main__": sc = SparkContext(appName="PythonRandomForestClassificationExample") # $example on$ # Load and parse the data file into an RDD of LabeledPoint. data = MLUtils.loadLibSVMFile(sc, 'data/mllib/sample_libsvm_data.txt') # Split the data into training and test sets (30% held out for testing) (trainingData, testData) = data.randomSplit([0.7, 0.3]) # Train a RandomForest model. # Empty categoricalFeaturesInfo indicates all features are continuous. # Note: Use larger numTrees in practice. # Setting featureSubsetStrategy="auto" lets the algorithm choose. model = RandomForest.trainClassifier(trainingData, numClasses=2, categoricalFeaturesInfo={}, numTrees=3, featureSubsetStrategy="auto", impurity='gini', maxDepth=4, maxBins=32) # Evaluate model on test instances and compute test error predictions = model.predict(testData.map(lambda x: x.features)) labelsAndPredictions = testData.map(lambda lp: lp.label).zip(predictions) testErr = labelsAndPredictions.filter(lambda (v, p): v != p).count() / float(testData.count()) print('Test Error = ' + str(testErr)) print('Learned classification forest model:') print(model.toDebugString()) # Save and load model model.save(sc, "target/tmp/myRandomForestClassificationModel") sameModel = RandomForestModel.load(sc, "target/tmp/myRandomForestClassificationModel") # $example off$ 模型样子: TreeEnsembleModel classifier with 3 trees Tree 0: If (feature 511 <= 0.0) If (feature 434 <= 0.0) Predict: 0.0 Else (feature 434 > 0.0) Predict: 1.0 Else (feature 511 > 0.0) Predict: 0.0 Tree 1: If (feature 490 <= 31.0) Predict: 0.0 Else (feature 490 > 31.0) Predict: 1.0 Tree 2: If (feature 302 <= 0.0) If (feature 461 <= 0.0) If (feature 208 <= 107.0) Predict: 1.0 Else (feature 208 > 107.0) Predict: 0.0 Else (feature 461 > 0.0) Predict: 1.0 Else (feature 302 > 0.0) Predict: 0.0 本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/7204096.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

python spark 随机森林入门demo

classpyspark.mllib.tree.RandomForest[source] Learning algorithm for a random forest model for classification or regression. New in version 1.2.0. supportedFeatureSubsetStrategies = ('auto', 'all', 'sqrt', 'log2', 'onethird') classmethodtrainClassifier( data, numClasses, categoricalFeaturesInfo, numTrees, featureSubsetStrategy='auto', impurity='gini', maxDepth=4, maxBins=32, seed=None) [source] Train a random forest model for binary or multiclass classification. Parameters: data– Training dataset: RDD of LabeledPoint. Labels should take values {0, 1, ..., numClasses-1}. numClasses– Number of classes for classification. categoricalFeaturesInfo– Map storing arity of categorical features. An entry (n -> k) indicates that feature n is categorical with k categories indexed from 0: {0, 1, ..., k-1}. numTrees– Number of trees in the random forest. featureSubsetStrategy– Number of features to consider for splits at each node. Supported values: “auto”, “all”, “sqrt”, “log2”, “onethird”. If “auto” is set, this parameter is set based on numTrees: if numTrees == 1, set to “all”; if numTrees > 1 (forest) set to “sqrt”. (default: “auto”) impurity– Criterion used for information gain calculation. Supported values: “gini” or “entropy”. (default: “gini”) maxDepth– Maximum depth of tree (e.g. depth 0 means 1 leaf node, depth 1 means 1 internal node + 2 leaf nodes). (default: 4) maxBins– Maximum number of bins used for splitting features. (default: 32) seed– Random seed for bootstrapping and choosing feature subsets. Set as None to generate seed based on system time. (default: None) Returns: RandomForestModel that can be used for prediction. Example usage: >>> from pyspark.mllib.regression import LabeledPoint >>> from pyspark.mllib.tree import RandomForest >>> >>> data = [ ... LabeledPoint(0.0, [0.0]), ... LabeledPoint(0.0, [1.0]), ... LabeledPoint(1.0, [2.0]), ... LabeledPoint(1.0, [3.0]) ... ] >>> model = RandomForest.trainClassifier(sc.parallelize(data), 2, {}, 3, seed=42) >>> model.numTrees() 3 >>> model.totalNumNodes() 7 >>> print(model) TreeEnsembleModel classifier with 3 trees >>> print(model.toDebugString()) TreeEnsembleModel classifier with 3 trees Tree 0: Predict: 1.0 Tree 1: If (feature 0 <= 1.0) Predict: 0.0 Else (feature 0 > 1.0) Predict: 1.0 Tree 2: If (feature 0 <= 1.0) Predict: 0.0 Else (feature 0 > 1.0) Predict: 1.0 >>> model.predict([2.0]) 1.0 >>> model.predict([0.0]) 0.0 >>> rdd = sc.parallelize([[3.0], [1.0]]) >>> model.predict(rdd).collect() [1.0, 0.0] New in version 1.2.0. 摘自:https://spark.apache.org/docs/latest/api/python/pyspark.mllib.html#pyspark.mllib.tree.DecisionTree 本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/7150484.html ,如需转载请自行联系原作者

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册