Skip to main content

A simple example package

Project description

#ml 1 import pandas as pd import numpy as np import matplotlib.pyplot as plt import warnings import seaborn as sns #We do not want to see warnings warnings.filterwarnings("ignore")

data = pd.read_csv("uber.csv")

df = data.copy()

df.head

df.info()

df["pickup_datetime"] = pd.to_datetime(df["pickup_datetime"])

df.info()

df.describe() df.isnull().sum() df.drop(['key', 'Unnamed: 0'],axis = 1,inplace=True) df2=df.drop(["pickup_datetime"],axis = 1) df2 df2.corr()

fig,axis = plt.subplots(figsize = (10,6)) sns.heatmap(df2.corr(),annot = True)

df.dropna(inplace=True) df.plot(kind = "box",subplots = True,layout = (7,2),figsize=(15,20)) #Boxplot shows that dataset is free from outliers

def remove_outlier(df1, col): Q1 = df1[col].quantile(0.25) Q3 = df1[col].quantile(0.75) IQR = Q3 - Q1 lower_whisker = Q1 - 1.5 * IQR upper_whisker = Q3 + 1.5 * IQR df[col] = np.clip(df1[col], lower_whisker, upper_whisker) return df1

def treat_outliers_all(df1, col_list): for c in col_list: df1 = remove_outlier(df, c) return df1

df = treat_outliers_all(df, df.iloc[:, 0::])

df.plot(kind="box", subplots=True, layout=(7, 2), figsize=(15, 20)) # Boxplot shows that dataset is free from outliers

#Check the missing values now df.isnull().sum()

from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler

datatime_column = df['pickup_datetime'] df.drop('pickup_datetime', axis=1, inplace=True)

Scale the DataFrame without the datetime column

standard_scaler = StandardScaler() df_scaled_array = standard_scaler.fit_transform(df)

Convert the scaled array back to a DataFrame

df_scaled = pd.DataFrame(df_scaled_array, columns=df.columns)

Add the pickup_datetime column back

df_scaled['pickup_datetime'] = datatime_column.reset_index(drop=True)

#Take x as predictor variable x = df_scaled.drop("fare_amount", axis = 1) #And y as target variable y = df_scaled['fare_amount']

x['pickup_datetime'] = pd.to_numeric(pd.to_datetime(x['pickup_datetime']))

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 1)

from sklearn.linear_model import LinearRegression

lrmodel = LinearRegression() lrmodel.fit(x_train, y_train)

predict = lrmodel.predict(x_test)

from sklearn.metrics import mean_squared_error , r2_score lrmodelrmse = np.sqrt(mean_squared_error(y_test,predict)) r2 = r2_score(y_test,predict) print("RMSE error for the model is ", lrmodelrmse) print("R2 score for the model is ", r2)

#Let's Apply Random Forest Regressor from sklearn.ensemble import RandomForestRegressor rfrmodel = RandomForestRegressor(n_estimators = 100, random_state = 101)

#Fit the Forest rfrmodel.fit(x_train, y_train) rfrmodel_pred = rfrmodel.predict(x_test)

#Errors for the forest rfrmodel_rmse = np.sqrt(mean_squared_error(y_test , rfrmodel_pred)) rf_r2 = r2_score(y_test,rfrmodel_pred) print("RMSE value for Random Forest is:",rfrmodel_rmse) print("R2 score for the model is ", rf_r2)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

heycheckthisout-0.1.0.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

heycheckthisout-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file heycheckthisout-0.1.0.tar.gz.

File metadata

  • Download URL: heycheckthisout-0.1.0.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for heycheckthisout-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d6cbaadd6de75080fd3af1038920398d096f18139c73b669580908994681d2e0
MD5 dfa8d9e94307661300cf01579d84e821
BLAKE2b-256 88699999e06613eac2df0758149c67d1976a23a98397b5dd08feb16e421f650e

See more details on using hashes here.

File details

Details for the file heycheckthisout-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for heycheckthisout-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58a1bbf0bff4c89fcce626dcba3b9768ed1de08ba14d3217cc80185a6db0a3c8
MD5 a06fb012bd18e7494537ddc54160071a
BLAKE2b-256 7a1582d411c2f07793af98af7192cfde5715275cafdc03fc54ecc8d1149b5fe1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page