<jupyter_start><jupyter_text>Scikit-Learn Examples 202-05-08<jupyter_code>import numpy as np 
import pandas as pd

from yellowbrick.datasets import load_concrete


from sklearn.svm import SVR
from sklearn.linear_model import SGDRegressor
from sklearn.linear_model import Ridge, Lasso, ElasticNet
X, y = load_concrete()
X.head()
y.head()
X.shape
y.shape
model = SGDRegressor()
model.get_params()
model.set_params(fit_intercept=False)
model.fit(X, y)
for attr in dir(model):
    if attr.endswith("_") and not attr.startswith("_"):
        print(attr)
model.coef_
model.predict([[300, 130, 0, 400, 1, 500, 300, 200]])
model.predict(X.iloc[:10,])
model.score(X, y)
model = Ridge().fit(X, y)
model.score(X, y)
model.predict([[300, 130, 0, 400, 1, 500, 300, 200]])

y.min(), y.max()
model.get_params()<jupyter_output><empty_output><jupyter_text>UCI Machine Learning WorkshopDownloaded from [AI4I 2020 Predictive Maintenance Dataset](https://archive.ics.uci.edu/ml/datasets/AI4I+2020+Predictive+Maintenance+Dataset)<jupyter_code>df = pd.read_csv("../data/ai4i2020.csv", index_col='UDI')
df.head()
# Do we have any missing data?
df.isnull().sum()
def extract_target(df):
    cols = ["TWF", "HDF", "PWF", "OSF", "RNF"]
    for row in df[cols].values:
        if np.sum(row) == 1:
            yield cols[np.where(row==1)[0][0]]
        else:
            yield "NONE"
       
feature_cols = [
    "Type", 
    "Air temperature [K]", 
    "Process temperature [K]", 
    "Rotational speed [rpm]",
    "Torque [Nm]",
    "Tool wear [min]",
]
    
X = df[feature_cols]
y = np.array(list(extract_target(df)))
X.shape
y.shape
from yellowbrick.target import class_balance

class_balance(y)
from sklearn.model_selection import train_test_split as tts

X_train, X_test, y_train, y_test = tts(X, y, test_size=.2)
X_train.shape, y_train.shape
X_test.shape, y_test.shape
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer, make_column_selector
from sklearn.preprocessing import RobustScaler, OneHotEncoder

from sklearn.ensemble import RandomForestClassifier
X.dtypes
categorical = make_column_selector(dtype_include=np.object)
numeric = make_column_selector(dtype_exclude=np.object)

model = Pipeline([
    ("columns", ColumnTransformer([
        ('onehot', OneHotEncoder(), categorical),
        ('scalar', RobustScaler(), numeric),
    ], remainder='drop')),
    ('clf', RandomForestClassifier()),
])
model.fit(X_train, y_train)
model.score(X_test, y_test)
from sklearn.model_selection import StratifiedKFold, cross_val_score
scores = cross_val_score(model, X, y, cv=StratifiedKFold(12))
scores
scores.mean()
scores.var()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13846153846153847, 'fraction_numerical': 0.026641651031894933, 'mean_word_length': 5.0590909090909095, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '379752', 'license': 'MIT', 'n_tokens_mistral': 1034, 'n_tokens_neox': 958, 'n_words': 215, 'path': 'demos/20210508.ipynb', 'repo_name': 'georgetown-analytics/machine-learning', 'size': '2665'}