AttributeError: 'Pipeline' object has no attribute 'fit_resample' - python

Based on the documentation given on the following link pipeline and imbalanced
i have tried to implement code on some dataset, here is code :
import numpy as np
import pandas as pd
from collections import Counter
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import GaussianNB
data =pd.read_csv('aug_train.csv')
data.drop('id',axis=1,inplace=True)
print(data.info())
print(data.select_dtypes(include='object').columns.tolist())
data[data.select_dtypes(include='object').columns.tolist()]=data[data.select_dtypes(include='object').columns.tolist()].apply(LabelEncoder().fit_transform)
print(data.head())
#print(data['Response'].value_counts())
mymodel =GaussianNB()
y =data['Response'].values
print(Counter(y))
X =data.drop('Response',axis=1).values
#X,y =SMOTE().fit_resample(X,y)
#mymodel.fit(X,y)
#print(mymodel.score(X,y))
#print(Counter(y))
over = SMOTE(sampling_strategy=0.1)
under = RandomUnderSampler(sampling_strategy=0.5)
steps = [('o', over), ('u', under)]
pipeline = Pipeline(steps=steps)
# transform the dataset
X, y = pipeline.fit_sample(X, y)
the main problem in this code is with line :
X, y = pipeline.fit_sample(X, y)
error says that AttributeError: 'Pipeline' object has no attribute 'fit_resample' how can i fix this issue? thanks in advance

The tutorial employs imblearn.pipeline.Pipeline, while your code uses sklearn.pipeline.Pipeline (check import expressions). These appear to be different kinds of pipelines.

Related

KeyError: None of [Index([ ], dtype='object')] are in the [columns]

I am Learing Sklearn and have been working out with it. But as I started with KNN there seem to be a problem. Here is my code for this:
# import required packages
import numpy as np # later use
import pandas as pd
from sklearn import neighbors, metrics # later use
from sklearn.model_selection import train_test_split # later use
from sklearn.preprocessing import LabelEncoder
# start training
data = pd.read_csv('data/car.data')
X = data[[
'buying',
'maint',
'safety'
]]
y = data[['class']]
Le = LabelEncoder()
for i in range(len[X[0]]):
X[:, i] = Le.fit_transform(X[:, i])
After running the debugger, the problem seems to be at X = data[[
And I have no Idea how to solve it
I once saw the same error before and for that I just added .values to the end of the variable X

TypeErrorr: fit() missing 1 required positional argument: 'y'

please while i were doing me learning machine i saw that the code gives me these error, can you help me? thz
from asyncore import read
from pyexpat import model
from re import X
from pandas import read_csv
from sklearn.tree import DecisionTreeClassifier
giocatori = read_csv(r"C:\Users\alessandrini\Desktop\giocatori.csv")
X = giocatori.drop(columns=['videogame'])
y = giocatori['videogame']
modello = DecisionTreeClassifier
modello.fit(y.values, X.values)
previsione = modello.predict([[0,31]])
print(previsione)

AttributeError: 'SimpleImputer' object has no attribute 'mean'

I am trying to perform preprocessing on iris dataset but on the imputation step I get this error while using SimpleImputer to print the mean of every column.
here is the full code for reference. I am getting the error in last part.
import numpy as np
from sklearn.datasets import load_iris
from sklearn import preprocessing
iris = load_iris()
X = iris.data
iris_normalized = preprocessing.normalize(iris.datadata,norm='l2')
print(iris_normalized.mean(axis=0))
enc = preprocessing.OneHotEncoder()
iris_target_onehot = enc.fit_transform(iris.target.reshape(-1,1))
print(iris_target_onehot.toarray()[[0,50,100]])
X[:50,:] = np.nan
from sklearn.impute import SimpleImputer
iris_imputed = SimpleImputer(missing_values=np.nan,strategy='mean')
iris_imputed.fit_transform(X)
print(iris_imputed.mean(axis=0))
Sorry I am new to Machine Learning.
Just save the fit_transform to iris_imputed before calling its mean. It will work
iris_imputed = iris_imputed.fit_transform(iris.data)

MLeap broken with Skicit-learn when serialising: object has no attribute 'input_features'

I'm facing an issue with MLeap 0.16 and Python 3 when I try serialising a model.
Here is my code:
from mleap.sklearn.logistic import LogisticRegression
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
clf = LogisticRegression(random_state=0).fit(X, y)
clf.serialize_to_bundle("path", "irismodel")
error:
AttributeError: 'LogisticRegression' object has no attribute 'input_features'
Did anyone find a workaround?
I found the solution.
clf.mlinit(input_features="features", prediction_column="prediction")
was missing.
You can also use a pipeline to do that:
from mleap.sklearn.logistic import LogisticRegression
from sklearn.datasets import load_iris
from mleap.sklearn.pipeline import Pipeline
X, y = load_iris(return_X_y=True)
logistic = LogisticRegression(random_state=0)
logistic.mlinit(input_features="features", prediction_column="prediction")
pipeline = Pipeline([("log", logistic)])
clf = pipeline.fit(X, y)
clf.mlinit()
clf.serialize_to_bundle("/dbfs/endpath", "model.json")

Trying to run regression code. Getting error about 'linear_model'

I am trying to run this regression code.
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn
import sklearn.cross_validation
# Load the data
oecd_bli = pd.read_csv("C:/Users/Excel/Desktop/Briefcase/PDFs/ALL PYTHON & R CODE SAMPLES/Hands-On Machine_Learning_with_Scikit_Learn_and_Tensorflow/GDP Per Capita/oecd_bli.csv", thousands=',')
gdp_per_capita = pd.read_csv("C:/Users/Excel/Desktop/Briefcase/PDFs/ALL PYTHON & R CODE SAMPLES/Hands-On Machine_Learning_with_Scikit_Learn_and_Tensorflow/GDP Per Capita/gdp_per_capita.csv",thousands=',')
# view first 10 rows of data frame
oecd_bli[:10]
gdp_per_capita[:10]
country_stats = pd.merge(oecd_bli, gdp_per_capita, left_index=True, right_index=True)
country_stats[:10]
X = np.c_[country_stats["GDP"]]
Y = np.c_[country_stats["VALUE"]]
print(X)
print(Y)
# Visualize the data
country_stats.plot(kind='scatter', x="GDP", y='VALUE')
plt.show()
# Select a linear model
lin_reg_model = sklearn.linear_model.LinearRegression()
# Train the model
lin_reg_model.fit(X, Y)
# Make a prediction for Cyprus
X_new = [[22587]] # Cyprus' GDP per capita
print(lin_reg_model.predict(X_new))
I get this error.
AttributeError: module 'sklearn' has no attribute 'linear_model'
I'm not sure what's going on. I am trying to learn about this from an example that I saw in a book.
#import package, call the class
from sklearn.linear_model import LinearRegression
#build the model(create a regression object)
model = LinearRegression()
#fit the model
model.fit(x,y)
linear_model is a subpackage of sklearn. It wont work if you only imported via: import sklearn. Try import sklearn.linear_model instead.
Python does not automatically import all the subpackages. When I tried to explicitly import, linear_module, it works:
from sklearn import linear_model

Categories

Resources