@@ -0,0 +1,2304 @@
+import pandas as pd%0Aimport numpy as np%0Aimport sklearn%0Afrom sklearn.preprocessing import LabelEncoder, StandardScaler%0Afrom sklearn.model_selection import train_test_split%0Aimport sklearn.linear_model as linear_model%0Aimport sklearn.metrics as metrics%0A%0A%0A# Encode the categorical features as numbers%0Adef number_encode_features(df):%0A    result = df.copy()%0A    encoders = %7B%7D%0A    for column in result.columns:%0A        if result.dtypes%5Bcolumn%5D == np.object:%0A            encoders%5Bcolumn%5D = LabelEncoder()%0A            result%5Bcolumn%5D = encoders%5Bcolumn%5D.fit_transform(result%5Bcolumn%5D)%0A    return result, encoders%0A%0A%0Aif __name__ == '__main__':%0A    original_data = pd.read_csv(%0A        %22adult.data.txt%22,%0A        names=%5B%0A            %22Age%22, %22Workclass%22, %22fnlwgt%22, %22Education%22, %22Education-Num%22, %22Martial Status%22,%0A            %22Occupation%22, %22Relationship%22, %22Race%22, %22Sex%22, %22Capital Gain%22, %22Capital Loss%22,%0A            %22Hours per week%22, %22Country%22, %22Target%22%5D,%0A        sep=r'%5Cs*,%5Cs*',%0A        engine='python',%0A        na_values=%22?%22)%0A%0A    print %22Data with missing values set to Nan:%22%0A    print original_data%0A    print '%5Cn'%0A%0A    encoded_data, _ = number_encode_features(original_data)%0A    print %22Encoded Data:%22%0A    print encoded_data.head()%0A    print '%5Cn'%0A%0A    # Split dataset to train and test%0A    X_train, X_test, y_train, y_test = train_test_split(%0A        encoded_data%5B%5B%22Age%22, %22Workclass%22, %22fnlwgt%22, %22Education%22, %22Education-Num%22, %22Martial Status%22,%0A            %22Occupation%22, %22Relationship%22, %22Race%22, %22Sex%22, %22Capital Gain%22, %22Capital Loss%22,%0A            %22Hours per week%22, %22Country%22%5D%5D, encoded_data%5B%22Target%22%5D, train_size=0.70)%0A%0A    # Scale the data%0A    scaler = StandardScaler()%0A    X_train = pd.DataFrame(scaler.fit_transform(X_train.astype(%22float64%22)), columns=X_train.columns)%0A    X_test = scaler.transform(X_test.astype(%22float64%22))%0A%0A    print %22X_train:%22%0A    print X_train.head()%0A    print '%5Cn'%0A%0A    print %22Y_train:%22%0A    print y_train.head()%0A    print '%5Cn'%0A%0A    print %22X_test:%22%0A    print X_test%0A    print '%5Cn'%0A%0A    print %22Y_test:%22%0A    print y_test.head()%0A    print '%5Cn'%0A%0A    # Define model%0A    cls = linear_model.LogisticRegression()%0A%0A    cls.fit(X_train, y_train)%0A    y_pred = cls.predict(X_test)%0A    cm = metrics.confusion_matrix(y_test, y_pred)%0A%0A    print cm%0A    print %22F1 score: %25f%22 %25 sklearn.metrics.f1_score(y_test, y_pred)%0A%0A%0A

add solution from teacher