美文网首页
【Python | sklearn】Logistic regre

【Python | sklearn】Logistic regre

作者: 盐果儿 | 来源:发表于2022-08-18 04:45 被阅读0次

import pandas as pd

from sklearn.linear_model import LogisticRegression

df = pd.read_csv('https://...')

# Transform the string data into one-hot encoding

df['male'] = df['Sex'] == 'male'

X = df[['Fare', 'Age', 'male']].values

y = df['Survived'].values

model = LogisticRegression()

model.fit(X, y)

# Print the a, b and c of 0 = ax + by + c function: [[0.001, -0.002]] [-0.510]

print(model.coef_, model.intercept_)

print(model.predict(X))

# Predict one passenger

print(model.predict(X[:1]))

# Predict first five passengers

print(model.predict(X[:5]))

# The accuracy score

print(model.score(X, y))

Using the dataset from sklearn

import pandas as pd

from sklearn.datasets import load_breast_cancer

cancer_data = load_breast_cancer()

print(cancer_data.keys())

# Print the detailed description of the dataset

print(cancer_data['DESC'])

# Print the shape of the data

print(cancer_data['data'].shape)

df = pd.DataFrame(cancer_data['data'], columns=cancer_data['feature_names'])

# Print the first 5 rows of data

print(df.head())

X = df[cancer_data.feature_names].values

y = df['target'].values

model = LogisticRegression()

model.fit(X, y)

model.predict([X[0]])

model.score(X, y)

相关文章

网友评论

      本文标题:【Python | sklearn】Logistic regre

      本文链接:https://www.haomeiwen.com/subject/zxasgrtx.html