#!/usr/bin/env python # coding: utf-8 # In[15]: import pandas as pd import numpy as np import seaborn as sns import warnings warnings.filterwarnings("ignore") from sklearn.linear_model import LinearRegression from sklearn.metrics import r2_score,mean_squared_error from sklearn.compose import ColumnTransformer from sklearn.preprocessing import OneHotEncoder, StandardScaler from sklearn.pipeline import Pipeline from sklearn.model_selection import train_test_split # In[4]: pip install xldr # In[6]: ls # In[7]: df=pd.read_excel("cars.xls") # In[8]: df.head() # In[9]: #veri ön işleme,standartlıştırma # In[10]: X=df.drop("Price",axis=1) # In[13]: y=df["Price"] # In[16]: X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42) # In[17]: preprocess=ColumnTransformer( transformers=[ ("num",StandardScaler(),["Mileage","Cylinder","Liter","Doors"]), ("cat",OneHotEncoder(),["Make","Model","Trim","Type"]) ] ) # In[20]: my_model=LinearRegression() pipe=Pipeline(steps=[("preprocessor",preprocess),("model",my_model)]) # In[21]: pipe.fit(X_train,y_train) # In[24]: y_pred=pipe.predict(X_test) rmse=mean_squared_error(y_test,y_pred)**0.5 r2=r2_score(y_test,y_pred) r2,rmse # In[25]: #modeli yayma, kullanıma sunma # ### Streamlit # In[28]: get_ipython().system('pip install streamlit') # In[29]: import streamlit as st # In[34]: def price(make,model,trim,mileage,car_type,cylinder,liter,doors,cruise,sound,leather): input_data=pd.DataFrame({"Make":[make], "Model":[model], "Trim":[trim], "Mileage":[mileage], "Type":[car_type], "Cylinder":[cylinder], "Liter":[liter], "Doors":[doors], "Cruise":[cruise], "Sound":[sound], "Leather":[leather]}) prediction=pipe.predict(input_data)[0] return prediction st.title("Car Price Prediction:blue_car:@neslisahozturk") st.write("Select the features of the car") make=st.selectbox("Brand",df["Make"].unique()) model=st.selectbox("Model",df[df["Make"]==make]["Model"].unique()) trim=st.selectbox("Trim",df[(df["Make"]==make)&(df["Model"]==model)]["Trim"].unique()) mileage=st.number_input("Km",100,df["Mileage"].max()) car_type=st.selectbox("Car Type",df["Type"].unique()) cylinder=st.selectbox("Cylinder",df["Cylinder"].unique()) liter=st.number_input("Fuel Volume",df["Liter"].min(),df["Liter"].max()) doors=st.selectbox("Number of Doors",df["Doors"].unique()) cruise=st.radio("Velocity Constant",[True,False]) sound=st.radio("Sound System",[True,False]) leather=st.radio("Leather Seats",[True,False]) if st.button("Predict"): pred=price(make,model,trim,mileage,car_type,cylinder,liter,doors,cruise,sound,leather) st.write("Fiyat:$",round(pred[0],2)) # In[ ]: