A university student wielding a knife attacked riders aboard a subway train in Taiwan's capital on Wednesday, killing four people and injuring at least 21 others, police and local media said.

The seemingly random attack occurred at about 4 p.m. at the start of the evening rush hour aboard a train on Taipei's Banqiao line, which leads to the city's southwestern suburbs.

Photos showed the floor of one of the train's cars and the adjacent station platform splattered with blood and strewn with backpacks, umbrellas and other items.

The government's Central News Agency and other media described panicked passengers rushing from the train when it arrived at Jiangzicui station, shouting for those waiting on the platform to flee.

They said security guards quickly called police, who arrived within minutes and took the man into custody. They said the man reeked of alcohol.

"Everyone in the train was trying to run to the other side. No one tried to stop the criminal suspect," a woman injured in the attack told reporters at a local hospital. The woman, who was stabbed in the hand, would only give her surname, Yen.

Pictures showed the attacker handcuffed sitting against a wall, his face covered in blood. He was dressed in white tennis shoes, black shorts and a red T-shirt.

A police spokesman, speaking on routine condition of anonymity, said police were preparing to charge the suspect with murder.

Speaking to reporters, Chen Kuo-en, the police chief of Xinbeishi, where Jiangzicui station is located, identified the attacker as a 21-year-old second-year student at Tunghai University in the central city of Taichung.

Chen said the man told police he had wanted to do something "shocking and big" and had plotted to carry out the attack from childhood. Chen said no other motive was presently known and the suspect had no record of mental health problems.

"He's aware of the legal responsibility of his crime," Chen said.

A Tunghai University official confirmed the suspect was a student there and said a school counselor had seen him in April after he posted a message on his Facebook page saying he "wanted to do something big this year."

However, the matter was dropped after the counselor reported nothing unusual, said the spokesman, who declined to be further identified.

Chen said the suspect had grown up around the station and might have chosen it because of his familiarity with the area. He said he boarded the train two stations earlier and started stabbing passengers after it passed the first one.

Police said the man used a 3-inch knife in the attack, and was also carrying a second blade three times as long. Although the suspect had been drinking, a breath analysis test showed he was below the legal limit.

Police said a 47-year-old woman and two men, one in his 20s and the other in his 30s, were declared dead on arrival at hospital. A 62-year-old woman died in surgery four hours after the attack.

The suspect struck his victims in the chest and stomach and injured at least 21 others in the attack.

Service was restored about an hour after the attack with police officers assigned to all trains and tighter security checks in stations.

Street crime is extremely rare in Taipei, one of Asia's safest cities, and violent incidents are practically unheard of in the city's extensive and well managed subway system.