Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Scanner;
  8.  
  9. import org.apache.hadoop.conf.Configuration;
  10. import org.apache.hadoop.fs.Path;
  11. import org.apache.hadoop.hbase.HBaseConfiguration;
  12. import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
  13. import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
  14. import org.apache.hadoop.hbase.mapreduce.TableMapper;
  15. import org.apache.hadoop.io.LongWritable;
  16. import org.apache.hadoop.mapreduce.Job;
  17. import org.apache.hadoop.mapreduce.Reducer;
  18. import org.apache.hadoop.mapreduce.Reducer.Context;
  19. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  20. import org.apache.hadoop.hbase.client.Result;
  21. import org.apache.hadoop.hbase.client.Scan;
  22. import org.apache.hadoop.io.DoubleWritable;
  23. import org.apache.hadoop.io.IntWritable;
  24.  
  25.  
  26.  
  27. public class Logistic {
  28. private static double[] theta = new double[1601];
  29. public static double a = 0.01;
  30.  
  31.  
  32. public static class mapper extends TableMapper<IntWritable, DoubleWritable>{
  33. private static double[] theta = new double[1601];
  34. private static double[] grad = new double[1601];
  35.  
  36. @Override
  37. public void setup(Context ct){
  38. Configuration cg = ct.getConfiguration();
  39. for(int i = 0; i < 1601; ++i){
  40. theta[i] = cg.getDouble("theta"+i, 0.0);
  41. }
  42. }
  43.  
  44. @Override
  45. public void map(ImmutableBytesWritable row, Result value, Context context) {
  46. String usuario = new String(row.get());
  47. double y = 0.0;
  48. char tipo = 'a';
  49.  
  50. Map<byte[], byte[]> caracteristicas = new HashMap<byte[], byte[]>();
  51. caracteristicas = value.getFamilyMap("a".getBytes());
  52. if(caracteristicas.isEmpty()){
  53. caracteristicas = value.getFamilyMap("b".getBytes());
  54. tipo = 'b';
  55. }
  56. if(tipo == 'b'){
  57. y = 1.0;
  58. }
  59. List<byte[]> cs = new ArrayList<>(caracteristicas.keySet());
  60. List<byte[]> vs = new ArrayList<>(caracteristicas.values());
  61. double[] x = new double[1601];
  62. x[0] = 1;
  63. for(int i = 0; i < vs.size();++i){
  64. int j = Integer.parseInt((new String(cs.get(i))).substring(1));
  65. x[j] = Double.parseDouble(new String(vs.get(i)));
  66. }
  67.  
  68. gradX(x, y);
  69. }
  70.  
  71. private static void gradX(double[] x, double y){
  72. double aux = 0;
  73. for(int i = 0; i < 1601;++i){
  74. aux += theta[i] * x[i];
  75. }
  76. double g = (1 / (1 + java.lang.Math.exp(-aux))) - y;
  77. for(int i = 0; i < 1601;++i){
  78. grad[i] += g * x[i];
  79. }
  80. }
  81.  
  82. @Override
  83. protected void cleanup (Context context) throws IOException, InterruptedException {
  84. for(int i = 0; i < 1601;++i){
  85. context.write(new IntWritable(i), new DoubleWritable(grad[i]));
  86. }
  87. }
  88. }
  89.  
  90. public static class reducer
  91. extends Reducer<IntWritable,DoubleWritable,IntWritable,DoubleWritable> {
  92.  
  93.  
  94. public void reduce(IntWritable key, Iterable<DoubleWritable> values, Context context)
  95. throws IOException, InterruptedException {
  96. double sum = 0;
  97. for(DoubleWritable v: values){
  98. sum += v.get();
  99. }
  100. context.write(key,new DoubleWritable(sum));
  101. }
  102. }
  103.  
  104. private static double g(double[] x){
  105. double aux = 0;
  106. for(int i = 0; i < 1601;++i){
  107. aux += theta[i] * x[i];
  108. }
  109. return 1 / (1 + java.lang.Math.exp(-aux));
  110. }
  111.  
  112. private static double[] grad(double[][] x, double[] y){
  113. double aux = 0;
  114. double[] grad = new double[1601];
  115. for(int j = 0; j < 1601;++j){
  116. grad[j] = 0;
  117. }
  118. int row = x.length;
  119. int col = x[0].length;
  120. for(int j = 0; j < 1601;++j){
  121. for(int i = 0; i < row;++i){
  122. aux += g(x[i]) * x[i][j];
  123. }
  124. grad[j] = aux;
  125. aux = 0;
  126. }
  127. return grad;
  128. }
  129.  
  130. private static void calcularModelo(){
  131. for(int i = 0; i < 1601;++i){
  132. theta[i] = 0;
  133. }
  134. double[] grad = new double[1601];
  135. for(int i = 0; i < 20; ++i){
  136.  
  137. Configuration conf = HBaseConfiguration.create();
  138. for(int j=0;j<1601;j++){
  139. //theta[j]= conf.getDouble("tetha"+j,0.0);
  140. conf.setDouble("tetha"+j,theta[j]);
  141. }
  142. try{
  143. Job job = Job.getInstance(conf,"Regresion logistica");
  144. Scan scan = new Scan();
  145. scan.setCaching(500);
  146. //scan.addColumn("t".getBytes(), "a".getBytes());
  147. //scan.addColumn("t".getBytes(), "b".getBytes());
  148. job.setJarByClass(Logistic.class);
  149.  
  150. job.setJarByClass(Logistic.class);
  151. TableMapReduceUtil.initTableMapperJob("p", scan,mapper.class, IntWritable.class, DoubleWritable.class, job);
  152. job.setReducerClass(reducer.class);
  153. job.setNumReduceTasks(6);
  154. job.setOutputKeyClass(IntWritable.class);
  155. job.setOutputValueClass(DoubleWritable.class);
  156.  
  157. FileOutputFormat.setOutputPath(job, new Path("out"));
  158.  
  159. if (!job.waitForCompletion(true))
  160. return;
  161. /*for(int j=0;j<theta.length;j++){
  162. theta[j]= conf.getDouble("tetha"+j,0.0);
  163. }*/
  164. }catch(Exception e){
  165. System.out.println(e.getMessage());
  166. }
  167. try{
  168. Scanner out = new Scanner(new File("out/part-r-00000"));
  169. while(out.hasNextLine()){
  170. int j = out.nextInt();
  171. double valor = out.nextDouble();
  172. theta[j] = theta[j] - 0.01*valor;
  173. out.nextLine();
  174. System.out.println(theta[j]);
  175. }
  176. }
  177. catch(Exception e){}
  178. }
  179. }
  180.  
  181. public static void main(String[] args) throws Exception {
  182. calcularModelo();
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement