机器学习

线性回归

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn import linear_model

from sklearn import metrics


data = pd.read_csv('Advertising.csv') 

x = data[['TV', 'Radio', 'Newspaper']]

y = data['Sales']

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1,random_state=1)

reg=linear_model.LinearRegression()

reg.fit(x_train,y_train)

metrics.mean_squared_error(y_test,y_pred)



逻辑回归


data=pd.read_csv('iris.data',header=None)

data[4]=pd.Categorical(data[4]).codes

x,y=np.split(data.values,(4,),axis=1)

cls=linear_model.LogisticRegression()

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.1,random_state=0)

cls.fit(x_train,y_train)

y_pred=cls.predict(x_test)

y_hat_prob = lr.predict_proba(x)   #概率


print(‘准确度:%.2f%%’ % (100*np.mean(y_pred==y_test))




阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭