Skip to content

Instantly share code, notes, and snippets.

View tommydangerous's full-sized avatar
🧙
Fire!

DANGerous tommydangerous

🧙
Fire!
View GitHub Profile
@tommydangerous
tommydangerous / create_matrix.py
Created June 17, 2021 05:36
create_matrix.py
import numpy
input_arr = numpy.array([
[10, 20, 30],
[40, 50, 60],
])
@tommydangerous
tommydangerous / create_vectors.py
Created June 17, 2021 05:35
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],
@tommydangerous
tommydangerous / baseline_accuracy.py
Created June 11, 2021 05:52
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}')
@tommydangerous
tommydangerous / calculate_model_accuracy.py
Created June 11, 2021 05:51
calculate_model_accuracy.py
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy score: {accuracy}')
@tommydangerous
tommydangerous / predict_test_data.py
Created June 11, 2021 05:49
predict_test_data.py
y_pred = classifier.predict(X_test)
@tommydangerous
tommydangerous / prepare_test_data.py
Created June 11, 2021 05:46
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'])
@tommydangerous
tommydangerous / train_model.py
Created June 11, 2021 05:45
train_model.py
classifier.fit(X_train, y_train)
@tommydangerous
tommydangerous / choose_algorithm.py
Created June 11, 2021 05:41
choose_algorithm.py
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(max_iter=10000)
@tommydangerous
tommydangerous / select_features.py
Created June 11, 2021 05:39
select_features.py
features_to_use = [
'Age',
'SibSp',
'Parch',
'Fare',
'can_vote',
] + new_column_names
X_train = df[features_to_use].copy()
@tommydangerous
tommydangerous / encode_values.py
Created June 11, 2021 05:38
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]