Python 有人知道如何在训练每个模型之前和之后放置秒表,以评估哪个模型更快吗?

Python 有人知道如何在训练每个模型之前和之后放置秒表,以评估哪个模型更快吗?,python,pandas,data-science,python-module,Python,Pandas,Data Science,Python Module,我创建了贷款风险预测python机器学习模型,用于预测借款人是否能够支付银行贷款。我的模型工作得非常好,有78%的准确率。然而,我的教授告诉我” 在训练每个模型之前和之后放置一个秒表,以评估哪一个更快,甚至更好,在速度和精度之间达成最佳折衷(我们需要快速准确的模型)。”,但我不知道如何在模型中添加秒表。我在网上搜索过这件事,但并没有得到任何关于如何在模型中放置秒表的信息。请让我知道,如果有人知道如何把秒表之前和之后,培训每个模型 ##我的Python预测模型 # Importing the Li

我创建了贷款风险预测python机器学习模型,用于预测借款人是否能够支付银行贷款。我的模型工作得非常好,有78%的准确率。然而,我的教授告诉我” 在训练每个模型之前和之后放置一个秒表,以评估哪一个更快,甚至更好,在速度和精度之间达成最佳折衷(我们需要快速准确的模型)。”,但我不知道如何在模型中添加秒表。我在网上搜索过这件事,但并没有得到任何关于如何在模型中放置秒表的信息。请让我知道,如果有人知道如何把秒表之前和之后,培训每个模型

##我的Python预测模型

# Importing the Libraries
import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.preprocessing import StandardScaler
import seaborn as sns
sns.set(style="white", color_codes=True)

# Importimg the dataset and displaying first 10 values 
data = pd.read_csv("credit_train.csv")
data.head(10)

# Find null values
data.isnull().sum()

# Drop null records
data = data.dropna(axis=0)

#To get basic information and statistics
data.describe()

# Check number of unique values
data["Home Ownership"].unique()
data["Home Ownership"].value_counts()

# Data Representation
sns.FacetGrid(data,hue="Loan Status",size=4) \
.map(plt.scatter,"Current Loan Amount","Monthly Debt") \
.add_legend()
plt.show()

# Categorical attributes visualization
sns.countplot(x="Loan Status",data=data)
sns.countplot(x="Term",data=data)
sns.countplot(x="Years in current job",data=data)
sns.countplot(x="Home Ownership",data=data)
sns.countplot(x="Loan Status",hue="Home Ownership",data=data)
sns.countplot(x="Loan Status",hue="Term",data=data)

# Numerical attributes visualization
sns.distplot(data['Current Loan Amount'])
sns.distplot(data['Annual Income'])
sns.distplot(data['Credit Score'])
sns.distplot(data['Monthly Debt'])
sns.distplot(data['Current Credit Balance'])

#Normalization and log transformation 
data['Current Loan Amount Log'] = np.log(data['Current Loan Amount']+1)
sns.distplot(data["Current Loan Amount Log"])
data['Credit Score Log'] = np.log(data['Credit Score']+1)
sns.distplot(data["Credit Score Log"])
data['Annual Income Log'] = np.log(data['Annual Income']+1)
sns.distplot(data["Annual Income Log"])
data['Monthly Debt Log'] = np.log(data['Monthly Debt']+1)
sns.distplot(data["Monthly Debt Log"])
data['Current Credit Balance Log'] = np.log(data['Current Credit Balance']+1)
sns.distplot(data["Current Credit Balance Log"])

# Drop unnecessary columns
data = data.drop(['Loan ID', 'Customer ID', "Current Loan Amount", "Credit Score", "Annual Income", 'Years in current job', 'Current Credit Balance', 'Purpose', 'Monthly Debt'], axis=1)

# Correlation Matrix of the columns given below
cols = ['Credit Score Log','Annual Income Log','Monthly Debt Log',
        'Current Credit Balance Log','Current Credit Balance Log','Current Loan Amount Log','Tax Liens','Years of Credit History', 'Maximum Open Credit']
f, ax = plt.subplots(figsize=(15, 10))
cm = np.corrcoef(df.values.T)
sns.set(font_scale=1.5)
hm = sns.heatmap(cm, cbar=True, annot=True, square=True, fmt='.2f', annot_kws={'size': 15}, yticklabels=cols, xticklabels=cols)
plt.show()

# Label Encoding
from sklearn.preprocessing import LabelEncoder
cols = ['Loan Status',"Term","Home Ownership"]
le = LabelEncoder()
for col in cols:
    data[col] = le.fit_transform(data[col])

# data slicing
x = data.drop(columns=['Loan Status'], axis=1)
y = data['Loan Status']

# Train-Test Split
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)

# Random forest model
# Importing libraries and classes
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model.fit(x_train,y_train)

# Find accuracy in training and testing model
model.score(x_train,y_train)
model.score(x_test,y_test)

# Predict the value of test dataset
predicted = model.predict(x_test)

# Generating Report
from sklearn import metrics
print(metrics.classification_report(y_test, predicted))

# Confusion Matrix
print(metrics.confusion_matrix(y_test, predicted))

您可以通过使用
time.time()
函数在上下文中读取代码前后的时间来复制计时器。请注意,时间计算可能需要更改,如在
stmt2
中所做的那样,以获得精度。

请参阅我最近给某人的有关方法计时的答案。使用time module@carperyeltsin没有理由这样评论。当有人投票以重复的方式结束您的问题时,Bram的评论将自动生成。这只是暗示你的问题已经在其他地方得到了回答。这不是一种准确的时间编码方法。使用
timeit
模块。是的,它可能无法准确执行。但这是最基本的。
from time import time

t_bef = time()
function()
t_aft = time()

print("function took", t_aft-t_bef, "seconds")                # stmt1
print("function took", (t_aft-t_bef)*1000, "microseconds")    # stmt2