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

mltools2024-0.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

mltools2024-0.1.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mltools2024-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5d6ee736816de4a6af5a45ffcfb27d45c10d3c9968f9d9eb0f22f387fca7d09b
MD5 4e26676f5fd654f27a0df264b5257192
BLAKE2b-256 ae67f8c0b177ab9767f01835ea389590f277a375935c4d26547017aac4495b43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mltools2024-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for mltools2024-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6f841fb61b70ff768544225416134193ff900801fe20205bfb9bdfc1cc6b001
MD5 426328b101215c1ad2f215a08fa1bacc
BLAKE2b-256 db2e775c2e99fc02f1ebd0d7bb3f2028fee648155fe0a75d05acad68e7658574

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