Skip to content

Instantly share code, notes, and snippets.

Avatar
🔥

DANGerous tommydangerous

🔥
View GitHub Profile
View create_matrix.py
import numpy
input_arr = numpy.array([
[10, 20, 30],
[40, 50, 60],
])
View create_vectors.py
# Load Numpy module
import numpy as np
# Creating a 1-D list (horizontal)
list1 = [2, 3, 5]
# Creating a 1-D list (vertical)
list2 = [
[20],
[30],
View baseline_accuracy.py
baseline_accuracy_score = y_test.value_counts()[0] / len(y_test)
print(f'Model performance. : {accuracy}')
print(f'Baseline performance: {baseline_accuracy_score}')
View calculate_model_accuracy.py
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy score: {accuracy}')
View predict_test_data.py
y_pred = classifier.predict(X_test)
View prepare_test_data.py
X_test = X_test_raw.copy()
# Add columns
X_test['can_vote'] = X_test['Age'].apply(lambda age: 1 if age >= 18 else 0)
X_test.loc[:, 'cabin_letter'] = X_test['Cabin'].apply(
lambda cabin: cabin[0] if cabin and type(cabin) is str else None,
)
# Remove columns
X_test = X_test.drop(columns=['Name', 'PassengerId'])
View train_model.py
classifier.fit(X_train, y_train)
View choose_algorithm.py
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(max_iter=10000)
View select_features.py
features_to_use = [
'Age',
'SibSp',
'Parch',
'Fare',
'can_vote',
] + new_column_names
X_train = df[features_to_use].copy()
View encode_values.py
from sklearn.preprocessing import OneHotEncoder
categorical_columns = ['Pclass', 'Sex', 'Embarked', 'cabin_letter']
categorical_encoder = OneHotEncoder(handle_unknown='ignore')
categorical_encoder.fit(df[categorical_columns])
# Add the new columns to the data
new_column_names = []
for idx, cat_column_name in enumerate(categorical_columns):
values = categorical_encoder.categories_[idx]