代码拉取完成,页面将自动刷新
import numpy as np
import pandas as pd
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import matplotlib.pylab as plt
from dmba import classificationSummary
mower_df = pd.read_csv('RidingMowers.csv')
da_reg = LinearDiscriminantAnalysis()
da_reg.fit(mower_df.drop(columns=['Ownership']),
mower_df['Ownership'])
print('Coefficients', da_reg.coef_)
print('Intercept', da_reg.intercept_)
da_reg = LinearDiscriminantAnalysis()
da_reg.fit(mower_df.drop(columns=['Ownership']), mower_df['Ownership'])
result_df = mower_df.copy()
result_df['Dec. Function'] = da_reg.decision_function(mower_df.drop(columns=
['Ownership']))
result_df['Prediction'] = da_reg.predict(mower_df.drop(columns=['Ownership']))
result_df['p(Owner)'] = da_reg.predict_proba(mower_df.drop(columns=['Ownership']))[:, 1]
accidents_df = pd.read_csv('accidents.csv')
lda_reg = LinearDiscriminantAnalysis()
lda_reg.fit(accidents_df.drop(columns=['MAX_SEV']),
accidents_df['MAX_SEV'])
print('Coefficients and intercept')
fct = pd.DataFrame([lda_reg.intercept_],
columns=lda_reg.classes_, index=['constant'])
fct = fct.append(pd.DataFrame(lda_reg.coef_.transpose(),
columns=lda_reg.classes_,
index=list(accidents_df.columns)[:-1]))
print(fct)
print()
classificationSummary(accidents_df['MAX_SEV'],
lda_reg.predict(accidents_df.drop(columns=['MAX_SEV'])),
class_names=lda_reg.classes_)
result = pd.concat([
pd.DataFrame({'Classification':
lda_reg.predict(accidents_df.drop(columns=['MAX_SEV'])),
'Actual': accidents_df['MAX_SEV']}),
pd.DataFrame(lda_reg.decision_function(accidents_df.drop(columns=
['MAX_SEV'])),
columns=['Score {}'.format(cls) for cls in
lda_reg.classes_]),
pd.DataFrame(lda_reg.predict_proba(accidents_df.drop(columns=
['MAX_SEV'])),
columns=['Propensity {}'.format(cls) for cls in
lda_reg.classes_])
], axis=1)
pd.set_option('precision',2)
pd.set_option('chop_threshold', .01)
print(result.head())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。