赞
踩
前言
系列专栏:机器学习:高级应用与实践【项目实战100+】【2024】✨︎
在本专栏中不仅包含一些适合初学者的最新机器学习项目,每个项目都处理一组不同的问题,包括监督和无监督学习、分类、回归和聚类,而且涉及创建深度学习模型、处理非结构化数据以及指导复杂的模型,如卷积神经网络、门控递归单元、大型语言模型和强化学习模型
本文旨在使用Keras
等深度学习库,并熟悉神经网络的基础。
您可以从免费提供的UCI机器学习存储库中找到葡萄酒质量数据集。数据集由数据中包含的 12 个变量组成。其中少数如下——
# Import Required Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Read in white wine data
white = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv", sep =';')
# Read in red wine data
red = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv", sep =';')
# First rows of `red`
red.head()
输出
# Last rows of `white`
white.tail()
输出
取五行红酒的数据样本
# Take a sample of five rows of `red`
red.sample(5)
输出
# Describe `white`
white.describe()
输出
# Double check for null values in `red`
pd.isnull(red)
输出
# Create Histogram fig, ax = plt.subplots(1, 2) ax[0].hist(red.alcohol, 10, facecolor ='red', alpha = 0.5, label ="Red wine") ax[1].hist(white.alcohol, 10, facecolor ='white', ec ="black", lw = 0.5, alpha = 0.5, label ="White wine") fig.subplots_adjust(left = 0, right = 1, bottom = 0, top = 0.5, hspace = 0.05, wspace = 1) ax[0].set_ylim([0, 1000]) ax[0].set_xlabel("Alcohol in % Vol") ax[0].set_ylabel("Frequency") ax[1].set_ylim([0, 1000]) ax[1].set_xlabel("Alcohol in % Vol") ax[1].set_ylabel("Frequency") fig.suptitle("Distribution of Alcohol in % Vol") plt.show()
输出
# Add `type` column to `red` with price one red['type'] = 1 # Add `type` column to `white` with price zero white['type'] = 0 # Concat `white` with `red` wines = pd.concat([red,white], ignore_index = True) # Import `train_test_split` from `sklearn.model_selection` from sklearn.model_selection import train_test_split X = wines.iloc[:, 0:11] y = np.ravel(wines.type) # Splitting the data set for training and validating X_train, X_test, y_train, y_test = train_test_split( X, y, test_size = 0.34, random_state = 45)
# Import `Sequential` from `keras.models` from keras.models import Sequential # Import `Dense` from `keras.layers` from keras.layers import Dense # Initialize the constructor model = Sequential() # Add an input layer model.add(Dense(12, activation ='relu', input_shape =(11, ))) # Add one hidden layer model.add(Dense(9, activation ='relu')) # Add an output layer model.add(Dense(1, activation ='sigmoid')) # Model output shape model.output_shape # Model summary model.summary() # Model config model.get_config() # List all weight tensors model.get_weights() model.compile(loss ='binary_crossentropy', optimizer ='adam', metrics =['accuracy'])
# Training Model
model.fit(X_train, y_train, epochs = 3,
batch_size = 1, verbose = 1)
# Predicting the Value
y_pred = model.predict(X_test)
print(y_pred)
输出
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。