-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuvervised Cars.py
87 lines (71 loc) · 3.53 KB
/
Suvervised Cars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# import pandas as pd
# from sklearn.model_selection import train_test_split
# from sklearn.linear_model import LogisticRegression
# from sklearn.preprocessing import LabelEncoder
# from sklearn.metrics import accuracy_score
# # Load the data
# url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data'
# data = pd.read_csv(url, header=None, delimiter=', ', engine='python')
# # Rename the columns
# columns = ['age', 'workclass', 'fnlwgt', 'education', 'education-num', 'marital-status',
# 'occupation', 'relationship', 'race', 'sex', 'capital-gain', 'capital-loss',
# 'hours-per-week', 'native-country', 'income']
# data.columns = columns
# # Convert categorical variables using LabelEncoder
# label_encoder = LabelEncoder()
# data['workclass'] = label_encoder.fit_transform(data['workclass'])
# data['education'] = label_encoder.fit_transform(data['education'])
# data['marital-status'] = label_encoder.fit_transform(data['marital-status'])
# data['occupation'] = label_encoder.fit_transform(data['occupation'])
# data['relationship'] = label_encoder.fit_transform(data['relationship'])
# data['race'] = label_encoder.fit_transform(data['race'])
# data['sex'] = label_encoder.fit_transform(data['sex'])
# data['native-country'] = label_encoder.fit_transform(data['native-country'])
# data['income'] = label_encoder.fit_transform(data['income'])
# # Split the data into training and testing sets
# X = data.drop('income', axis=1)
# y = data['income']
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# # Train the logistic regression model
# model = LogisticRegression()
# model.fit(X_train, y_train)
# # Make predictions on the test set
# y_pred = model.predict(X_test)
# # Evaluate the model's performance
# accuracy = accuracy_score(y_test, y_pred)
# print(f"Accuracy score: {accuracy}")
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from sklearn.feature_selection import RFE
# Load the data
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data'
data = pd.read_csv(url, header=None, delimiter=', ', engine='python')
# Rename the columns
columns = ['age', 'workclass', 'fnlwgt', 'education', 'education-num', 'marital-status',
'occupation', 'relationship', 'race', 'sex', 'capital-gain', 'capital-loss',
'hours-per-week', 'native-country', 'income']
data.columns = columns
# Convert categorical variables using LabelEncoder
label_encoder = LabelEncoder()
categorical_cols = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'sex', 'native-country']
for col in categorical_cols:
data[col] = label_encoder.fit_transform(data[col])
data['income'] = label_encoder.fit_transform(data['income'])
# Select a subset of features using RFE
X = data.drop('income', axis=1)
y = data['income']
selector = RFE(LogisticRegression(max_iter=10000), n_features_to_select=10)
X_new = selector.fit_transform(X, y)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_new, y, test_size=0.2, random_state=42)
# Train the logistic regression model with regularization
model = LogisticRegression(max_iter=10000, C=0.1)
model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = model.predict(X_test)
# Evaluate the model's performance
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy score: {accuracy}")