0

My file is:

outlook temperature Humidity Windy PlayTennis 

sunny hot high false N

sunny hot high true N

overcast hot high false P

rain mild high false P

rain cool normal false P

I found out unique elements from file as:

element:occurence

suny :2 

overcast:1 

rain:2

mild:1

cool:1

hot :4

normal:1

high:2

false:4

true:1 

n:2

p:3

Then I removed elements whose occurrence was less than 1.

as output came as:

suny : 2
rain: 2
hot :3
high:4
false:4
n:2 
p:3

Now I want output as (from the first output, it should loop with every other element to make a set of two frequent set)

element:occurence

sunny,hot:2
sunny,high:2
sunny,false:1
sunny,n:2
sunny,p:0
rain,hot:0
rain,high:1
rain,false:2
rain,n:0
rain,p:2
hot,high:2
hot,false:1
hot,n:2
hot,p:0
and so on..

Here is my code:

var occurences = File.ReadAllLines(file)
    .Skip(1)
    .SelectMany(l => l.Split(new []{' '},StringSplitOptions.RemoveEmptyEntries))
    .GroupBy(w => w)
    .ToDictionary(g => g.Key, g => g.Count());

foreach(var pair in occurences)
    label1.Text += String.Format("{0}: {1}\n", pair.Key, pair.Value);

I implemented this to find the 1st frequent set.

For 2nd one what should I do?

I also need to find the third set.

Nalaka526
  • 11,278
  • 21
  • 82
  • 116

1 Answers1

0

I would start by refactoring. 1) Make a class to hold the data items

public class WeatherForecast
{
    public string Outlook { get; set; }
    public string Temperature { get; set; }
    public string Humidity { get; set; }
    public bool Windy { get; set; }
    public string PlayTennis { get; set; }
}

Then create your objects;

var forecasts = new List<WeatherForecast>();
foreach(var line in File.ReadLines(file).Skip(1))
{
    var values = line.Split(' ');
    forecasts.Add(new WeatherForecast
                      {
                          Outlook = values[0],
                          Temperature = values[1],
                          Humidity = values[2],
                          Windy = Convert.ToBoolean(values[3]),
                          PlayTennis = values[4]
                      });
}

Then you might be able to figure out how you want to query your data with Linq.

For grouping help see LINQ Group By Multiple fields -Syntax help

//Group by 1 key
forecasts.GroupBy(f => new { f.Outlook }, (key, group) => new { Key1 = key.Outlook, Count = group.Count() });

//Group by 2 keys
forecasts.GroupBy(f => new { f.Outlook, f.Temperature }, (key, group) => new { Key1 = key.Outlook, Key2 = key.Temperature , Count = group.Count()});

//Group by 3 keys
forecasts.GroupBy(f => new { f.Outlook, f.Temperature, f.Humidity }, (key, group) => new { Key1 = key.Outlook, Key2 = key.Temperature, Key3 = key.Humidity, Count = group.Count()});

Result:

enter image description here

Community
  • 1
  • 1
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
  • Want output for two elements. as i mentioned in the output. Please suggest. – Bela Kharidia Feb 27 '13 at 03:04
  • yes..the idea is perfect.. but a very silly question...to represent this value in label..i tuk,, var forecast=forecasts.GroupBy(f => new { f.Outlook }, (key, group) => new { Key1 = key.Outlook, Count = group.Count() }); as Dump over here shows error(system.collection.generic.ienumerable does not contain definition for dump), so have removed dump. label1.text=forecasts.tostring(); -- doesnt work... should i take dictionary ?? – Bela Kharidia Feb 27 '13 at 06:30
  • @BelaKharidia Just get rid of the Dump. That is Linqpad specific. – Dustin Kingen Feb 27 '13 at 12:29
  • okay..for printig values, this is not working .. var forecast=forecasts.GroupBy(f => new { f.Outlook }, (key, group) => new { Key1 = key.Outlook, Count = group.Count() }); label1.text=forecasts.tostring(); – Bela Kharidia Feb 27 '13 at 13:54
  • You can't use ToString on the Enumerable. Try using a foreach loop over the enumerable and ToString each member using a StringBuilder. – Dustin Kingen Feb 27 '13 at 14:02
  • StringBuilder str1 = new StringBuilder(); foreach (StringBuilder values in forecast1) { label1.Text = values.ToString(); } somthing like this ? – Bela Kharidia Feb 27 '13 at 15:13
  • @BelaKharidia `var stringBuilder = new StringBuilder(); foreach(var item in forecast1) { stringBuilder.AppendFormat("Key1 {0} Count {1}", item.Key1, item.Count); }` That should put you in the right direction. – Dustin Kingen Feb 27 '13 at 15:29