TypeError: '=' not supported between instances of 'str' and 'int'
Change
vote = input('Enter the name of the player you wish to vote for')
to
vote = int(input('Enter the name of the player you wish to vote for'))
You are getting the input from the console as a string, so you must cast that input string to an int
object in order to do numerical operations.
TypeError: '' not supported between instances of 'str' and 'int' after converting string to float
There are two Gls
columns in your dataframe. I think you converted only one "Gls"
column to float, and when you do dfstandard['Gls'] = dfstandard['Gls'] / dfstandard['90s']
, the other "Gls" column is getting considered?...
Try stripping whitespace from the column names too
df = df.rename(columns=lambda x: x.strip())
df['90s'] = pd.to_numeric(df['90s'], errors='coerce')
df['Gls'] = pd.to_numeric(df['Gls'], errors='coerce')
Thus the error.
=' not supported between instances of 'str' and 'int' in functions and using lambda in apply
Your point
is in string
so, you're getting this error. But you can solve this by changing that string
into integer
. And in function you are sending wrongly, it should be points
and country
rather country
and points
.
def stars(points,country):
if country == 'Canada':
return 3
elif int(points) >= 95:
return 3
elif int(points) >= 85:
return 2
else:
return 1
star_ratings = reviews.apply(lambda x: stars(x['points'], x['country']), axis=1)
' is not supported between instances of 'str' and 'int'
As the traceback said, age
is a string because it has just been "inputted" by the user. Unlike C, there is no method to do something like scanf("%d", &age)
, so you need to manually cast age to an integer with age = int(age)
.
name = input("Enter your name:")
print("Hello, " +name)
age = input("Please enter your age:")
# do exception handling to make sure age is in integer format
age = int(age)
What features are causing TypeError: '' not supported between instances of 'str' and 'int'
I see that your label column is of type object
, meaning it is a string. But in your class weights, you use an integer in class_weight = {1: 3.5, 0: 1}
, so you to specify the classes correctly, or labelEncode.
With an example dataset, where my labels are "yes" or "no" :
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import OneHotEncoder, Normalizer, LabelEncoder
from sklearn.compose import ColumnTransformer
import pandas as pd
import numpy as np
df = pd.DataFrame({'f1':np.random.uniform(0,1,100),
'f2':np.random.choice(['a','b','c'],100),
'label':np.random.choice(['yes','no'],100)})
df.dtypes
f1 float64
f2 object
label object
If we set up the pipeline like you did, I get a similar error:
columns_for_encoding = ['f2']
columns_for_scaling = ['f1']
transformerVectoriser = ColumnTransformer(
transformers=[('Vector Cat', OneHotEncoder(handle_unknown = "ignore"), columns_for_encoding),
('Normalizer', Normalizer(), columns_for_scaling)],
remainder='passthrough')
pipeline = Pipeline([('transformer', transformerVectoriser),
('classifier', RandomForestClassifier(class_weight = {1: 3.5, 0: 1}))])
pipeline.fit(df[['f1','f2']],df['label'])
Let's define the weights properly and it works:
pipeline = Pipeline([('transformer', transformerVectoriser),
('classifier', RandomForestClassifier(class_weight = {'yes': 3.5, 'no': 1}))])
pipeline.fit(df[['f1','f2']],df['label'])
Pipeline(steps=[('transformer',
ColumnTransformer(remainder='passthrough',
transformers=[('Vector Cat',
OneHotEncoder(handle_unknown='ignore'),
['f2']),
('Normalizer', Normalizer(),
['f1'])])),
('classifier',
RandomForestClassifier(class_weight={'no': 1, 'yes': 3.5}))])
Related Topics
Python: Problem with Raw_Input Reading a Number
In Practice, What Are the Main Uses for the "Yield From" Syntax in Python 3.3
What's the How to Install Pip, Virtualenv, and Distribute for Python
What's the Difference Between Select_Related and Prefetch_Related in Django Orm
Python's Equivalent of && (Logical-And) in an If-Statement
Matplotlib Make Tick Labels Font Size Smaller
Can Pandas Automatically Read Dates from a CSV File
Get Md5 Hash of Big Files in Python
Matplotlib Plots: Removing Axis, Legends and White Spaces
Python's Most Efficient Way to Choose Longest String in List
How to Get a Value of Datetime.Today() in Python That Is "Timezone Aware"
Pandas Dataframe: Replace Nan Values with Average of Columns
Does Python Support Multithreading? Can It Speed Up Execution Time
Unnest (Explode) a Pandas Series
Python: Calling 'List' on a Map Object Twice
Pelican 3.3 Pelican-Quickstart Error "Valueerror: Unknown Locale: Utf-8"
How to Reliably Open a File in the Same Directory as the Currently Running Script