0
$\begingroup$
  • I want to make a data frame with most repeated word in sentences and make a classification via Logistic-Regression.

  • I tried to write the steps clearly in codes.

  • The column B is my target.

What I have: (Sample)

raw_data={"A":["This is yellow","That is green","These are orange","This is a pen","This is an Orange"],
          "B":["Yes","No","Yes","No","No"]   }
df=pd.DataFrame(raw_data)
df

    A                   B
0   This is yellow      Yes
1   That is green       No
2   These are orange    Yes
3   This is a pen       No
4   This is an Orange   No

What I did:

### 1-Import Libraries:
import numpy as np 
import pandas as pd

### 2- Create data set:
raw_data={"A":["This is yellow","That is green","These are orange","This is a pen","This is an Orange"],
          "B":["Yes","No","Yes","No","No"]   }
df=pd.DataFrame(raw_data)
df


          A              B
0   This is yellow      Yes
1   That is green       No
2   These are orange    Yes
3   This is a pen       No
4   This is an Orange   No

### 3- Count the word and charachters 
df['word_count'] = df['A'].agg(lambda x: len(x.split(" ")))
df['char_count'] = df['A'].agg(lambda x:len(x))
df
             A         B    word_count  char_count
0   This is yellow     Yes  3           14
1   That is green      No   3           13
2   These are orange   Yes  3           16
3   This is a pen      No   4           13
4   This is an Orange  No   4           17

### 4- Count the most repeated words in column "A"
df_word_count=pd.DataFrame(df.A.str.split('').explode().value_counts()).reset_index().rename({'index':"A,"A":"Count"},axis=1)
display(df_word_count)
list_word_count=list(df_word_count["A"])
len(list_word_count)

    A       Count
0   is      4
1   This    3
2   yellow  1
3   These   1
4   orange  1
5   green   1
6   That    1
7   are     1
8   a       1
9   pen     1
10  Orange  1
11  an      1

### 5- Make a ZERO-Matrix
allfeatures=np.zeros((df.shape[0],len(list_word_count)))
allfeatures.shape

### 6- Create a data frame
for i in range(len(list_word_count)):
  allfeatures[:,i]=df['A'].agg(lambda x:x.split().count(list_word_count[i]))
Complete_data=pd.concat([df,pd.DataFrame(allfeatures)],axis=1)
display(Complete_data)

           A            B   word_count  char_count  0   1   2   3   4   5   6   7   8   9   10  11
0   This is yellow      Yes 3           14          1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1   That is green       No  3           13          1.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0
2   These are orange    Yes 3           16          0.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0
3   This is a pen       No  4           13          1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0
4   This is an Orange   No  4           17          1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0


### 7- change columns name from list
#This creates a list of the words 
l = list(df_word_count["A"])

l.insert(0,"char_count")
l.insert(0,"word_count")
l.insert(0,"B")
l.insert(0,"A")    
# Finally, I rename all the columns with the names that I have in the list l
Complete_data.columns = l

### 8- Define X and Y
x=Complete_data.drop(["A","B"],axis=1) # Features
y=Complete_data["B"] # Target


### 9- Encoding of Target
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(y)


### 10- Train|Test split
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 0)

### 11- Import Sklearn needed packages
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import r2_score

from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict

### 12- Prediction and Regression with Cross-Validation
LogReg=LogisticRegression()
LogReg.fit(x_train,y_train)

cv_LogReg=cross_val_score(LogReg,x_train,y_train,cv=2)
cv_LogReg_pred=cross_val_predict(LogReg,x_train,y_train,cv=2)

print("Score: ",r2_score(y_train,cv_LogReg_pred))

Error:

The Algorithm can't find any classification (0,1), although I used the LabelEncoder


ValueError                                Traceback (most recent call last)
<ipython-input-127-2d7e54ebfd6c> in <module>
      4 #LogReg_pred=LogReg.predict(x_test)
      5 cv_LogReg=cross_val_score(LogReg,x_train,y_train,cv=2)
----> 6 cv_LogReg_pred=cross_val_predict(LogReg,x_train,y_train,cv=2)
      7 
      8 print("Score: ",r2_score(y_train,cv_LogReg_pred))

.
.
.

This solver needs samples of at least 2 classes in the data, but the data contains only one class: 0

I don't know what I did wrong 🤷‍♂️

$\endgroup$
0
0
$\begingroup$

Since you are doing cross validation on a sample, while choosing a sample it is dividing in such a way that the sample contains only one class hence you are getting that error. If you have more data , you should not get this error. I have performed simple Logistic regression on these 5 records and I am able to create a model so can you increase your data and check? I have added data like this:

2- Create data set:

raw_data={"A":["This is yellow","That is green","These are orange","This > is a pen","This is an Orange", "This yllow","That geen","These ornge","This a >pn","This an Ornge"], "B":["Yes","No","Yes","No","No", "Yes","No","Yes","No","No"] }

And one more thing, I changed the r2_score in the last line to accuracy score.

$\endgroup$
1
  • $\begingroup$ Thank you very much for your support. You have right, but I made this sample dataset only for presentation, else my data is 4 GiB. $\endgroup$ – JiJoik Feb 10 at 14:53
0
$\begingroup$

This seems to be caused by the fact that your y_train dataset (or one of the folds) only contains one class, in this case all examples are of class 0. See also this stackexchange answer. You could solve this by increasing the number of samples (lower chance of having all samples of the same class) or using a stratified data split/cross validation strategy to make sure that the number of samples with value 0 and 1 are roughly the same between all subsets.

$\endgroup$
1
  • $\begingroup$ Thank u for the answer. Actually my data set is 1000000 x 10101100010. that was only a sample of the same problem what i have $\endgroup$ – JiJoik Feb 9 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.