Python error while using the LDA in Sklearn - python

I'm trying to implement the LinearDiscriminantAnalysis from sklearn for that here is what I've done so far:
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import numpy as np
import pandas as pd
# Reading csv file
training_file = 'Training.csv'
testing_file = 'Test.csv'
dataframe_train = pd.read_csv(training_file)
dataframe_test = pd.read_csv(testing_file)
dataframe_train['onehot_code'] = dataframe_train.apply(lambda x : onehot_processing(int(float(x['Onehot'])), numberOFclasses), axis=1)
dataframe_test['onehot_code'] = dataframe_test.apply(lambda x:onehot_processing(int(float(x['Onehot'])),numberOFclasses),axis=1)
stdsc = preprocessing.StandardScaler()
np_scaled_train = stdsc.fit_transform(dataframe_train.iloc[:,:-3])
np_scaled_test = stdsc.transform(dataframe_test.iloc[:,:-3])
lda = LinearDiscriminantAnalysis(n_components=2)
Training_Frame = lda.fit_transform(np_scaled_train,dataframe_train.iloc[:,-1]) # the script crashes here
Testing_Frame = lda.transform(np_scaled_test)
The error message that I get is:
ValueError: You appear to be using a legacy multi-label data representation. Sequence of sequences are no longer supported; use a binary array or sparse matrix instead.
The shapes of the dataframe are correct, So I don't get what I'm missing or what should I convert so that the function accepts the parameter, or is the cause something else ?
I'll be grateful for any hint!
Update
Here's howdataframe_train.iloc[:,-1]looks like :
0 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
1 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
3 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
4 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
5 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
6 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
7 [1, 0, 0, 0, 0, 0, 0,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346 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2347 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2348 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2349 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2350 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2351 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2352 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2353 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2354 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2355 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2356 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2357 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
Name: onehot_code, dtype: object
each row is a vector of 20 elements .
**2nd_UPDATE"
Running the following : Training_Frame = lda.fit_transform(np_scaled_train,np.asarray(dataframe_train.iloc[:,-1]))
delivers this error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-a8adf693ad9e> in <module>()
----> 1 Training_Frame = lda.fit_transform(np_scaled_train,np.asarray(dataframe_train.iloc[:,-1]))
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\sklearn\base.py in fit_transform(self, X, y, **fit_params)
495 else:
496 # fit method of arity 2 (supervised transformation)
--> 497 return self.fit(X, y, **fit_params).transform(X)
498
499
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\sklearn\discriminant_analysis.py in fit(self, X, y, store_covariance, tol)
441 self.tol = tol
442 X, y = check_X_y(X, y, ensure_min_samples=2, estimator=self)
--> 443 self.classes_ = unique_labels(y)
444
445 if self.priors is None: # estimate priors from sample
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\sklearn\utils\multiclass.py in unique_labels(*ys)
77 # Check that we don't mix label format
78
---> 79 ys_types = set(type_of_target(x) for x in ys)
80 if ys_types == set(["binary", "multiclass"]):
81 ys_types = set(["multiclass"])
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages
\sklearn\utils\multiclass.py in <genexpr>(.0)
77 # Check that we don't mix label format
78
---> 79 ys_types = set(type_of_target(x) for x in ys)
80 if ys_types == set(["binary", "multiclass"]):
81 ys_types = set(["multiclass"])
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\sklearn\utils\multiclass.py in type_of_target(y)
248 if (not hasattr(y[0], '__array__') and isinstance(y[0], Sequence)
249 and not isinstance(y[0], string_types)):
--> 250 raise ValueError('You appear to be using a legacy multi-label data'
251 ' representation. Sequence of sequences are no'
252 ' longer supported; use a binary array or sparse'
ValueError: You appear to be using a legacy multi-label data representation. Sequence of sequences are no longer supported; use a binary array or sparse matrix instead.

This is what works for me, when tried duplicating your example.
y_train = dataframe_train.iloc[:,-1]
y_test = dataframe_test.iloc[:,-1]
from sklearn.preprocessing import MultiLabelBinarizer
mlb = MultiLabelBinarizer()
y_train = mlb.fit_transform(y_train)
y_test = mlb.transform(y_test)
lda = LinearDiscriminantAnalysis(n_components=2)
Training_Frame = lda.fit_transform(np_scaled_train, y_train)
Testing_Frame = lda.transform(np_scaled_test)
The error is most probably due to how pandas handle the lists in a column, and how numpy interprets them. The scikit-learn checks if supplied y is a numpy array of supported types (dtypes) (int, float, string, etc), but in your case df.iloc[:, -1] returns a pandas.Series which when directly converted to numpy, results in dtype = object. And hence the error.
One more workaround is (without using any of the code above):
Training_Frame = lda.fit_transform(np_scaled_train,
np.array([np.array(r) for r in dataframe_train‌​.iloc[:,-1]]))
Hope it works for you.

Related

Building SVM throws error because my training array has an extra dimension [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am creating a support vector machine. The model below reads the arrays that begin with "log" as vectors in the SVM graph. Arrays log15-log21 are to be classified "c" while the lines log22-log36 are to be classified "d". The goal is to give the svm another vector in the format of the "log" lines and for the svm to label it "c" or "d".
from sklearn import svm
log15 = [0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
log16 = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
log17 = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
log18 = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0]
log19 = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0]
log20 = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
log21 = [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
log22 = [0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
log23 = [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0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, 0, 0, 0, 0]
log35 = [0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
log36 = [0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
cLines = [log15, log16, log17, log18, log19, log20, log21]
dLines = [log22, log23, log24, log25, log26, log27, log28, log29, log30, log31, log32, log33, log34, log35, log36]
lines = [log15, log16, log17, log18, log19, log20, log21, log22, log23, log24, log25, log26, log27, log28, log29, log30, log31, log32, log33, log34, log35, log36]
X = [lines]
y = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] #0 for c, 1 for d
clf = svm.SVC()
clf.fit(X, y)
print(clf.predict([[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]]))
When I run the code above I get this error:
Traceback (most recent call last):
File "C:/Users/craig/Code/Python Programs/TensorFlowLabs/svm.py", line 34, in <module>
clf.fit(X, y)
File "c:\Users\craig\Programming Languages\Lib\site-packages\sklearn\svm\_base.py", line 196, in fit
accept_large_sparse=False,
File "c:\Users\craig\Programming Languages\Lib\site-packages\sklearn\base.py", line 576, in _validate_data
X, y = check_X_y(X, y, **check_params)
File "c:\Users\craig\Programming Languages\Lib\site-packages\sklearn\utils\validation.py", line 968, in check_X_y
estimator=estimator,
File "c:\Users\craig\Programming Languages\Lib\site-packages\sklearn\utils\validation.py", line 788, in check_array
% (array.ndim, estimator_name)
builtins.ValueError: Found array with dim 3. Estimator expected <= 2.
An online guide I see does not have the vectors separated by commas, but the individual characters in the vector arrays have significant meaning, so I don't want the 1's and 0's to be "jumbled up", if that makes sense.
You are defining X as an array (you are using brackets). That's why you are obtaining an error. Change the way you define X and it should work:
X = lines

Remove array([]) brackets to create clean array for matrix equations

I want to remove array brackets from my vectors so I can turn them into matrices for equations, what is the best way to do this? I want the vector to be [0,0] instead of array([0,0]) so conjoined into matrices are [[0,0],[0,1]] instead of [array([0,0]) , array([0,1])]
my code:
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
#create all actual subject matters
subjectmatters = ["basic", "python", "programming", "engineering", "mathematics", "logic", "hard", "html", "computers",
"design", "easy", "americanhistory", "history", "civilizations", "languagearts", "algebra",
"basicmath", "calculus", "nueralnets"]
#vectorize the subjects
vectorizer = CountVectorizer()
subjectmatters_vectorized = vectorizer.fit_transform(subjectmatters)
subjectmatters_vectorized_to_array = subjectmatters_vectorized.toarray()
subjectmatters_vectorized_to_array_shape = np.shape(subjectmatters_vectorized.toarray())
subjectvectordict = dict(zip(subjectmatters, subjectmatters_vectorized_to_array))
print(subjectvectordict)
This prints the below, looking to have array[()] removed:
{
"basic": array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
"python": array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
"programming": array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]),
"engineering": array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
"mathematics": array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]),
"logic": array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]),
"hard": array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]),
"html": array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]),
"computers": array([0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
"design": array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
"easy": array([0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
"americanhistory": array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
"history": array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]),
"civilizations": array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
"languagearts": array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]),
"algebra": array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
"basicmath": array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
"calculus": array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
"nueralnets": array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]),
}
Please see if this is what you want:
from sklearn.feature_extraction.text import CountVectorizer
#create all actual subject matters
subjectmatters = ["basic", "python", "programming", "engineering", "mathematics", "logic", "hard", "html", "computers",
"design", "easy", "americanhistory", "history", "civilizations", "languagearts", "algebra",
"basicmath", "calculus", "nueralnets"]
#vectorize the subjects
vectorizer = CountVectorizer()
subjectmatters_vectorized = vectorizer.fit_transform(subjectmatters)
subjectmatters_vectorized_to_array = subjectmatters_vectorized.toarray().tolist()
subjectvectordict = dict(zip(subjectmatters, subjectmatters_vectorized_to_array))
print(subjectvectordict)
{'basic': [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'python': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
'programming': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
'engineering': [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'mathematics': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
'logic': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
'hard': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
'html': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
'computers': [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'design': [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'easy': [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'americanhistory': [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'history': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
'civilizations': [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'languagearts': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
'algebra': [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'basicmath': [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'calculus': [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'nueralnets': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
}

How to pretty-print json with long array in the same line

I want to store collected data in json, they are all arrays of integers with couple of thousand elements each.
I want the file to have the elements in the same list to be in the same line, should look like
{
"foo": [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
],
"bar": [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
}
Right now, all I can find json in python can only be export with either
{'foo': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ... , 'bar': [0 ...]}
Or
{
"foo": [
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
But neither is desirable.
Using jsbeautifier:
import json
import jsbeautifier
x = {'foo': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0]], 'bar': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}
options = jsbeautifier.default_options()
options.indent_size = 2
print(jsbeautifier.beautify(json.dumps(x), options))
OUTPUT:
{
"foo": [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
],
"bar": [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
}

TypeError: an integer is required in sklearn Lasso model [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I seriously don't know what went wrong in my code. My data X and y look like this.
from sklearn.linear_model import Lasso
X = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
y = [11775, 3132, 8173, 5911]
lambda_val = 1.0023052380778996
lr = Lasso(alpha = lambda_val, fit_intercept=True, normalize=False, precompute=False,copy_X=True, max_iter=None, tol=0.0001, warm_start=False, positive=False,random_state=0, selection='cyclic')
lr.fit(X, y)
Getting an error message
TypeError: an integer is required
For the Lasso model, you need to specify the input argument max_iter. This needs to be an integer.
The default values is max_iter=1000 based on the documentation.
Just use this:
lambda_val = 1.0023052380778996
max_iter = 10 # or whatever integer you want
lr = Lasso(alpha = lambda_val, fit_intercept=True, normalize=False, precompute=False,copy_X=True, max_iter=max_iter, tol=0.0001, warm_start=False, positive=False,random_state=0, selection='cyclic')
lr.fit(X, y)

Pygame rectangle drawing issue

So me and my friend have been working on recreating Conway's game of life in python. It's been going well until we started drawing the cells with pygame. we just finished the grid easily but for some unknown reason our cells won't draw.
Please let us know whats wrong with the draw command.
Here's the code:
#Import goodies
import time
import pygame
pygame.init()
pygame.display.init()
#Create dimension variables
width = 32
height = 18
rectwidth = 1280 / width
rectheight = 720 / height
#Colors!
run = True
white = (255, 255, 255)
black = (0, 0, 0)
#Set up screen for pygame
size = (1280, 720)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Conway's Game of Life")
#Build frames
initial_frame = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]]
row = []
#Create neighbor variable
neighborcount = 0
#While run loop to prevent lag after closing window
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#Time for the big loop!
while True:
#Drawing grid
for b in range(0, width + 1):
pygame.draw.rect(screen, white, pygame.Rect(b * 40, 720, 1, -720))
for c in range(0, height + 1):
pygame.draw.rect(screen, white, pygame.Rect(1280, c * 40, -1280, 1))
pygame.display.flip()
#Start nested for loop for neighbor checking
for i in range(1, width):
for o in range(1, height):
#Check all 8 neighbors:
neighborcount = 0
#Down 1
if initial_frame[(o + 1)][i] == 1:
neighborcount += 1
#Up 1
if initial_frame[(o - 1)][i] == 1:
neighborcount += 1
#Right 1
if initial_frame[o][(i + 1)] == 1:
neighborcount += 1
#Left 1
if initial_frame[o][(i - 1)] == 1:
neighborcount += 1
#Down 1, Right 1
if initial_frame[(o + 1)][(i + 1)] == 1:
neighborcount += 1
#Down 1, Left 1
if initial_frame[(o + 1)][(i - 1)] == 1:
neighborcount += 1
#Up 1, Left 1
if initial_frame[(o - 1)][(i - 1)] == 1:
neighborcount += 1
#Up 1, Right 1
if initial_frame[(o - 1)][(i + 1)] == 1:
neighborcount += 1
#If dead cell has exactly 3 neighbors, set it to be born
next_frame[o][i] = initial_frame[o][i]
if initial_frame[o][i] == 0 and neighborcount == 3:
next_frame[o][i] = 1
#If living cell:
if initial_frame[o][i] == 1:
#draw it!
pygame.draw.rect(screen, white, pygame.Rect(o * 40, -i * 40, rectwidth, rectheight))
#If it does not have either 2 or 3 neighbors, set it to die
if neighborcount != 2 and neighborcount != 3:
next_frame[o][i] = 0
#reset neighbors
neighborcount = 0
#Project set values onto real board
garbage_arr = initial_frame
initial_frame = next_frame
next_frame = garbage_arr
#Finished!
pygame.quit()
In
pygame.draw.rect(screen, white, pygame.Rect(o * 40, -i * 40, rectwidth, rectheight))
the variabel i is negated.
This causes that the y coordinate of the rectangle is negative and the rectangle is out of bounds of the surface.
Change -i to i, to solve the issue:
pygame.draw.rect(screen, white, pygame.Rect(o * 40, i * 40, rectwidth, rectheight))

Categories

Resources