Message Boards Message Boards

0
|
2766 Views
|
10 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Predict error: argument should be a rule or a list of rules

Dear all,

I am trying to use the "Predict", but I keep getting a message that I don't understand.

This is the message:

daa1=Flatten[Import["F:\\Eigen\\T06Sep2022\\ct1_v1a30.csv" ,"Data" ]]

Importing is still going well, but after that:

pdaa1=Predict[daa1,Method→"LinearRegression"]

Predict::bdfmt: Argument {1→5.0,2→8.0,3→20.0,4→26.0,5→35.0,6→53.0,7→58.0,8→71.0,9→88.0,10→106.0,11→110.0,12→114.0,13→117.0,14→136.0,15→139.0,16→145.0,17→147.0,18→171.0,19→177.0,20→182.0,21→183.0,22→197.0,23→204.0,24→208.0,25→209.0,26→224.0,27→228.0,28→250.0,29→264.0,30→282.0} should be a rule or a list of rules.

So it is the above message, starting with "Predict::bdfmt: Argument " and ending with* "should be a rule or a list of rules.* ".

Can anyone help me with instructions or reference to a document or book or other literature such as articles, Blogs, YouTube with examples.

Thanks in advance.

Roy

POSTED BY: Roy Stewart
10 Replies

Dear Eric Rimbey and Rohit Namjoshi and others,

With the help of you all, I have found a solution from your comments and tips. Thank you very much. Roy

POSTED BY: Roy Stewart

Dear Eric Rimbey and Rohit Namjoshi and others,

Herewith the data, namely the file: ct1_v1a30.csv

But when I read the comments from you, I thought there is error with the arrow in the file ct1v1a30.csv. So I changed that, which can be seen in a new file: ct1v1a30n.csv But despite the change, I can't manage to apply the 'Predict'. I hope you all can help me.

Thanks in advance Roy

POSTED BY: Roy Stewart
Posted 1 year ago

Okay, this is pretty easy. I was assuming that there was something subtle going on, but this is pretty straightforward. If you have control over the file, then just make it a regular csv file. For example,

1,5.0
2,8.0
3,20.0

Then your import will get a list of lists:

rawData = Import[pathToCsv]
(* {{1, 5.}, {2, 8.}, {3, 20.}} *)

You can turn each pair into a Rule,

Rule @@@ rawData

which you can either store in a variable or just use directly:

Predict[Rule @@@ rawData]

If you don't control the data file, then you'll need to turn the raw strings you imported into Rules, something like this:

Rule @@@ ToExpression[StringSplit[Flatten@rawData, "\[RightArrow]"]]

The problem here is that each line is like

1\[RightArrow]5.0

which is going to be interpreted as a string, since that right arrow has no numeric meaning. So, you're importing just a bunch of strings.

POSTED BY: Eric Rimbey

Look at

InputForm@daa1

Looks like a List of String. Need to be converted to a List of Rule. One way to do that

Rule @@@ ToExpression@daa1

Try passing that to Predict.

POSTED BY: Rohit Namjoshi

Attachments:
POSTED BY: Roy Stewart
Posted 1 year ago

I suspect that your Import resulted in textual \[RightArrow] instead of actual Rule. But we'd need to see the actual data. Can you post a sample of the file?

POSTED BY: Eric Rimbey

I have used the Predict-Wolfram link as an example: https://reference.wolfram.com/language/ref/Predict.html

And I give the print-screen to show what I did.

Thanks in advance. Roy

POSTED BY: Roy Stewart
Posted 1 year ago

I wanted to see the original data in F:\\Eigen\\T06Sep2022\\ct1_v1a30.csv so that I didn't need to try to reverse engineer what's going on here. Once you import it into Mathematica, you're getting RightArrow instead of Rule. I don't have an explanation off of the top of my head, but if I saw the original data I might be able to figure it out. To see what I'm saying, evaluate this:

FullForm[daa1]

You'll see something like

List[RightArrow[1,5.`],RightArrow[2,8.`], ... ,RightArrow[30,282.`]]

What you want is something like this:

List[Rule[1,5.`],Rule[2,8.`],Rule[3,20.`], ... ,Rule[29,264.`],Rule[30,282.`]]
POSTED BY: Eric Rimbey
Posted 1 year ago

And by the way, you could fix this after-the-fact by replacing RightArrow with Rule (but it would be better to fix this with the import).

daa1Fixed = daa1 /. RightArrow -> Rule
POSTED BY: Eric Rimbey
Posted 1 year ago

The syntax for Predict is

{
  example_1 -> value_1,
  example_2 -> value_2,
  ...
}

It is not ovbious to say that from your error message but it seems you are passing a flat list of values. What is supposed to be the input and what the training output?

POSTED BY: Updating Name
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract