sklearn-pandas

Cross-validation for Sklearn 0.20+?

梦想的初衷 提交于 2019-11-29 17:06:52
I am trying to do cross validation and I am running into an error that says: 'Found input variables with inconsistent numbers of samples: [18, 1]' I am using different columns in a pandas data frame (df) as the features, with the last column as the label. This is derived from the machine learning repository for UC Irvine. When importing the cross-validation package that I have used in the past, I am getting an error that it may have depreciated. I am going to be running a decision tree, SVM, and K-NN. My code is as such: feature = [df['age'], df['job'], df['marital'], df['education'], df[

Co-occurrence Matrix from list of words in Python

核能气质少年 提交于 2019-11-28 11:17:01
I have a list of names like: names = ['A', 'B', 'C', 'D'] and a list of documents, that in each documents some of these names are mentioned. document =[['A', 'B'], ['C', 'B', 'K'],['A', 'B', 'C', 'D', 'Z']] I would like to get an output as a matrix of co-occurrences like: A B C D A 0 2 1 1 B 2 0 2 1 C 1 2 0 1 D 1 1 1 0 There is a solution ( Creating co-occurrence matrix ) for this problem in R, but I couldn't do it in Python. I am thinking of doing it in Pandas, but yet no progress! Obviously this can be extended for your purposes, but it performs the general operation in mind: import math for

Cross-validation for Sklearn 0.20+?

試著忘記壹切 提交于 2019-11-28 09:58:22
问题 I am trying to do cross validation and I am running into an error that says: 'Found input variables with inconsistent numbers of samples: [18, 1]' I am using different columns in a pandas data frame (df) as the features, with the last column as the label. This is derived from the machine learning repository for UC Irvine. When importing the cross-validation package that I have used in the past, I am getting an error that it may have depreciated. I am going to be running a decision tree, SVM,

Converting a Pandas Dataframe column into one hot labels

风格不统一 提交于 2019-11-28 08:09:56
问题 I have a pandas dataframe similar to this: Col1 ABC 0 XYZ A 1 XYZ B 2 XYZ C By using the pandas get_dummies() function on column ABC, I can get this: Col1 A B C 0 XYZ 1 0 0 1 XYZ 0 1 0 2 XYZ 0 0 1 While I need something like this, where the ABC column has a list / array datatype: Col1 ABC 0 XYZ [1,0,0] 1 XYZ [0,1,0] 2 XYZ [0,0,1] I tried using the get_dummies function and then combining all the columns into the column which I wanted. I found lot of answers explaining how to combine multiple

sklearn SVM fit() “ValueError: setting an array element with a sequence”

一笑奈何 提交于 2019-11-28 04:47:01
问题 I am using sklearn to apply svm on my own set of images. The images are put in a data frame. I pass to the fit function a numpy array that has 2D lists, these 2D lists represents images and the second input I pass to the function is the list of targets (The targets are numbers). I always get this error "ValueError: setting an array element with a sequence". trainingImages = images.ix[images.partID <=9] trainingTargets = images.clustNo.ix[images.partID<=9] trainingImages.reset_index(inplace

Co-occurrence Matrix from list of words in Python

拥有回忆 提交于 2019-11-27 03:25:53
问题 I have a list of names like: names = ['A', 'B', 'C', 'D'] and a list of documents, that in each documents some of these names are mentioned. document =[['A', 'B'], ['C', 'B', 'K'],['A', 'B', 'C', 'D', 'Z']] I would like to get an output as a matrix of co-occurrences like: A B C D A 0 2 1 1 B 2 0 2 1 C 1 2 0 1 D 1 1 1 0 There is a solution (Creating co-occurrence matrix) for this problem in R, but I couldn't do it in Python. I am thinking of doing it in Pandas, but yet no progress! 回答1:

How to one-hot-encode from a pandas column containing a list?

只谈情不闲聊 提交于 2019-11-26 12:15:45
I would like to break down a pandas column consisting of a list of elements into as many columns as there are unique elements i.e. one-hot-encode them (with value 1 representing a given element existing in a row and 0 in the case of absence). For example, taking dataframe df Col1 Col2 Col3 C 33 [Apple, Orange, Banana] A 2.5 [Apple, Grape] B 42 [Banana] I would like to convert this to: df Col1 Col2 Apple Orange Banana Grape C 33 1 1 1 0 A 2.5 1 0 0 1 B 42 0 0 1 0 How can I use pandas/sklearn to achieve this? We can also use sklearn.preprocessing.MultiLabelBinarizer : from sklearn.preprocessing

How to one-hot-encode from a pandas column containing a list?

非 Y 不嫁゛ 提交于 2019-11-26 02:08:29
问题 I would like to break down a pandas column consisting of a list of elements into as many columns as there are unique elements i.e. one-hot-encode them (with value 1 representing a given element existing in a row and 0 in the case of absence). For example, taking dataframe df Col1 Col2 Col3 C 33 [Apple, Orange, Banana] A 2.5 [Apple, Grape] B 42 [Banana] I would like to convert this to: df Col1 Col2 Apple Orange Banana Grape C 33 1 1 1 0 A 2.5 1 0 0 1 B 42 0 0 1 0 How can I use pandas/sklearn