Advertisement
KillianMills

DataMiningOld.java

Nov 30th, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.69 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.*;
  5.  
  6. /**
  7.  * Created by admin on 11/30/2015.
  8.  */
  9. /*
  10. #RB = Running Back
  11. #OL = Outside Linebacker
  12. #WR = Wide Receiver
  13. #TE = Tight End
  14. #FB = Full Back **************** merged with RB
  15. #DB = Defensive Back
  16. #QB = Quarter Back
  17. #LB = Linebacker
  18. #DL = Defensive Lineman
  19. */
  20.  
  21. public class DataMining {
  22.  
  23.     //This will hold all of the data, each index will hold a row
  24.     List<String[]> masterList = new ArrayList<>();
  25.  
  26.     //This holds the List names and the list
  27.     HashMap< String, List> printingMap = new HashMap<>();
  28.  
  29.     //This holds the List names and the list
  30.     HashMap< String, Double> meanRoles = new HashMap<>();
  31.  
  32.     //Lists of ages per section
  33.     List<Integer> allAges = new ArrayList<>();
  34.     List<Integer> fullBackAges = new ArrayList<>();
  35.     List<Integer> outsideLineBackerAges = new ArrayList<>();
  36.     List<Integer> wideReceiverAges = new ArrayList<>();
  37.     List<Integer> tightEndAges = new ArrayList<>();
  38.     List<Integer> defensiveBackAges = new ArrayList<>();
  39.     List<Integer> quarterBackAges = new ArrayList<>();
  40.     List<Integer> lineBackerAges = new ArrayList<>();
  41.     List<Integer> defensiveLinemanAges = new ArrayList<>();
  42.     List<Integer> cleanedUpAges = new ArrayList<>();
  43.     List<Integer> modernAges = new ArrayList<>();
  44.    
  45.     //Variables for var and std dev
  46.     HashMap<String,Double> varianceResults = new HashMap<>();
  47.  
  48.     public static void main(String args[]){
  49.         DataMining dm = new DataMining();
  50.         dm.readFile();
  51.         dm.buildAgeList();
  52.         dm.calculateValues();
  53.  
  54.         dm.getVariance(dm.printingMap, dm.meanRoles);
  55.     }
  56.  
  57.     public void readFile(){
  58.         String filename= "src/DataSet_DEADNFLPLAYERS.txt";
  59.  
  60.         try {
  61.             //read in the .txt file
  62.             BufferedReader br = new BufferedReader(new FileReader(filename));
  63.             String line= null;
  64.             String[] lineArray = {};
  65.  
  66.             //need to change this, puts it all into index 0 at the moment
  67.             while ((line = br.readLine()) != null) {
  68.                 // process the line.
  69.                 lineArray = line.split(",");
  70.                 masterList.add(lineArray);
  71.             }
  72.  
  73.         }
  74.         catch(IOException e){
  75.  
  76.             e.printStackTrace();
  77.         }
  78.     }
  79.  
  80.     public void buildAgeList(){
  81.         int deathYear;
  82.         int birthYear;
  83.         int age; // deathYear - birthYear
  84.  
  85.         //fill lists by using .contains RB, OL, WR etc...
  86.         for(int i=1; i< masterList.size(); i++ ){ //starts at 1 to avoid headers
  87.  
  88.             String[] currentLine = masterList.get(i);
  89.  
  90.             deathYear = Integer.parseInt(currentLine[10].replace("\"", "")); //10th element
  91.             birthYear = Integer.parseInt(currentLine[masterList.get(i).length-1].replace("\"", "")); //last element
  92.             age = deathYear - birthYear; // determines the age at death
  93.             allAges.add(age);
  94.  
  95.             List<String> tempArray = Arrays.asList(currentLine);
  96.  
  97.             if(tempArray.contains("\"RB/FB\"")){
  98.                 fullBackAges.add(age);
  99.             }
  100.             else if(tempArray.contains("\"OL\"")){
  101.                 outsideLineBackerAges.add(age);
  102.             }
  103.             else if(tempArray.contains("\"WR\"")){
  104.                 wideReceiverAges.add(age);
  105.             }
  106.             else if(tempArray.contains("\"TE\"")){
  107.                 tightEndAges.add(age);
  108.             }
  109.             else if(tempArray.contains("\"DB\"")){
  110.                 defensiveBackAges.add(age);
  111.             }
  112.             else if(tempArray.contains("\"QB\"")){
  113.                 quarterBackAges.add(age);
  114.             }
  115.             else if(tempArray.contains("\"LB\"")){
  116.                 lineBackerAges.add(age);
  117.             }
  118.             else{ // DL
  119.                 defensiveLinemanAges.add(age);
  120.             }
  121.  
  122.             if(age < 80 && age > 50){
  123.                 cleanedUpAges.add(age);
  124.             }
  125.  
  126.             if(birthYear >= 1940){
  127.                 modernAges.add(age);
  128.             }
  129.         }
  130.  
  131.         printingMap.put("ALL AGES", allAges);
  132.         printingMap.put("FULL BACK AGES", fullBackAges);
  133.         printingMap.put("OUTSIDE LINE BACKER AGES", outsideLineBackerAges);
  134.         printingMap.put("WIDE RECEIVER AGES", wideReceiverAges);
  135.         printingMap.put("DEFENSIVE BACK AGES", defensiveBackAges);
  136.         printingMap.put("QUARTER BACK AGES", quarterBackAges);
  137.         printingMap.put("LINE BACKER AGES", lineBackerAges);
  138.         printingMap.put("DEFENSIVE LINEMAN AGES", defensiveLinemanAges);
  139.         printingMap.put("CLEANED UP AGES", cleanedUpAges);
  140.         printingMap.put("MODERN AGES", modernAges);
  141.     }
  142.  
  143.     public void calculateValues(){
  144.         for(Map.Entry<String, List> entry : printingMap.entrySet() ){
  145.             String key = entry.getKey();
  146.             List value = entry.getValue();
  147.  
  148.             Collections.sort(value);
  149.  
  150.             System.out.println("MEAN OF " + key);
  151.             double mean= mean(key, value);
  152.             System.out.println(mean);
  153.             System.out.println("MODE OF " + key);
  154.             System.out.println(mode(key, value));
  155.             System.out.println("MEDIAN OF " + key);
  156.             System.out.println(median(key, value));
  157.             System.out.println("MIDRANGE OF " + key);
  158.             System.out.println(midRange(key, value));
  159.             System.out.println("---------------------");
  160.  
  161.             //adds a mean for each role, key is name of list
  162.             meanRoles.put(key, mean);
  163.  
  164.         }
  165.     }
  166.  
  167.     public double mean(String key, List value){
  168.         int totalLifeSpan = 0;
  169.  
  170.         for(int i = 0; i < value.size(); i++) { // get first value
  171.             totalLifeSpan += (int) value.get(i);
  172.         }
  173.  
  174.         double averageAges = totalLifeSpan / (value.size()-1);
  175.         return averageAges;
  176.     }
  177.  
  178.     public double mode(String key, List value){
  179.  
  180.         int popularity= 0;
  181.         int mostPop= 0;
  182.         int mode= 0;
  183.         int currentValue;
  184.  
  185.         for(int i = 0; i < value.size(); i++) { // get first value
  186.             currentValue = (int)value.get(i);
  187.  
  188.             for(int j=0; j< value.size(); j++){ // compare to other values in the list
  189.                 if(currentValue == (int)value.get(j)){
  190.                     popularity++; // increment the popularity if same
  191.                 }
  192.  
  193.             }
  194.             if(popularity > mostPop) { // if the new value is bigger than the old
  195.                 mostPop = popularity;
  196.                 mode = (int)value.get(i);
  197.             }
  198.             popularity = 0;
  199.         }
  200.         return((double)mode);
  201.     }
  202.  
  203.     public int median(String key, List value){
  204.  
  205.         int lengthOfArray = value.size();
  206.         int middleIndex = 0;
  207.         middleIndex = lengthOfArray / 2;
  208.         return((int)value.get(middleIndex));
  209.     }
  210.  
  211.     public double midRange(String key, List value){
  212.  
  213.         double max = (int) value.get(0);
  214.         double min = (int) value.get(value.size()-1);
  215.         double midrange = (min + max) / 2 ;
  216.         return(midrange);
  217.     }
  218.  
  219.     public void getVariance(HashMap<String, List> ageList , HashMap<String, Double> meanList){
  220.  
  221.        
  222.  
  223.         for(Map.Entry<String, List> entry : ageList.entrySet() ) {
  224.  
  225.             int counter=0;
  226.             double totalSum=0;
  227.  
  228.             String key = entry.getKey(); // name of current list
  229.             List value = entry.getValue(); // current list
  230.  
  231.             double currentMean = meanList.get(key);
  232.  
  233.             for(Object currentAge:value){
  234.                 totalSum+= (int)currentAge-currentMean;
  235.                 counter++;
  236.             }
  237.             varianceResults.put(key,totalSum/counter);
  238.         }
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement