"A column-vector y was passed when a 1d array was expected" error message - sklearn-pandas

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
clf = LinearDiscriminantAnalysis()
clf.fit(np.matrix(X_train), np.matrix(y_train))
but I get the error message. Specified above.
I checked the shape of y_train but it's (294,1).
tried the ravel() thing but it then it's (1,294) and if I transpose it then it looks back how it did before the ravel().
X_train.shape is (294,8).

first, don't use np.matrix, use np.array instead, its no longer recommended to use this class.
Try this:
clf.fit(X_train, np.ravel(y_train))

Related

PySpark: How do I fix 'function' object has no attribute 'rand' error?

I am trying to randomly select 100 rows from my PySpark Dataframe. For that I would like to use the code as described in this post:
training_data= data.orderBy(F.rand()).limit(100)
However I get the error:
AttributeError: 'function' object has no attribute 'rand'
I imported rand() the following way:
from pyspark.sql.functions import rand as F
I tried to import rand the same way as decribed in the post, but I get the error:
ModuleNotFoundError: No module named 'org'
I also tried to use the function just as such:
training_data= data.orderBy(rand()).limit(100)
But then I get the following name error:
NameError: name 'rand' is not defined
Does anyone know how to fix it ? I am new to PySpark and I think I am missing something obvious here. Note that I am working on Databricks.
Thank you
Ok, I actually managed to achieve what I wanted by doing the following:
training_data, test_data = data.randomSplit([0.7, 0.3], seed = 100)

want a calculation result instead of <bound method ...Python 2.8

I have tried finding this in the docs but don't quite get it. I am starting to work with Classes, When I try to get the result, I get instead of the actual 'width' I maybe I need to get but not sure.. It sounds too complicated just to get a result. Can someone advise?
I have:
class Rectangle(object):
def __init__(self, pt_ll, pt_ur):
self.ll = pt_ll
self.lr = Point(pt_ur.x, pt_ll.y)
self.ur = pt_ur
self.ul = Point(pt_ll.x, pt_ur.y)
def width(self):
"""Returns the width-float of the Rectangle."""
w = self.ur.x - self.ll.x
return w
Then I call it:
r = Rectangle(Point(0,0), Point(10,10))
print r.ur.x
print r.ll.x
print r.width
and get this output:
10.0
0.0
<bound method Rectangle.width of <__main__.Rectangle object at 0x000000000B5CBC50>>
print r.width
This returns the width method itself, this is what you see in the output.
You want to call that method so you need to instead do this:
print r.width()
This hints at an interesting thing about python, you can pass around functions just like you can with other variables.

Flask-WTF: How to allow zero upon DataRequired() validation

I have defined a form like this:
class RecordForm(Form):
rating = IntegerField('Rating')
If no value is inserted I get a default message like this:
Not a valid integer value
I would like to have a custom message instead, so I came up with this:
class RecordForm(Form):
rating = IntegerField('Rating',[validators.DataRequired("Helllo???")])
The custom message works now, but I get a side effect. 0 (zero) is no longer accepted as an integer value. What are my options here please?
Use InputRequired instead:
class RecordForm(Form):
rating = IntegerField('Rating',[validators.InputRequired("You got to enter some rating!")])
From the docs:
Note there is a distinction between this and DataRequired in that InputRequired looks that form-input data was provided, and DataRequired looks at the post-coercion data.
(Emphasis mine)

How to access attribute which are stored as variables

model Sample has attributes car,bike
x ="bike"
y = Sample.new
How can I do?
y.x ??
It gives me an error
Is there any way I can do it, I know that x is an attribute but I dont know which one.
So how can I get y.x?
You can use send to invoke a method on an object when the method is stored as a string:
x = "bike"
y = Sample.new
y.send(x) # Equivalent to y.bike
The following are equivalent, except that you may send protected methods:
object.method_name
object.send("method_name")
object.send(:method_name)
You have to use dynamic message passing. Try this:
y.send :bike
Or, in your case
y.send x

Accessing an array of vectors trouble

My code looks like this:
a = IO.readlines("input.txt").map { |line| Vector.[](line.split) }
Now I want to access a single component of the first vector within my a array. I'm writing the following to address a vector:
puts a[0]
The behavior is pretty much expected - I receive the following:
Vector[1.2357, 2.1742, -5.4834, -2.0735]
Now let's try to address a single component this way:
puts a[0][0]
and voila, I receive a list of all the vector components,like:
1.2357
2.1742
-5.4834
-2.0735
How come? Maybe the last attempt was wrong? How do I correctly address a scalar inside a vector within an array?
Due to your code I think the array construction should be:
a = IO.readlines("input.txt").map { |line| Vector[*line.split] }

Resources