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)
网友评论