I've been having a problem with my ml.net console app. This is my first time using ml.net in Visual Studio so I was following this tutorial from microsoft.com, which is a sentiment analysis using binary classification.
I'm trying to process some test data in the form of tsv files to get a positive or negative sentiment analysis, but in debugging I'm receiving warnings there being 1 format error and 2 bad values.
I decided to ask all you great devs here on Stack to see if anyone can help me find a solution.
Here's an image of the debugging below:
Here's the link to my test data:
wiki-data
wiki-test-data
Finally, here's my code for those who what to reproduce the problem:
There's 2 c# files: SentimentData.cs & Program.cs.
1 - SentimentData.cs:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ML.Runtime.Api;
namespace MachineLearningTut
{
public class SentimentData
{
[Column(ordinal: "0")]
public string SentimentText;
[Column(ordinal: "1", name: "Label")]
public float Sentiment;
}
public class SentimentPrediction
{
[ColumnName("PredictedLabel")]
public bool Sentiment;
}
}
2 - Program.cs:
using System;
using Microsoft.ML.Models;
using Microsoft.ML.Runtime;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
using System.Threading.Tasks;
namespace MachineLearningTut
{
class Program
{
const string _dataPath = #".\Data\wikipedia-detox-250-line-data.tsv";
const string _testDataPath = #".\Data\wikipedia-detox-250-line-test.tsv";
const string _modelpath = #".\Data\Model.zip";
static async Task Main(string[] args)
{
var model = await TrainAsync();
Evaluate(model);
Predict(model);
}
public static async Task<PredictionModel<SentimentData, SentimentPrediction>> TrainAsync()
{
var pipeline = new LearningPipeline();
pipeline.Add(new TextLoader (_dataPath).CreateFrom<SentimentData>());
pipeline.Add(new TextFeaturizer("Features", "SentimentText"));
pipeline.Add(new FastForestBinaryClassifier() { NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 });
PredictionModel<SentimentData, SentimentPrediction> model = pipeline.Train<SentimentData, SentimentPrediction>();
await model.WriteAsync(path: _modelpath);
return model;
}
public static void Evaluate(PredictionModel<SentimentData, SentimentPrediction> model)
{
var testData = new TextLoader(_testDataPath).CreateFrom<SentimentData>();
var evaluator = new BinaryClassificationEvaluator();
BinaryClassificationMetrics metrics = evaluator.Evaluate(model, testData);
Console.WriteLine();
Console.WriteLine("PredictionModel quality metrics evaluation");
Console.WriteLine("-------------------------------------");
Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
Console.WriteLine($"Auc: {metrics.Auc:P2}");
Console.WriteLine($"F1Score: {metrics.F1Score:P2}");
}
public static void Predict(PredictionModel<SentimentData, SentimentPrediction> model)
{
IEnumerable<SentimentData> sentiments = new[]
{
new SentimentData
{
SentimentText = "Please refrain from adding nonsense to Wikipedia."
},
new SentimentData
{
SentimentText = "He is the best, and the article should say that."
}
};
IEnumerable<SentimentPrediction> predictions = model.Predict(sentiments);
Console.WriteLine();
Console.WriteLine("Sentiment Predictions");
Console.WriteLine("---------------------");
var sentimentsAndPredictions = sentiments.Zip(predictions, (sentiment, prediction) => (sentiment, prediction));
foreach (var item in sentimentsAndPredictions)
{
Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(item.prediction.Sentiment ? "Positive" : "Negative")}");
}
Console.WriteLine();
}
}
}
If anyone would like to see the code or more details on the solution, ask me on the chat and I'll send it. Thanks in advance!!! [Throws a Thumbs Up]
I think I got a fix for you. A couple of things to update:
First, I think you got your SentimentData properties switched to what the data has. Try changing it to
[Column(ordinal: "0", name: "Label")]
public float Sentiment;
[Column(ordinal: "1")]
public string SentimentText;
Second, use the useHeader parameter in the TextLoader.CreateFrom method. Don't forget to add that to the other one for the validation data, as well.
pipeline.Add(new TextLoader(_dataPath).CreateFrom<SentimentData>(useHeader: true));
With those two updates, I got the below output. Looks like a nice model with an AUC of 85%!
Another thing that helps with text type datasets is indicating that the text has quotes:
TextLoader("someFile.txt").CreateFrom<Input>(useHeader: true, allowQuotedStrings: true)
There is bad formated value on 252 and 253 rows. May me there fields wich contains the delimiter charachter.
If you post code or sample data we can be more precise.
Related
I am trying to get the distance between 2 points using the Longitude and Latitude in c# now i stumbled into some good codes and using them in my code i am getting this as an exception error :
System.Net.WebException: 'Request Not Authorized or Over QueryLimit'
My code is looking thus :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GoogleMaps.LocationServices;
using System.Device.Location;
namespace DistanceCalcConsole
{
class Program
{
static void Main(string[] args)
{
getDistancebetween2Points();
}
private static void getDistancebetween2Points()
{
var originAddress = "Lagos, Nigeria";
var locationService = new GoogleLocationService();
var point = locationService.GetLatLongFromAddress(originAddress);
var latitude1 = point.Latitude;
var longitude1 = point.Longitude;
var destinationAddress = "Ibadan, Nigeria";
var locationService2 = new GoogleLocationService();
var point2 = locationService2.GetLatLongFromAddress(destinationAddress);
var latitude2 = point.Latitude;
var longitude2 = point.Longitude;
var locA = new GeoCoordinate(latitude1, longitude1);
var locB = new GeoCoordinate(latitude2, longitude2);
double distance = locA.GetDistanceTo(locB);
double finalDistance = distance / 1000;
Console.Write(finalDistance +" Kilometers");
Console.Read();
}
}
}
Does it look like i am missing something? was made to understand that using Googlemaps.Locationservice is free so i should not be expecting this error...
There are few possible reasons for having unauthorized or over query limit error:
Billing not enabled
Keyless Implementation
Self-Imposed usage cap
If above solutions didn't work or are already done on your end, kindly file a support case via https://console.cloud.google.com/google/maps-apis/support in order to open personalized communication channel as this will need further investigation to address the issue properly.
Trying to make a simple app which will ask a few questions.
But for some reason, my AskQuestion function doesn't work.
I plan on adding an easily swap able database later which is why I'm trying to take a slightly more modular approach and as I am a beginner I am unsure what I did wrong. The only errors are in line 21 for the AskQuestion class.
Errors are:
CS1001 Identifier Expected
CS1514 { expected
CS1513 } expected
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz
{
class Program
{
// Question Base
class Question
{
public String question = "Empty Question";
public String correctanswer = "Empty Answer";
public String givenanswer = "Empty Answer";
public String response = "Empty Response.";
public bool cleared = false;
}
// Ask Base
class void AskQuestion(Question Q)
{
while (Q.cleared == false)
{
Console.WriteLine(Q.question);
Q.givenanswer = Console.ReadLine();
Q.givenanswer.ToLower();
if (Q.givenanswer == Q.correctanswer)
{
Console.WriteLine(Q.response);
Q.cleared = true;
}
else
{
Console.WriteLine("Wrong. Try again.");
}
}
}
// Main Function
void Main(string[] args)
{
string Name;
Console.WriteLine("Welcome challenger! You're going to have a good time.");
Console.WriteLine("Make sure you use proper grammar. Or you may be stuck for no reason.");
Console.WriteLine("What is your name challenger?");
Name = Console.ReadLine();
Console.WriteLine("Welcome {0} to the challenge. I wish you best of luck. You will need it.",Name);
Question Q1 = new Question();
Q1.question = "What is the color of the sun?";
Q1.correctanswer = "White";
Q1.response = "Correct. Despite the fact it appears Yellow on earth, if you observe the sun from space, you would see it's true color. White.";
AskQuestion(Q1);
Q1.cleared = true;
Console.WriteLine("Nice little warmup. But, lets get a bit serious.");
}
}
}
change this
class void AskQuestion(Question Q)
to
void AskQuestion(Question Q)
This should be a method. The keyword class tells the compiler you want to create a inner class inside the out class Program
Q.givenanswer.ToLower(); doesn't make Q.givenanswer lowercase - it returns a new lowercase string which you need to assign to a variable, or just `Q.givenanswer = Q.givenanswer.ToLower();
Okay so this is very basic but I've literally started learning how to read an XML document today and i usually find answers more comprehensive on here than on online guides. Essentially i'm coding a Pokemon game which uses an XML file to load all the stats (its one i swiped from someone else).The user will input a Pokemon and i then want to read the Base Stats of the Pokemon from the XML file, to give a template, this would be one of the Pokemon:
<Pokemon>
<Name>Bulbasaur</Name>
<BaseStats>
<Health>5</Health>
<Attack>5</Attack>
<Defense>5</Defense>
<SpecialAttack>7</SpecialAttack>
<SpecialDefense>7</SpecialDefense>
<Speed>5</Speed>
</BaseStats>
</Pokemon>
The code ive tried to use is:
XDocument pokemonDoc = XDocument.Load(#"File Path Here");
while(pokemonDoc.Descendants("Pokemon").Elements("Name").ToString() == cbSpecies.SelectedText)
{
var Stats = pokemonDoc.Descendants("Pokemon").Elements("BaseStats");
}
but this just returns pokemonDoc as null, any idea where im going wrong?
NOTE:
cbSpeciesSelect is where the user selects which species of pokemon they want.
The File Path definitely works as i've used it already in my program
The while loop never actually starts
Try xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var pokemon = doc.Descendants("Pokemon").Select(x => new {
name = (string)x.Element("Name"),
health = (int)x.Element("BaseStats").Element("Health"),
attack = (int)x.Element("BaseStats").Element("Attack"),
defense = (int)x.Element("BaseStats").Element("Defense"),
specialAttack = (int)x.Element("BaseStats").Element("SpecialAttack"),
specialDefense = (int)x.Element("BaseStats").Element("SpecialDefense"),
speed = (int)x.Element("BaseStats").Element("Speed"),
}).FirstOrDefault();
}
}
}
Can you try below code:
foreach(var e in pokemonDoc.Descendants("Pokemon").Elements("Name"))
{
if(e.Value==cbSpecies.SelectedText)
{
var Stats = pokemonDoc.Descendants("Pokemon").Elements("BaseStats");
}
}
This is using stackexchange.redis v1.1.603, .net 4.6, console application.
Here is my codes:
using System;
using System.Collections.Generic;
using StackExchange.Redis;
namespace RedisClusterTesting
{
class Program
{
static void Main(string[] args)
{
string ip = "192.168.1.20:30001,192.168.1.20:30002,192.168.1.20:30003,resolvedns=1";
var conf = ConfigurationOptions.Parse(ip);
conf.CommandMap = CommandMap.Create(new HashSet<string> {
"INFO", "CONFIG", "CLUSTER","PING", "ECHO", "CLIENT"
}, false);
using (ConnectionMultiplexer conn = ConnectionMultiplexer.Connect(conf))
{
var db = conn.GetDatabase();
Do(db);
}
Console.ReadKey();
}
private static void Do(IDatabase db)
{
/*here throws MOVED Exception:MOVED 12182 192.168.1.20:30003*/
db.StringSet("foo", "changed");
Console.WriteLine("foo now:" + db.StringGet("foo").ToString());
}
}
}
Always show the message "MOVED: 12586[192.168.1.20:30003]".
I search all the offcial document and on the Internet, can't find the right answer. It's OK while I use redis-cli.
How to fix this?Do I need process the exception in my code?If, how?
Seems like you may be running into this issue: https://github.com/StackExchange/StackExchange.Redis/issues/248. If you put a 1 second sleep between your Connect() call and your Do() call, I would guess that you will see the issue go away.
I am trying to connect mongolab to an application in Unity, but I have a problem in this line:
async static Task AsyncCrud(BsonDocument[] seedData)
The problem is here:
I wonder if someone can help me to solve this problem or suggest me another way to create the connection.
This is all the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
/*
* Copyright (c) 2015 ObjectLabs Corporation
* Distributed under the MIT license - http://opensource.org/licenses/MIT
*
* Written with CSharpDriver-2.0.0
* Documentation: http://api.mongodb.org/csharp/
* A C# class connecting to a MongoDB database given a MongoDB Connection URI.
*/
namespace MongoLab
{
class Simple
{
// Extra helper code
static BsonDocument[] CreateSeedData()
{
BsonDocument seventies = new BsonDocument {
{ "Decade" , "1970s" },
{ "Artist" , "Debby Boone" },
{ "Title" , "You Light Up My Life" },
{ "WeeksAtOne" , 10 }
};
BsonDocument eighties = new BsonDocument {
{ "Decade" , "1980s" },
{ "Artist" , "Olivia Newton-John" },
{ "Title" , "Physical" },
{ "WeeksAtOne" , 10 }
};
BsonDocument nineties = new BsonDocument {
{ "Decade" , "1990s" },
{ "Artist" , "Mariah Carey" },
{ "Title" , "One Sweet Day" },
{ "WeeksAtOne" , 16 }
};
BsonDocument[] SeedData = { seventies, eighties, nineties };
return SeedData;
}//end create
async static Task AsyncCrud(BsonDocument[] seedData)
{
// Create seed data
BsonDocument[] songData = seedData;
// Standard URI format: mongodb://[dbuser:dbpassword#]host:port/dbname
String uri = "mongodb://usuario:pass#server.mongolab.com:puerto/basededatos";
var client = new MongoClient(uri);
var db = client.GetDatabase("learnygames");//debe de ser la base de datos existente
Console.WriteLine(db);
Console.WriteLine(client.GetDatabase("db"));
/*
* First we'll add a few songs. Nothing is required to create the
* songs collection; it is created automatically when we insert.
*/
var songs = db.GetCollection<BsonDocument>("songs");
// Use InsertOneAsync for single BsonDocument insertion.
await songs.InsertManyAsync(songData);
Console.WriteLine("songs ********************************************");
/*
* Then we need to give Boyz II Men credit for their contribution to
* the hit "One Sweet Day".
*/
var updateFilter = Builders<BsonDocument>.Filter.Eq("Title", "One Sweet Day");
var update = Builders<BsonDocument>.Update.Set("Artist", "Mariah Carey ft. Boyz II Men");
await songs.UpdateOneAsync(updateFilter, update);
/*
* Finally we run a query which returns all the hits that spent 10
* or more weeks at number 1.
*/
var filter = Builders<BsonDocument>.Filter.Gte("WeeksAtOne", 10);
var sort = Builders<BsonDocument>.Sort.Ascending("Decade");
await songs.Find(filter).Sort(sort).ForEachAsync(song =>
Console.WriteLine("In the {0}, {1} by {2} topped the charts for {3} straight weeks",
song["Decade"], song["Title"], song["Artist"], song["WeeksAtOne"])
);
// Since this is an example, we'll clean up after ourselves.
// await db.DropCollectionAsync("songs");
}
static void Main()
{
BsonDocument[] seedData = CreateSeedData();
AsyncCrud(seedData).Wait();
}
}//end class conexxion
}//end name space
Unity doesnt support the async keyword since it uses .Net 3.5.
Take a look here.
You can do async operations with coroutines
Edit: I have learnt that Async isn't supported in Unity as another user has an highlighted as an answer.
Update
I was also trying to implement an async call in unity which brought my search to this question. Further research I discovered that Async was not supported but I also had found a github project by Real Serious Games that implement promises which were their answer to implementing async in unity.
https://github.com/Real-Serious-Games/C-Sharp-Promise
I first discovered their promise library from the unity knight's blog
http://blog.theknightsofunity.com/coroutines-unity-encapsulating-promises-part-1/
They have a 3/4 part article going over the basics and there is also a detail blog by Real Serious Games about the library on their blog.
I am now doing async by attaching the promise object to my unity coroutines to for async functionality.
Original Answer
The error you've pictured is basically saying put the keyword static before async.
static async Task AsyncCrud(BsonDocument[] seedData){}
you may need to Template the Task type with your actual return type
static async Task<T> AsyncCrud(BsonDocument[] seedData){}