首页 文章 精选 留言 我的

精选列表

搜索[python解释器],共10000篇文章
优秀的个人博客,低调大师

[Spark][Python]RDD flatMap 操作例子

RDD flatMap 操作例子: flatMap,对原RDD的每个元素(行)执行函数操作,然后把每行都“拍扁” [training@localhost ~]$ hdfs dfs -put cats.txt [training@localhost ~]$ hdfs dfa -cat cats.txt Error: Could not find or load main class dfa [training@localhost ~]$ hdfs dfs -cat cats.txt The cat on the mat The aardvark sat on the sofa mydata=sc.textFile("cats.txt") mydata.count() Out[14]: 2 mydata.take(2) Out[15]: [u'The cat on the mat', u'The aardvark sat on the sofa'] myflatdata=mydata.flatMap(lambda line: line.split(' ')) myflatdta.count() Out[19]: 11 myflatdata.take(2) Out[20]: [u'The', u'cat'] myflatdata.take(11) Out[21]: [u'The', u'cat', u'on', u'the', u'mat', u'The', u'aardvark', u'sat', u'on', u'the', u'sofa'] 本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/p/7593885.html,如需转载请自行联系原作者

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

[Spark][Python]Spark Join 小例子

[training@localhost ~]$ hdfs dfs -cat people.json {"name":"Alice","pcode":"94304"} {"name":"Brayden","age":30,"pcode":"94304"} {"name":"Carla","age":19,"pcoe":"10036"} {"name":"Diana","age":46} {"name":"Etienne","pcode":"94104"} [training@localhost ~]$ hdfs dfs -cat pcodes.json {"pcode":"10036","city":"New York","state":"NY"} {"pcode:"87501","city":"Santa Fe","state":"NM"} {"pcode":"94304","city":"Palo Alto","state":"CA"} {"pcode":"94104","city":"San Francisco","state":"CA"} sqlContext = HiveContext(sc) peopleDF = sqlContext.read.json("people.json") sqlContext = HiveContext(sc) pcodesDF = sqlContext.read.json("pcodes.json") mydf001=peopleDF.join(pcodesDF,"pcode") mydf001.limit(5).show() +-----+----+-------+----+---------------+-------------+-----+ |pcode| age| name|pcoe|_corrupt_record| city|state| +-----+----+-------+----+---------------+-------------+-----+ |94304|null| Alice|null| null| Palo Alto| CA| |94304| 30|Brayden|null| null| Palo Alto| CA| |94104|null|Etienne|null| null|San Francisco| CA| +-----+----+-------+----+---------------+-------------+-----+ 本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/p/7630003.html,如需转载请自行联系原作者

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

随机森林算法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,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

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

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册
修改密码
忘记密码