<jupyter_start><jupyter_text>Basic model with scikit-learn> First model with scikit-learn- toc: true - badges: false- comments: true- author: <NAME>- categories: [Panda, sklearn]  Imports<jupyter_code>import pandas as pd
myDataFrame = pd.read_csv("../../scikit-learn-mooc/datasets/adult-census.csv")<jupyter_output><empty_output><jupyter_text>First analysis<jupyter_code>print(f"The dataset contains {myDataFrame.shape[0]} samples and "
      f"{myDataFrame.shape[1]} columns")
myDataFrame.head()<jupyter_output><empty_output><jupyter_text>Which column is our target to predict?<jupyter_code>target_column = 'class'

target_y = myDataFrame["class"]
data_X = myDataFrame.drop(columns="class")
target_y.value_counts()
data_X.head()<jupyter_output><empty_output><jupyter_text>CrosstabUseful to detect columns containing the same information in two different forms (thus correlated). If this is the case, one of the columns is excluded. Here we excluded "education-num".<jupyter_code>pd.crosstab(index=data_X['education'],
            columns=data_X['education-num'])
data_X = data_X.drop(columns="education-num")<jupyter_output><empty_output><jupyter_text>Separation between numerical and categorical columns<jupyter_code>print(f"The dataset data_X contains {data_X.shape[0]} samples and "
      f"{data_X.shape[1]} columns")
data_X.dtypes<jupyter_output><empty_output><jupyter_text>We sort the variable names according to their type by hand<jupyter_code>numerical_columns = ["age", "capital-gain", "capital-loss", "hours-per-week"]
categorical_columns = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 
                       'race', 'sex', 'native-country']<jupyter_output><empty_output><jupyter_text>select columns based on their data type<jupyter_code>from sklearn.compose import make_column_selector as selector
categorical_columns = selector(dtype_include="object")(data_X)
categorical_columns
numerical_columns = selector(dtype_include="int64")(data_X)
numerical_columns
all_columns = numerical_columns + categorical_columns

data_X = data_X[all_columns]
print(f"The dataset data_X contains {data_X.shape[0]} samples and "
      f"{data_X.shape[1]} columns")
data_X[numerical_columns].describe()
data_X_numerical = data_X[numerical_columns]<jupyter_output><empty_output><jupyter_text>The model  Train-test split the dataset<jupyter_code>from sklearn.model_selection import train_test_split

data_train, data_test, target_train, target_test = train_test_split(
    data_X_numerical, 
    target_y, 
    random_state=42, 
    test_size=0.25)
print(f"Number of samples in testing: {data_test.shape[0]} => "
      f"{data_test.shape[0] / data_X_numerical.shape[0] * 100:.1f}% of the"
      f" original set")
print(f"Number of samples in training: {data_train.shape[0]} => "
      f"{data_train.shape[0] / data_X_numerical.shape[0] * 100:.1f}% of the"
      f" original set")<jupyter_output>Number of samples in training: 36631 => 75.0% of the original set<jupyter_text>To display nice model diagram<jupyter_code>from sklearn import set_config
set_config(display='diagram')<jupyter_output><empty_output><jupyter_text>To create a logistic regression model in scikit-learn<jupyter_code>from sklearn.linear_model import LogisticRegression

model = LogisticRegression()<jupyter_output><empty_output><jupyter_text>Use the fit method to train the model using the training data and labels<jupyter_code>model.fit(data_train, target_train)<jupyter_output><empty_output><jupyter_text>Use the score method to check the model statistical performance on the test set<jupyter_code>accuracy = model.score(data_test, target_test)
print(f"Accuracy of logistic regression: {accuracy:.3f}")<jupyter_output>Accuracy of logistic regression: 0.807
{'fraction_non_alphanumeric': 0.11170354572114102, 'fraction_numerical': 0.010663822980538524, 'mean_word_length': 7.13882863340564, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '227307', 'license': 'Apache-2.0', 'n_tokens_mistral': 1302, 'n_tokens_neox': 1247, 'n_words': 262, 'path': '_notebooks/2021-05-21-FirstRegression.ipynb', 'repo_name': 'CecileGallioz/blog', 'size': '3759'}