python常用语句总结(机器学习...)



            python常用语句总结(机器学习...)

# 导入pandas用于数据分析
import pandas as pd
# 导入numpy,并命名为 np
import numpy as np

# 利用pandas的read_csv模块传入数据文件地址,并返回数据(dataframe格式)保存到data,测试数据可同样读取或从中分割,见下面
data = pd.read_csv('../Datasets/Breast-Cancer/breast-cancer-train.csv') #地址可为路径
data = pd.read_csv('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt')#地址可为URL
# 有时需要自己设定特征列表
column_names = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape', 'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli', 'Mitoses', 'Class']
data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data', names = column_names )

#输出数据量和维度
data.shape
# 观察前几行数据确定数据种类,数字型,类别型甚至缺省值
data.head()

# 缺省值的处理
data = data.replace(to_replace = '?', value = np.nan)
##1.丢弃含缺省值样本
data = data.dropna(how = 'any')
##2.用平均值赋值
data.fillna(data.mean(), inplace=True)

# 查看数据的统计特性
data.info()

# 特征选择
X = data[['pclass', 'age', 'sex']]
y = data['survived']

# 从sklearn.model_selection中的train_test_split模块用于数据分割,随机25%做测试样本
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state = 33)

# 使用scikit-learn.feature_extraction中的特征转换器
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer(sparse=False)
# 转换特征后,凡是类别型的特征都单独剥离出来,独成一列特征,数值型的则保持不变
X_train = vec.fit_transform(X_train.to_dict(orient='record'))
X_test = vec.transform(X_test.to_dict(orient='record'))
print(vec.feature_names_)

# 检查样本的数量和类别分布
y_train.value_counts()
y_test.value_counts()

#有时需要标准化数据,使每个维度的特征数据方差为1,均值为0,使预测结果不会被某些维度过大的特征值而主导
# 从sklearn.preprocessing里导入StandardScaler
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)

#使用不同的模型进行训练 .
***.fit(X_train, y_train)

 #使用训练好的模型进行预测
y_predict = ***.predict(X_test)

# 从sklearn.metrics导入classification_report
from sklearn.metrics import classification_report
# 输出预测准确性
print('The Accuracy is :', ***.score(X_test, y_test))
# 输出更加详细的分类性能
print(classification_report(y_predict, y_test, target_names = ['died', 'survived']))

 

       十个最常用机器学习算法编码指南(Python版)

 

Machine Learning Algorithms Python Code
LinearRegression线性回归

#Import Library
#Import other necessary libraries like pandas,
#numpy...
from sklearn import linear_model
#Load Train and Test datasets
#Identify feature and response variable(s) and
#values must be numeric and numpy arrays
x_train=input_variables_values_training_datasets
y_train=target_variables_values_training_datasets
x_test=input_variables_values_test_datasets
#Create linear regression object
linear = linear_model.LinearRegression()
#Train the model using the training sets and
#check score
linear.fit(x_train, y_train)
linear.score(x_train, y_train)
#Equation coefficient and Intercept
print('Coefficient: \n', linear.coef_)
print('Intercept: \n', linear.intercept_)
#Predict Output
predicted= linear.predict(x_test)

 

Logistic Regression逻辑回归

#Import Library  

from sklearn.linear_model import LogisticRegression  

#Assumed you have, X (predictor) and Y (target)  

#for training data set and x_test(predictor)  

#of test_dataset  

#Create logistic regression object  

model = LogisticRegression()  

#Train the model using the training sets  

#and check score  

model.fit(X, y)  

model.score(X, y)  

#Equation coefficient and Intercept  

print('Coefficient: \n', model.coef_)  

print('Intercept: \n', model.intercept_)  

#Predict Output  

predicted= model.predict(x_test) 

 

Decision Tree决策树

#Import Library

#Import other necessary libraries like pandas, numpy...

from sklearn import tree

#Assumed you have, X (predictor) and Y (target) for

#training data set and x_test(predictor) of

#test_dataset

#Create tree object

model = tree.DecisionTreeClassifier(criterion='gini')

#for classification, here you can change the

#algorithm as gini or entropy (information gain) by

#default it is gini

#model = tree.DecisionTreeRegressor() for

#regression

#Train the model using the training sets and check

#score

model.fit(X, y)

model.score(X, y)

#Predict Output

predicted= model.predict(x_test)

 

SVM (Support Vector Machine)支持向量机

#Import Library

from sklearn import svm

#Assumed you have, X (predictor) and Y (target) for

#training data set and x_test(predictor) of test_dataset

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值