首页 文章 精选 留言 我的

精选列表

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

[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,如需转载请自行联系原作者

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

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

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

hadoop&spark安装(上)

硬件环境: hddcluster1 10.0.0.197 redhat7 hddcluster2 10.0.0.228 centos7 这台作为master hddcluster3 10.0.0.202 redhat7 hddcluster4 10.0.0.181 centos7 软件环境: 关闭所有防火墙firewall openssh-clients openssh-server java-1.8.0-openjdk java-1.8.0-openjdk-devel hadoop-2.7.3.tar.gz 流程: 选定一台机器作为 Master 在 Master 节点上配置 hadoop 用户、安装 SSH server、安装 Java 环境 在 Master 节点上安装 Hadoop,并完成配置 在其他 Slave 节点上配置 hadoop 用户、安装 SSH server、安装 Java 环境 将 Master 节点上的 /usr/local/hadoop 目录复制到其他 Slave 节点上 在 Master 节点上开启 Hadoop 1 2 3 4 5 6 7 8 #节点的名称与对应的IP关系 [hadoop@hddcluster2~]$ cat /etc/hosts 127.0.0.1localhostlocalhost.localdomainlocalhost4localhost4.localdomain4 ::1localhostlocalhost.localdomainlocalhost6localhost6.localdomain6 10.0.0.228hddcluster2 10.0.0.197hddcluster1 10.0.0.202hddcluster3 10.0.0.181hddcluster4 1 2 3 4 5 创建hadoop用户 su #上述提到的以root用户登录 useradd -mhadoop-s /bin/bash #创建新用户hadoop passwd hadoop #设置hadoop密码 visudo #rootALL=(ALL)ALL这行下面添加hadoopALL=(ALL)ALL 1 2 3 4 5 6 7 8 9 10 11 #登录hadoop用户,安装SSH、配置SSH无密码登陆 [hadoop@hddcluster2~]$rpm-qa| grep ssh [hadoop@hddcluster2~]$ sudo yum install openssh-clients [hadoop@hddcluster2~]$ sudo yum install openssh-server [hadoop@hddcluster2~]$ cd ~/. ssh / #若没有该目录,请先执行一次sshlocalhost [hadoop@hddcluster2~]$ ssh -keygen-trsa #会有提示,都按回车就可以 [hadoop@hddcluster2~]$ ssh -copy- id -i~/. ssh /id_rsa .publocalhost #加入授权 [hadoop@hddcluster2~]$ chmod 600. /authorized_keys #修改文件权限 [hadoop@hddcluster2~]$ ssh -copy- id -i~/. ssh /id_rsa .pubhadoop@hddcluster1 [hadoop@hddcluster2~]$ ssh -copy- id -i~/. ssh /id_rsa .pubhadoop@hddcluster3 [hadoop@hddcluster2~]$ ssh -copy- id -i~/. ssh /id_rsa .pubhadoop@hddcluster4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #解压hadoop文件到/usr/local/hadoop [hadoop@hddcluster2~]$ sudo tar -zxfhadoop-2.7.3. tar .gz-C /usr/local/ [hadoop@hddcluster2~]$ sudo mv /usr/local/hadoop-2 .7.3 /usr/local/hadoop [hadoop@hddcluster2~]$ sudo chown -Rhadoop:hadoop /usr/local/hadoop cd /usr/local/hadoop . /bin/hadoop version #安装java环境 [hadoop@hddcluster2~]$ sudo yum install java-1.8.0-openjdkjava-1.8.0-openjdk-devel [hadoop@hddcluster2~]$rpm-qljava-1.8.0-openjdk-devel| grep '/bin/javac' /usr/lib/jvm/java-1 .8.0-openjdk-1.8.0.111-2.b15.el7_3.x86_64 /bin/javac [hadoop@hddcluster2~]$vim~/.bashrc export JAVA_HOME= /usr/lib/jvm/java-1 .8.0-openjdk-1.8.0.111-2.b15.el7_3.x86_64 export HADOOP_HOME= /usr/local/hadoop export HADOOP_INSTALL=$HADOOP_HOME export HADOOP_MAPRED_HOME=$HADOOP_HOME export HADOOP_COMMON_HOME=$HADOOP_HOME export HADOOP_HDFS_HOME=$HADOOP_HOME export YARN_HOME=$HADOOP_HOME export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME /lib/native export PATH=$PATH:$HADOOP_HOME /sbin :$HADOOP_HOME /bin export HADOOP_PREFIX=$HADOOP_HOME export HADOOP_OPTS= "-Djava.library.path=$HADOOP_PREFIX/lib:$HADOOP_PREFIX/lib/native" #测试java环境 source ~/.bashrc java-version $JAVA_HOME /bin/java -version #与直接执行java-version一样 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 #修改hadoop文件配置 [hadoop@hddcluster2hadoop]$ pwd /usr/local/hadoop/etc/hadoop [hadoop@hddcluster2hadoop]$ cat core-site.xml <?xmlversion= "1.0" encoding= "UTF-8" ?> <?xml-stylesheet type = "text/xsl" href= "configuration.xsl" ?> <!-- LicensedundertheApacheLicense,Version2.0(the "License" ); youmaynotusethis file except in compliancewiththeLicense. YoumayobtainacopyoftheLicenseat http: //www .apache.org /licenses/LICENSE-2 .0 Unlessrequiredbyapplicablelaworagreedto in writing,software distributedundertheLicenseisdistributedonan "ASIS" BASIS, WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. SeetheLicense for thespecificlanguagegoverningpermissionsand limitationsundertheLicense.SeeaccompanyingLICENSE file . --> <!--Putsite-specificpropertyoverrides in this file .--> <configuration> <property> <name>fs.defaultFS< /name > <value>hdfs: //hddcluster2 :9000< /value > < /property > <property> <name>hadoop.tmp. dir < /name > <value> file : /usr/local/hadoop/tmp < /value > <description>Abase for othertemporarydirectories.< /description > < /property > < /configuration > [hadoop@hddcluster2hadoop]$ cat hdfs-site.xml <?xmlversion= "1.0" encoding= "UTF-8" ?> <?xml-stylesheet type = "text/xsl" href= "configuration.xsl" ?> <!-- LicensedundertheApacheLicense,Version2.0(the "License" ); youmaynotusethis file except in compliancewiththeLicense. YoumayobtainacopyoftheLicenseat http: //www .apache.org /licenses/LICENSE-2 .0 Unlessrequiredbyapplicablelaworagreedto in writing,software distributedundertheLicenseisdistributedonan "ASIS" BASIS, WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. SeetheLicense for thespecificlanguagegoverningpermissionsand limitationsundertheLicense.SeeaccompanyingLICENSE file . --> <!--Putsite-specificpropertyoverrides in this file .--> <configuration> <property> <name>dfs.namenode.secondary.http-address< /name > <value>hddcluster2:50090< /value > < /property > <property> <name>dfs.replication< /name > <value>3< /value > < /property > <property> <name>dfs.namenode.name. dir < /name > <value> file : /usr/local/hadoop/tmp/dfs/name < /value > < /property > <property> <name>dfs.datanode.data. dir < /name > <value> file : /usr/local/hadoop/tmp/dfs/data < /value > < /property > < /configuration > [hadoop@hddcluster2hadoop]$ [hadoop@hddcluster2hadoop]$ cat mapred-site.xml <?xmlversion= "1.0" ?> <?xml-stylesheet type = "text/xsl" href= "configuration.xsl" ?> <!-- LicensedundertheApacheLicense,Version2.0(the "License" ); youmaynotusethis file except in compliancewiththeLicense. YoumayobtainacopyoftheLicenseat http: //www .apache.org /licenses/LICENSE-2 .0 Unlessrequiredbyapplicablelaworagreedto in writing,software distributedundertheLicenseisdistributedonan "ASIS" BASIS, WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. SeetheLicense for thespecificlanguagegoverningpermissionsand limitationsundertheLicense.SeeaccompanyingLICENSE file . --> <!--Putsite-specificpropertyoverrides in this file .--> <configuration> <property> <name>mapreduce.framework.name< /name > <value>yarn< /value > < /property > <property> <name>mapreduce.jobhistory.address< /name > <value>hddcluster2:10020< /value > < /property > <property> <name>mapreduce.jobhistory.webapp.address< /name > <value>hddcluster2:19888< /value > < /property > < /configuration > [hadoop@hddcluster2hadoop]$ [hadoop@hddcluster2hadoop]$ cat yarn-site.xml <?xmlversion= "1.0" ?> <!-- LicensedundertheApacheLicense,Version2.0(the "License" ); youmaynotusethis file except in compliancewiththeLicense. YoumayobtainacopyoftheLicenseat http: //www .apache.org /licenses/LICENSE-2 .0 Unlessrequiredbyapplicablelaworagreedto in writing,software distributedundertheLicenseisdistributedonan "ASIS" BASIS, WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. SeetheLicense for thespecificlanguagegoverningpermissionsand limitationsundertheLicense.SeeaccompanyingLICENSE file . --> <configuration> <!--SitespecificYARNconfigurationproperties--> <property> <name>yarn.resourcemanager. hostname < /name > <value>hddcluster2< /value > < /property > <property> <name>yarn.nodemanager.aux-services< /name > <value>mapreduce_shuffle< /value > < /property > < /configuration > [hadoop@hddcluster2hadoop]$ [hadoop@hddcluster2hadoop]$ cat slaves hddcluster1 hddcluster2 hddcluster3 hddcluster4 1 2 3 4 5 6 7 8 $ cd /usr/local $ sudo rm -r. /hadoop/tmp #删除Hadoop临时文件 $ sudo rm -r. /hadoop/logs/ * #删除日志文件 $ tar -zcf~ /hadoop .master. tar .gz. /hadoop #先压缩再复制 $ cd ~ $ scp . /hadoop .master. tar .gzhddcluster1: /home/hadoop $ scp . /hadoop .master. tar .gzhddcluster3: /home/hadoop $ scp . /hadoop .master. tar .gzhddcluster4: /home/hadoop 1 2 3 4 在salve节点上操作,安装软件环境并配置好.bashrc sudo tar -zxf~ /hadoop .master. tar .gz-C /usr/local sudo chown -Rhadoop /usr/local/hadoop 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [hadoop@hddcluster2~]$hdfsnamenode- format #首次运行需要执行初始化,之后不需要 接着可以启动hadoop了,启动需要在Master节点上进行启动命令: $start-dfs.sh $start-yarn.sh $mr-jobhistory-daemon.shstarthistoryserver 通过命令jps可以查看各个节点所启动的进程。正确的话, 在Master节点上可以看到NameNode、ResourceManager、SecondrryNameNode、JobHistoryServer进程, 另外还需要在Master节点上通过命令hdfsdfsadmin-report查看DataNode是否正常启动,如果Livedatanodes不为0,则说明集群启动成功。 [hadoop@hddcluster2~]$hdfsdfsadmin-report ConfiguredCapacity:2125104381952(1.93TB) PresentCapacity:1975826509824(1.80TB) DFSRemaining:1975824982016(1.80TB) DFSUsed:1527808(1.46MB) DFSUsed%:0.00% Underreplicatedblocks:0 Blockswithcorruptreplicas:0 Missingblocks:0 Missingblocks(withreplicationfactor1):0 ------------------------------------------------- Livedatanodes(4): 也可以通过Web页面看到查看DataNode和NameNode的状态:http: //hddcluster2 :50070/。如果不成功,可以通过启动日志排查原因。 1 在Slave节点操作可以看到DataNode和NodeManager进程 1 2 3 4 5 6 7 8 9 10 测试hadoop分布式实例 首先创建HDFS上的用户目录: hdfsdfs- mkdir -p /user/hadoop 将 /usr/local/hadoop/etc/hadoop 中的配置文件作为输入文件复制到分布式文件系统中: hdfsdfs- mkdir input hdfsdfs-put /usr/local/hadoop/etc/hadoop/ *.xmlinput 通过查看的DataNode的状态(占用大小有改变),输入文件确实复制到了DataNode中。 接着就可以运行MapReduce作业了: hadoopjar /usr/local/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples- *.jar grep inputoutput 'dfs[a-z.]+' 等待执行完毕后的输出结果: 1 2 3 4 5 6 7 8 hadoop启动命令: start-dfs.sh start-yarn.sh mr-jobhistory-daemon.shstarthistoryserver hadoop关闭命令: stop-dfs.sh stop-yarn.sh mr-jobhistory-daemon.shstophistoryserver PS:如果集群有一两台无法启动的话,先尝试一下删除hadoop临时文件 cd /usr/local sudo rm -r ./hadoop/tmp sudo rm -r ./hadoop/logs/* 然后执行 hdfs namenode -format 再启动 本文参考了一下网站并实验成功: http://www.powerxing.com/install-hadoop-cluster/ 本文转自 yanconggod 51CTO博客,原文链接: http://blog.51cto.com/yanconggod/1884998

资源下载

更多资源
Nacos

Nacos

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

Spring

Spring

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

Rocky Linux

Rocky Linux

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

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

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