acoustic-carpenter-78188
09/07/2023, 12:36 AMimage▾
#url = "<https://github.com/smadarab/flytelab/raw/main/census.csv>" # Make sure the url is the raw version of the file on GitHub
url="<https://github.com/smadarab/flytelab/raw/main/census> 2.csv"
download = requests.get(url).content
df = pd.read_csv(io.StringIO(download.decode('utf-8')),sep=',')
print("df is created",df.columns)
#df.dropna(inplace=True)
#df = df.reset_index()
print(df.columns)
return(df)
@task
def plot_dataset(df:pd.DataFrame) -> pyplot:
x = df['age'].value_counts().index
y = df['age'].value_counts().values
fig = pyplot.figure(figsize =(10, 7))
# Horizontal Bar Plot
pyplot.bar(x,y)
# Show Plot
#pyplot.show()
return(fig)
#@task
#def clean_dataset() -> pd.DataFrame:
pass
#@task
#def transform_cat() -> pd.DataFrame:
pass
#@task
#def transform_num() -> pd.DataFrame:
pass
@task
def train_model(train: pd.DataFrame) -> Tuple[AdaBoostClassifier,OneHotEncoder,MinMaxScaler]:
num_cols = ['age', 'education-num', 'capital-gain',
'capital-loss', 'hours-per-week']
cat_cols = ['workclass',
'marital-status', 'occupation',
'relationship', 'race',
'sex', 'native-country']
log_transform_cols = ['capital-loss', 'capital-gain']
def get_cat_cols(X):
return X[cat_cols]
def get_num_cols(X):
return X[num_cols]
def get_log_transform_cols(X):
return X[log_transform_cols]
def get_dummies(X):
print('\n \n',type(X))
return pd.get_dummies(pd.DataFrame(X))
def cat_imputer(X):
print(X.shape)
return(imputer_cat.fit_transform(X))
#return X.apply(lambda col: imputer_cat.fit_transform(col))
def one_hot_encode(X):
print("one hot encode")
ohe = OneHotEncoder(handle_unknown = 'ignore')
#print("DF:: ")
ohe.fit(pd.DataFrame(X))
global hi
hi=ohe
#dump(ohe, 'onehot.joblib')
return ohe.transform(pd.DataFrame(X)).toarray()
def min_max_scaling(X):
scaler = MinMaxScaler()
scaler.fit(X)
global scale
scale=scaler
return scaler.transform(pd.DataFrame(X)).tolist()
log_transform_pipeline = Pipeline([
('get_log_transform_cols', FunctionTransformer(get_log_transform_cols, validate=False)),
('imputer', SimpleImputer(strategy='mean')),
('log_transform', FunctionTransformer(np.log1p))
])
num_cols_pipeline = Pipeline([
('get_num_cols', FunctionTransformer(get_num_cols, validate=False)),
('imputer', SimpleImputer(strategy='mean')),
('min_max_scaler', FunctionTransformer(min_max_scaling, validate=False))
])
cat_cols_pipeline = Pipeline([
('get_cat_cols', FunctionTransformer(get_cat_cols, validate=False)),
('imputer', SimpleImputer(strategy="most_frequent")),
# ('get_dummies', FunctionTransformer(get_dummies, validate=False))
('one_hot_encode', FunctionTransformer(one_hot_encode, validate=False))
])
steps_ = FeatureUnion([
('log_transform', log_transform_pipeline),
('num_cols', num_cols_pipeline),
('cat_cols', cat_cols_pipeline)
])
full_pipeline = Pipeline([('steps_', steps_)])
y = train['income'].map({'<=50K': 0, '>50K': 1})
X = full_pipeline.fit_transform(train)
model = AdaBoostClassifier(n_estimators=300)
X_train, X_test, y_train, y_test = train_test_split(X, y,shuffle=False)
model = model.fit(X_train, y_train)
return model,hi,scale
#@task
#def test_model() -> pd.DataFrame:
pass
#@workflow
#def main1() -> pd.DataFrame:
return get_dataset()
@workflow
def main() -> pyplot:
return plot_dataset(df=get_dataset())
#@workflow
#def main() -> Tuple[AdaBoostClassifier,OneHotEncoder,MinMaxScaler]:
return train_model(train=get_dataset())
if name == "*main*":
print(f"trained model: {main()}")
Screenshots
No response
Are you sure this issue hasn't been raised already?
☑︎ Yes
Have you read the Code of Conduct?
☑︎ Yes
flyteorg/flyteacoustic-carpenter-78188
09/07/2023, 12:36 AM