-
Notifications
You must be signed in to change notification settings - Fork 0
/
salary_predictor.py
77 lines (60 loc) · 2.27 KB
/
salary_predictor.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
import streamlit as st
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.metrics import mean_squared_error, r2_score
# Function to load data from CSV
def load_data(filename):
return pd.read_csv(filename)
# Function to preprocess the data
def preprocess_data(data):
X = data.drop(columns=['salary'])
y = data['salary']
numeric_features = ['years_of_experience']
categorical_features = ['degree']
numeric_transformer = Pipeline(steps=[
('scaler', StandardScaler())
])
categorical_transformer = Pipeline(steps=[
('onehot', OneHotEncoder())
])
preprocessor = ColumnTransformer(
transformers=[
('num', numeric_transformer, numeric_features),
('cat', categorical_transformer, categorical_features)
])
X_preprocessed = preprocessor.fit_transform(X)
return X_preprocessed, y, preprocessor
# Function to train a model
def train_model(X, y):
model = LinearRegression()
model.fit(X, y)
return model
# Main function to display the interface and make predictions
def main():
# Title and subheader
st.title('Software Developer Salary Prediction')
st.subheader('Using Machine Learning')
# Load data
filename = 'salaries.csv'
data = load_data(filename)
# Preprocess data
X, y, preprocessor = preprocess_data(data)
# Train model
model = train_model(X, y)
# User input for prediction
st.subheader('Predict Salary')
experience = st.slider('Years of Experience', 1, 10, 5)
degree = st.selectbox('Degree', ['Bachelors', 'Masters', 'Phd'])
# Convert user input to DataFrame
user_data = pd.DataFrame([[experience, degree]], columns=['years_of_experience', 'degree'])
# Preprocess user input
user_data_transformed = preprocessor.transform(user_data)
# Predict and display the salary
prediction = model.predict(user_data_transformed)[0]
st.write(f'Predicted Salary: ${prediction:,.2f}')
if __name__ == '__main__':
main()